Commit | Line | Data |
---|---|---|
a5d51291 TM |
1 | #define PTHREAD_FREEWHEEL |
2 | ||
a8e71e8f TM |
3 | #include <signal.h> |
4 | #include <pthread.h> | |
5 | #include <pthread_extra.h> | |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <assert.h> | |
9 | #include <unistd.h> | |
10 | ||
a5d51291 TM |
11 | //#define printf(...) (0) |
12 | ||
3109d0d1 TM |
13 | pthread_t main_thread; |
14 | ||
a8e71e8f TM |
15 | void *thread_test(void *arg) { |
16 | //Whole process dies if you kill thread immediately before it is pausable | |
17 | //pthread_pause_enable(); | |
18 | while(1) { | |
3109d0d1 | 19 | pthread_nsleep(0, 1000*1000*300); |
de59b17e | 20 | pthread_pause_all(); |
a8e71e8f | 21 | printf("Running%s!\n", (char *)arg); |
de59b17e TM |
22 | pthread_unpause_all(); |
23 | pthread_pause(main_thread); | |
3109d0d1 | 24 | pthread_unpause(main_thread); |
a8e71e8f TM |
25 | } |
26 | } | |
27 | ||
28 | int main() { | |
29 | ||
3109d0d1 TM |
30 | main_thread = pthread_self(); |
31 | ||
a8e71e8f TM |
32 | pthread_t a, b; |
33 | pthread_pause_enable(); //Will get inherited by all threads from now on | |
34 | //That way you can be sure it is pausable immediately | |
7e0f3dba TM |
35 | pthread_extra_create(&a, NULL, thread_test, " A"); |
36 | pthread_extra_create(&b, NULL, thread_test, " B"); | |
3109d0d1 | 37 | //pthread_sleep(1); |
24286ba9 TM |
38 | |
39 | //printf("OK\n"); | |
40 | /* | |
41 | for(int32_t i = 1;i>0;i++) { | |
42 | pthread_pause(a); | |
43 | pthread_pause(a); | |
44 | pthread_unpause(a); | |
45 | pthread_unpause(a); | |
46 | } | |
47 | */ | |
48 | /* | |
49 | exit(23); | |
50 | pthread_pause(a); | |
51 | pthread_unpause(b); | |
52 | */ | |
a8e71e8f TM |
53 | |
54 | while(1) { | |
55 | pthread_pause(b); | |
56 | pthread_unpause(a); | |
a5d51291 TM |
57 | pthread_pause_disable(); |
58 | pthread_pause_disable(); | |
59 | pthread_pause_disable(); | |
a8e71e8f | 60 | printf("SWITCH A:\n"); |
a5d51291 TM |
61 | pthread_pause_enable(); |
62 | pthread_pause_enable(); | |
63 | pthread_pause_enable(); | |
3109d0d1 | 64 | pthread_sleep(2); |
a8e71e8f | 65 | |
a5d51291 | 66 | pthread_pause_all(); |
a8e71e8f | 67 | printf("SWITCH B:\n"); |
a5d51291 | 68 | pthread_unpause_all(); |
a8e71e8f TM |
69 | pthread_pause(a); |
70 | pthread_unpause(b); | |
3109d0d1 | 71 | pthread_sleep(2); |
a8e71e8f | 72 | |
a5d51291 TM |
73 | //pthread_pause_disable(); |
74 | pthread_pause_all(); | |
a8e71e8f | 75 | printf("SWITCH A+B:\n"); |
a5d51291 TM |
76 | pthread_unpause_all(); |
77 | //pthread_pause_enable(); | |
a8e71e8f TM |
78 | pthread_unpause(a); |
79 | pthread_unpause(b); | |
3109d0d1 | 80 | pthread_sleep(1); |
9b4be8e8 | 81 | |
a5d51291 TM |
82 | //pthread_pause_disable(); |
83 | pthread_pause_all(); | |
9b4be8e8 | 84 | printf("SWITCH MAIN ONLY:\n"); |
a5d51291 | 85 | //pthread_pause_enable(); |
9b4be8e8 | 86 | pthread_pause_all(); |
de59b17e | 87 | //printf("\n"); |
3109d0d1 | 88 | pthread_sleep(1); |
9b4be8e8 TM |
89 | printf("SWITCH MAIN A+B:\n"); |
90 | pthread_unpause_all(); | |
de59b17e | 91 | //printf("\n"); |
3109d0d1 | 92 | pthread_sleep(1); |
a8e71e8f TM |
93 | } |
94 | ||
95 | pthread_join(a, NULL); | |
96 | pthread_join(b, NULL); | |
97 | printf("DIEDED!\n"); | |
98 | } |