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> | |
dec91d37 | 9 | //#include <sys/siginfo.h> |
88342701 | 10 | //#include <stdio.h> |
7e0f3dba TM |
11 | #include <stdlib.h> |
12 | #include <assert.h> | |
88342701 | 13 | //#include <sys/time.h> |
a8e71e8f | 14 | |
70bd1c94 TM |
15 | ///When this variable is nonzero, only referenced thread is allowed to run |
16 | ///Access has to be protected by pthread_user_data_lock() | |
9b4be8e8 | 17 | pthread_t pthread_pause_holder = PTHREAD_XNULL; |
70bd1c94 | 18 | |
dec91d37 TM |
19 | void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) { |
20 | (void)signal; (void)info; (void)ptr; | |
70bd1c94 TM |
21 | int run = info->si_value.sival_int; |
22 | //(void)td; | |
dec91d37 | 23 | |
88342701 TM |
24 | //Do nothing when there are more signals pending (to cleanup the queue) |
25 | sigset_t pending; | |
26 | sigpending(&pending); | |
27 | if(sigismember(&pending, PTHREAD_XSIG_STOP)) return; | |
28 | ||
29 | //Keep waiting for signals until we are supposed to be running | |
a8e71e8f TM |
30 | sigset_t sigset; |
31 | sigfillset(&sigset); | |
88342701 | 32 | sigdelset(&sigset, PTHREAD_XSIG_STOP); |
dec91d37 TM |
33 | |
34 | //printf("RCV: %p = %p\n", (void *)pthread_user_data_internal(pthread_self()), (void *)td); | |
35 | ||
36 | //if(!pthread_user_data_internal(pthread_self())->running) { | |
70bd1c94 | 37 | if(!run) { |
88342701 TM |
38 | sigsuspend(&sigset); |
39 | } | |
a8e71e8f TM |
40 | } |
41 | ||
42 | void pthread_pause_enable() { | |
ee099ba7 | 43 | //Add thread to internal registry |
238a7032 | 44 | //pthread_user_data_internal(pthread_self()); |
ee099ba7 | 45 | |
88342701 TM |
46 | //Nesting signals too deep is not good for stack |
47 | //You can get runtime stats using following command: | |
48 | //grep -i sig /proc/$(pgrep binary)/status | |
49 | struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; | |
50 | setrlimit(RLIMIT_SIGPENDING, &sigq); | |
51 | ||
dec91d37 | 52 | //Prepare signal mask |
88342701 TM |
53 | sigset_t sigset; |
54 | sigemptyset(&sigset); | |
55 | sigaddset(&sigset, PTHREAD_XSIG_STOP); | |
dec91d37 TM |
56 | |
57 | //Setup signal handler | |
58 | //signal(PTHREAD_XSIG_STOP, pthread_pause_handler); | |
59 | const struct sigaction pause_sa = { | |
60 | .sa_sigaction = pthread_pause_handler, | |
61 | .sa_mask = sigset, | |
62 | .sa_flags = SA_SIGINFO, | |
63 | .sa_restorer = NULL | |
64 | }; | |
65 | sigaction(PTHREAD_XSIG_STOP, &pause_sa, NULL); | |
66 | ||
67 | //Unblock signal | |
88342701 TM |
68 | pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); |
69 | } | |
70 | ||
71 | void pthread_pause_disable() { | |
ee099ba7 | 72 | //Add thread to internal registry |
238a7032 | 73 | //pthread_user_data_internal(pthread_self()); |
ee099ba7 | 74 | |
88342701 TM |
75 | //Block signal |
76 | sigset_t sigset; | |
77 | sigemptyset(&sigset); | |
78 | sigaddset(&sigset, PTHREAD_XSIG_STOP); | |
79 | pthread_sigmask(SIG_BLOCK, &sigset, NULL); | |
80 | } | |
81 | ||
70bd1c94 | 82 | /* |
7e0f3dba TM |
83 | int pthread_pause_reschedule(pthread_t thread) { |
84 | //Send signal to initiate pause handler | |
dec91d37 | 85 | //printf("SND: %p\n", (void *)pthread_user_data_internal(thread)); |
86166123 | 86 | //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); |
dec91d37 TM |
87 | while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, |
88 | (const union sigval){.sival_ptr=pthread_user_data_internal(thread)} | |
89 | ) == EAGAIN) usleep(1000); | |
7e0f3dba TM |
90 | return 0; |
91 | } | |
70bd1c94 TM |
92 | */ |
93 | ||
94 | int pthread_pause_reschedule(pthread_t thread) { | |
95 | //Decide if the thread should run | |
96 | ||
97 | pthread_user_data_lock(); | |
98 | //Check if thread has running flag | |
99 | int run = (pthread_user_data_internal(thread)->running); | |
100 | //Check if privileged (single thread) mode is active | |
9b4be8e8 | 101 | if((pthread_pause_holder != PTHREAD_XNULL) && !pthread_equal(pthread_pause_holder, thread)) { |
70bd1c94 TM |
102 | run = 0; |
103 | } | |
104 | pthread_user_data_unlock(); | |
105 | ||
106 | //Send signal to initiate pause handler (keep trying while SigQueue is full) | |
107 | //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); | |
108 | while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, | |
109 | (const union sigval){.sival_int=run} | |
110 | ) == EAGAIN) usleep(1000); | |
111 | return 0; | |
112 | } | |
7e0f3dba | 113 | |
8df6da88 TM |
114 | int pthread_extra_yield() { |
115 | //Yield to both schedulers | |
116 | pthread_pause_reschedule(pthread_self()); | |
117 | return pthread_yield(); | |
118 | } | |
119 | ||
88342701 TM |
120 | int pthread_pause(pthread_t thread) { |
121 | //Set thread as paused and notify it via signal (wait when queue full) | |
70bd1c94 | 122 | pthread_user_data_lock(); |
88342701 | 123 | pthread_user_data_internal(thread)->running = 0; |
70bd1c94 | 124 | pthread_user_data_unlock(); |
7e0f3dba | 125 | pthread_pause_reschedule(thread); |
88342701 TM |
126 | return 0; |
127 | } | |
128 | ||
129 | int pthread_unpause(pthread_t thread) { | |
130 | //Set thread as running and notify it via signal (wait when queue full) | |
70bd1c94 | 131 | pthread_user_data_lock(); |
88342701 | 132 | pthread_user_data_internal(thread)->running = 1; |
70bd1c94 | 133 | pthread_user_data_unlock(); |
7e0f3dba | 134 | pthread_pause_reschedule(thread); |
88342701 | 135 | return 0; |
a8e71e8f | 136 | } |
7e0f3dba | 137 | |
70bd1c94 TM |
138 | int pthread_pause_all() { |
139 | pthread_user_data_lock(); | |
9b4be8e8 | 140 | if(pthread_pause_holder!=PTHREAD_XNULL) assert(pthread_equal(pthread_pause_holder, pthread_self())); |
70bd1c94 TM |
141 | pthread_pause_holder = pthread_self(); |
142 | pthread_user_data_unlock(); | |
9b4be8e8 | 143 | pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); |
70bd1c94 TM |
144 | return 0; |
145 | } | |
146 | ||
147 | int pthread_unpause_all() { | |
148 | pthread_user_data_lock(); | |
9b4be8e8 TM |
149 | if(pthread_pause_holder!=PTHREAD_XNULL) assert(pthread_equal(pthread_pause_holder, pthread_self())); |
150 | pthread_pause_holder = PTHREAD_XNULL; | |
70bd1c94 | 151 | pthread_user_data_unlock(); |
9b4be8e8 | 152 | pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); |
70bd1c94 TM |
153 | return 0; |
154 | } | |
7e0f3dba TM |
155 | |
156 | ||
157 | // Wrappers /////////////////////////////////////////////////////////// | |
158 | ||
159 | ||
160 | typedef struct pthread_extra_wrapper_t { | |
161 | void *(*start_routine)(void *); | |
162 | void *restrict arg; | |
163 | } pthread_extra_wrapper_t; | |
164 | ||
165 | void *pthread_extra_thread_wrapper(void *arg) { | |
166 | pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg); | |
167 | free(arg); | |
168 | ||
169 | //Register new thread to user data structure | |
170 | pthread_user_data_internal(pthread_self()); | |
171 | ||
172 | //TODO: user_data should do this automaticaly? | |
173 | pthread_cleanup_push(pthread_user_data_cleanup, (void *)pthread_self()); | |
174 | ||
175 | //Check if we should be running according to pthread_pause sub-scheduler | |
70bd1c94 TM |
176 | //pthread_pause_reschedule(pthread_self()); |
177 | pthread_extra_yield(); | |
7e0f3dba | 178 | |
70bd1c94 | 179 | //Run actual task |
7e0f3dba TM |
180 | return task.start_routine(task.arg); |
181 | ||
182 | pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro | |
183 | } | |
184 | ||
185 | int pthread_extra_create(pthread_t *restrict thread, | |
186 | const pthread_attr_t *restrict attr, | |
187 | void *(*start_routine)(void *), | |
188 | void *restrict arg) { | |
189 | ||
190 | pthread_extra_wrapper_t *task = malloc(sizeof(pthread_extra_wrapper_t)); | |
191 | assert(task != NULL); | |
192 | task->start_routine=start_routine; | |
193 | task->arg=arg; | |
194 | return pthread_create(thread, attr, pthread_extra_thread_wrapper, task); | |
195 | } |