Commit | Line | Data |
---|---|---|
88342701 TM |
1 | #define __PTHREAD_EXTRA_INTERNAL |
2 | ||
a8e71e8f TM |
3 | #include <pthread.h> |
4 | #include <pthread_extra.h> | |
5 | #include <signal.h> | |
88342701 TM |
6 | #include <errno.h> |
7 | #include <unistd.h> | |
8 | #include <sys/resource.h> | |
9 | //#include <stdio.h> | |
7e0f3dba TM |
10 | #include <stdlib.h> |
11 | #include <assert.h> | |
88342701 | 12 | //#include <sys/time.h> |
a8e71e8f TM |
13 | |
14 | void pthread_pause_handler() { | |
88342701 TM |
15 | //Do nothing when there are more signals pending (to cleanup the queue) |
16 | sigset_t pending; | |
17 | sigpending(&pending); | |
18 | if(sigismember(&pending, PTHREAD_XSIG_STOP)) return; | |
19 | ||
20 | //Keep waiting for signals until we are supposed to be running | |
a8e71e8f TM |
21 | sigset_t sigset; |
22 | sigfillset(&sigset); | |
88342701 | 23 | sigdelset(&sigset, PTHREAD_XSIG_STOP); |
24286ba9 | 24 | if(!pthread_user_data_internal(pthread_self())->running) { |
88342701 TM |
25 | sigsuspend(&sigset); |
26 | } | |
a8e71e8f TM |
27 | } |
28 | ||
29 | void pthread_pause_enable() { | |
ee099ba7 TM |
30 | //Add thread to internal registry |
31 | pthread_user_data_internal(pthread_self()); | |
32 | ||
88342701 TM |
33 | //Nesting signals too deep is not good for stack |
34 | //You can get runtime stats using following command: | |
35 | //grep -i sig /proc/$(pgrep binary)/status | |
36 | struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; | |
37 | setrlimit(RLIMIT_SIGPENDING, &sigq); | |
38 | ||
39 | //Setup signal handler | |
a8e71e8f | 40 | signal(PTHREAD_XSIG_STOP, pthread_pause_handler); |
88342701 TM |
41 | |
42 | //Unblock signal | |
43 | sigset_t sigset; | |
44 | sigemptyset(&sigset); | |
45 | sigaddset(&sigset, PTHREAD_XSIG_STOP); | |
46 | pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); | |
47 | } | |
48 | ||
49 | void pthread_pause_disable() { | |
ee099ba7 TM |
50 | //Add thread to internal registry |
51 | pthread_user_data_internal(pthread_self()); | |
52 | ||
88342701 TM |
53 | //Block signal |
54 | sigset_t sigset; | |
55 | sigemptyset(&sigset); | |
56 | sigaddset(&sigset, PTHREAD_XSIG_STOP); | |
57 | pthread_sigmask(SIG_BLOCK, &sigset, NULL); | |
58 | } | |
59 | ||
7e0f3dba TM |
60 | int pthread_pause_reschedule(pthread_t thread) { |
61 | //Send signal to initiate pause handler | |
86166123 TM |
62 | //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); |
63 | while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, (const union sigval){.sival_ptr=NULL}) == EAGAIN) usleep(1000); | |
7e0f3dba TM |
64 | return 0; |
65 | } | |
66 | ||
8df6da88 TM |
67 | int pthread_extra_yield() { |
68 | //Yield to both schedulers | |
69 | pthread_pause_reschedule(pthread_self()); | |
70 | return pthread_yield(); | |
71 | } | |
72 | ||
88342701 TM |
73 | int pthread_pause(pthread_t thread) { |
74 | //Set thread as paused and notify it via signal (wait when queue full) | |
75 | pthread_user_data_internal(thread)->running = 0; | |
7e0f3dba | 76 | pthread_pause_reschedule(thread); |
88342701 TM |
77 | return 0; |
78 | } | |
79 | ||
80 | int pthread_unpause(pthread_t thread) { | |
81 | //Set thread as running and notify it via signal (wait when queue full) | |
82 | pthread_user_data_internal(thread)->running = 1; | |
7e0f3dba | 83 | pthread_pause_reschedule(thread); |
88342701 | 84 | return 0; |
a8e71e8f | 85 | } |
7e0f3dba TM |
86 | |
87 | ||
88 | ||
89 | // Wrappers /////////////////////////////////////////////////////////// | |
90 | ||
91 | ||
92 | typedef struct pthread_extra_wrapper_t { | |
93 | void *(*start_routine)(void *); | |
94 | void *restrict arg; | |
95 | } pthread_extra_wrapper_t; | |
96 | ||
97 | void *pthread_extra_thread_wrapper(void *arg) { | |
98 | pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg); | |
99 | free(arg); | |
100 | ||
101 | //Register new thread to user data structure | |
102 | pthread_user_data_internal(pthread_self()); | |
103 | ||
104 | //TODO: user_data should do this automaticaly? | |
105 | pthread_cleanup_push(pthread_user_data_cleanup, (void *)pthread_self()); | |
106 | ||
107 | //Check if we should be running according to pthread_pause sub-scheduler | |
108 | pthread_pause_reschedule(pthread_self()); | |
109 | ||
110 | return task.start_routine(task.arg); | |
111 | ||
112 | pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro | |
113 | } | |
114 | ||
115 | int pthread_extra_create(pthread_t *restrict thread, | |
116 | const pthread_attr_t *restrict attr, | |
117 | void *(*start_routine)(void *), | |
118 | void *restrict arg) { | |
119 | ||
120 | pthread_extra_wrapper_t *task = malloc(sizeof(pthread_extra_wrapper_t)); | |
121 | assert(task != NULL); | |
122 | task->start_routine=start_routine; | |
123 | task->arg=arg; | |
124 | return pthread_create(thread, attr, pthread_extra_thread_wrapper, task); | |
125 | } |