Exclusive mode seems to work without deadlocks
[mirrors/Programs.git] / c / pthread_extra / pthread_pause.c
1 #define __PTHREAD_EXTRA_INTERNAL
2
3 #include <pthread.h>
4 #include <pthread_extra.h>
5 #include <signal.h>
6 #include <semaphore.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <sys/resource.h>
10 //#include <sys/siginfo.h>
11 //#include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14 //#include <sys/time.h>
15
16 //Mutex that ensures proper serialization of (un)pause calls
17 //pthread_mutex_t pthread_pause_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
18
19 //Semaphore that ensures proper serialization of (un)pause signals
20 sem_t pthread_pause_sem;
21 //Once control to init the semaphore (and possibly other stuff)
22 pthread_once_t pthread_pause_once_ctrl = PTHREAD_ONCE_INIT;
23 void pthread_pause_once(void) {
24 sem_init(&pthread_pause_sem, 0, 1);
25 }
26 void pthread_pause_init() { pthread_once(&pthread_pause_once_ctrl, &pthread_pause_once); }
27
28 ///When this variable is nonzero, only referenced thread is allowed to run
29 ///Access has to be protected by pthread_user_data_lock() and pthread_pause_sem;
30 pthread_t pthread_pause_holder = PTHREAD_XNULL;
31
32 void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) {
33 (void)signal; (void)info; (void)ptr;
34 int run = info->si_value.sival_int;
35 //(void)td;
36
37 //Post semaphore to confirm that signal is handled
38 sem_post(&pthread_pause_sem);
39
40 //Keep waiting for signals until we are supposed to be running
41 if(!run) {
42 sigset_t sigset;
43 sigfillset(&sigset);
44 sigdelset(&sigset, PTHREAD_XSIG_STOP);
45 sigsuspend(&sigset);
46 }
47 }
48
49 void pthread_pause_enable() {
50 pthread_pause_init(); //Make sure semaphore is init'd
51
52 //Add thread to internal registry
53 pthread_user_data_internal(pthread_self());
54
55 //Nesting signals too deep is not good for stack
56 //You can get runtime stats using following command:
57 //grep -i sig /proc/$(pgrep binary)/status
58 //struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32};
59 //setrlimit(RLIMIT_SIGPENDING, &sigq);
60
61 //Prepare signal mask
62 sigset_t sigset;
63 sigemptyset(&sigset);
64 sigaddset(&sigset, PTHREAD_XSIG_STOP);
65
66 //Setup signal handler
67 //signal(PTHREAD_XSIG_STOP, pthread_pause_handler);
68 const struct sigaction pause_sa = {
69 .sa_sigaction = pthread_pause_handler,
70 .sa_mask = sigset,
71 .sa_flags = SA_SIGINFO | SA_RESTART,
72 .sa_restorer = NULL
73 };
74 sigaction(PTHREAD_XSIG_STOP, &pause_sa, NULL);
75
76 //Unblock signal
77 pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
78 }
79
80 void pthread_pause_disable() {
81 pthread_pause_init(); //Make sure semaphore is init'd
82
83 //Add thread to internal registry
84 pthread_user_data_internal(pthread_self());
85
86 //Block signal
87 sigset_t sigset;
88 sigemptyset(&sigset);
89 sigaddset(&sigset, PTHREAD_XSIG_STOP);
90
91 //Make sure all signals are dispatched before we block them
92 sem_wait(&pthread_pause_sem);
93 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
94 sem_post(&pthread_pause_sem);
95 }
96
97 /*
98 int pthread_pause_reschedule(pthread_t thread) {
99 //Send signal to initiate pause handler
100 //printf("SND: %p\n", (void *)pthread_user_data_internal(thread));
101 //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
102 while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP,
103 (const union sigval){.sival_ptr=pthread_user_data_internal(thread)}
104 ) == EAGAIN) usleep(1000);
105 return 0;
106 }
107 */
108
109 int pthread_pause_reschedule(pthread_t thread) {
110 //Decide if the thread should run and signal it
111
112 pthread_user_data_lock();
113
114 //Wait for semaphore which means signal queue is empty
115 pthread_pause_init(); //Make sure semaphore is init'd
116 sem_wait(&pthread_pause_sem);
117
118 //Only call this if you already acquired pthread_pause_sem semaphore!!!!
119 //Otherwise call pthread_pause_reschedule()
120
121 //Check if thread has running flag
122 int run = (pthread_user_data_internal(thread)->running);
123 //Check if privileged (single thread) mode is active
124 if(!pthread_equal(pthread_pause_holder, PTHREAD_XNULL) && !pthread_equal(pthread_pause_holder, thread)) {
125 run = 0;
126 }
127
128 //Send signal to initiate pause handler (keep trying while SigQueue is full)
129 //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
130 //printf("Sched %lu = %d (self: %lu, lck: %lu)\n", thread, run, pthread_self(), pthread_pause_holder);
131 while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP,
132 (const union sigval){.sival_int=run}
133 ) == EAGAIN) usleep(1000);
134
135 //Wait for signal to be delivered
136 sem_wait(&pthread_pause_sem);
137 sem_post(&pthread_pause_sem);
138 pthread_user_data_unlock();
139
140 return 0;
141 }
142
143 int pthread_extra_yield() {
144 //Yield to both schedulers
145 pthread_pause_reschedule(pthread_self());
146 return pthread_yield();
147 }
148
149 int pthread_pause(pthread_t thread) {
150 //Set thread as paused and notify it via signal (wait when queue full)
151 pthread_user_data_lock();
152 pthread_user_data_internal(thread)->running = 0;
153 pthread_pause_reschedule(thread);
154 pthread_user_data_unlock();
155 return 0;
156 }
157
158 int pthread_unpause(pthread_t thread) {
159 //Set thread as running and notify it via signal (wait when queue full)
160 pthread_user_data_lock();
161 pthread_user_data_internal(thread)->running = 1;
162 pthread_pause_reschedule(thread);
163 pthread_user_data_unlock();
164 return 0;
165 }
166
167 ///Enter exclusive mode by pausing everyone else
168 int pthread_pause_all() {
169 //printf("Pause ALL\n");
170 pthread_user_data_lock();
171 //printf("Pause ALL+\n");
172 //printf("Pause %p == %p\n", (void *)pthread_pause_holder, (void *)pthread_self());
173 if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self()));
174 pthread_pause_holder = pthread_self();
175 pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL);
176 //printf("Pause ALL!\n");
177 pthread_user_data_unlock();
178 return 0;
179 }
180
181 ///Leave exclusive mode by unpausing everyone else
182 int pthread_unpause_all() {
183 //printf("UnPause ALL\n");
184 pthread_user_data_lock();
185 //printf("UnPause ALL+\n");
186 if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self()));
187 pthread_pause_holder = PTHREAD_XNULL;
188 pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL);
189 //printf("UnPause ALL!\n");
190 pthread_user_data_unlock();
191 return 0;
192 }
193
194
195 // Wrappers ///////////////////////////////////////////////////////////
196
197
198 typedef struct pthread_extra_wrapper_t {
199 void *(*start_routine)(void *);
200 void *restrict arg;
201 } pthread_extra_wrapper_t;
202
203 void *pthread_extra_thread_wrapper(void *arg) {
204 pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg);
205 free(arg);
206
207 //Register new thread to user data structure
208 pthread_user_data_internal(pthread_self()); //Perhaps already done in pthread_extra_yield()??
209
210 //TODO: user_data should do this automaticaly?
211 pthread_cleanup_push(pthread_user_data_cleanup, (void *)pthread_self());
212
213 //Check if we should be running according to pthread_pause sub-scheduler
214 //pthread_pause_reschedule(pthread_self());
215 pthread_extra_yield();
216
217 //Run actual task
218 return task.start_routine(task.arg);
219
220 pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro
221 }
222
223 int pthread_extra_create(pthread_t *restrict thread,
224 const pthread_attr_t *restrict attr,
225 void *(*start_routine)(void *),
226 void *restrict arg) {
227
228 pthread_extra_wrapper_t *task = malloc(sizeof(pthread_extra_wrapper_t));
229 assert(task != NULL);
230 task->start_routine=start_routine;
231 task->arg=arg;
232 return pthread_create(thread, attr, pthread_extra_thread_wrapper, task);
233 }
This page took 0.336435 seconds and 4 git commands to generate.