| 1 | #define PTHREAD_FREEWHEEL |
| 2 | |
| 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 | |
| 11 | //#define printf(...) (0) |
| 12 | |
| 13 | pthread_t main_thread; |
| 14 | |
| 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) { |
| 19 | pthread_nsleep(0, 1000*1000*300); |
| 20 | pthread_pause_all(); |
| 21 | printf("Running%s!\n", (char *)arg); |
| 22 | pthread_unpause_all(); |
| 23 | pthread_pause(main_thread); |
| 24 | pthread_unpause(main_thread); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | int main() { |
| 29 | |
| 30 | main_thread = pthread_self(); |
| 31 | |
| 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 |
| 35 | pthread_extra_create(&a, NULL, thread_test, " A"); |
| 36 | pthread_extra_create(&b, NULL, thread_test, " B"); |
| 37 | //pthread_sleep(1); |
| 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 | */ |
| 53 | |
| 54 | while(1) { |
| 55 | pthread_pause(b); |
| 56 | pthread_unpause(a); |
| 57 | pthread_pause_disable(); |
| 58 | pthread_pause_disable(); |
| 59 | pthread_pause_disable(); |
| 60 | printf("SWITCH A:\n"); |
| 61 | pthread_pause_enable(); |
| 62 | pthread_pause_enable(); |
| 63 | pthread_pause_enable(); |
| 64 | pthread_sleep(2); |
| 65 | |
| 66 | pthread_pause_all(); |
| 67 | printf("SWITCH B:\n"); |
| 68 | pthread_unpause_all(); |
| 69 | pthread_pause(a); |
| 70 | pthread_unpause(b); |
| 71 | pthread_sleep(2); |
| 72 | |
| 73 | //pthread_pause_disable(); |
| 74 | pthread_pause_all(); |
| 75 | printf("SWITCH A+B:\n"); |
| 76 | pthread_unpause_all(); |
| 77 | //pthread_pause_enable(); |
| 78 | pthread_unpause(a); |
| 79 | pthread_unpause(b); |
| 80 | pthread_sleep(1); |
| 81 | |
| 82 | //pthread_pause_disable(); |
| 83 | pthread_pause_all(); |
| 84 | printf("SWITCH MAIN ONLY:\n"); |
| 85 | //pthread_pause_enable(); |
| 86 | pthread_pause_all(); |
| 87 | //printf("\n"); |
| 88 | pthread_sleep(1); |
| 89 | printf("SWITCH MAIN A+B:\n"); |
| 90 | pthread_unpause_all(); |
| 91 | //printf("\n"); |
| 92 | pthread_sleep(1); |
| 93 | } |
| 94 | |
| 95 | pthread_join(a, NULL); |
| 96 | pthread_join(b, NULL); |
| 97 | printf("DIEDED!\n"); |
| 98 | } |