1 #define __PTHREAD_EXTRA_INTERNAL
4 #include <pthread_extra.h>
9 #include <sys/resource.h>
10 //#include <sys/siginfo.h>
14 //#include <sys/time.h>
16 //Mutex that ensures proper serialization of (un)pause calls
17 //pthread_mutex_t pthread_pause_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
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);
26 void pthread_pause_init() { pthread_once(&pthread_pause_once_ctrl
, &pthread_pause_once
); }
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
;
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
;
37 //Post semaphore to confirm that signal is handled
38 sem_post(&pthread_pause_sem
);
40 //Keep waiting for signals until we are supposed to be running
44 sigdelset(&sigset
, PTHREAD_XSIG_STOP
);
49 void pthread_pause_enable() {
50 pthread_pause_init(); //Make sure semaphore is init'd
52 //Nesting signals too deep is not good for stack
53 //You can get runtime stats using following command:
54 //grep -i sig /proc/$(pgrep binary)/status
55 //struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32};
56 //setrlimit(RLIMIT_SIGPENDING, &sigq);
61 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
63 //Setup signal handler
64 //signal(PTHREAD_XSIG_STOP, pthread_pause_handler);
65 const struct sigaction pause_sa
= {
66 .sa_sigaction
= pthread_pause_handler
,
68 .sa_flags
= SA_SIGINFO
| SA_RESTART
,
72 sigaction(PTHREAD_XSIG_STOP
, &pause_sa
, NULL
);
75 pthread_sigmask(SIG_UNBLOCK
, &sigset
, NULL
);
77 //Add thread to internal registry
78 pthread_user_data_internal(pthread_self()); //Only now, when signals are unblocked!
81 void pthread_pause_disable() {
82 //pthread_user_data_lock();
83 pthread_pause_init(); //Make sure semaphore is init'd
85 //Add thread to internal registry
86 //pthread_user_data_internal(pthread_self()); //DEADLOCKS!
91 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
93 //Make sure all signals are dispatched before we block them
94 //Maybe not a good idea, causes DEADLOCKS!
95 //sem_wait(&pthread_pause_sem);
96 pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
97 //sem_post(&pthread_pause_sem);
99 //pthread_user_data_unlock();
103 int pthread_pause_reschedule(pthread_t thread) {
104 //Send signal to initiate pause handler
105 //printf("SND: %p\n", (void *)pthread_user_data_internal(thread));
106 //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
107 while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP,
108 (const union sigval){.sival_ptr=pthread_user_data_internal(thread)}
109 ) == EAGAIN) usleep(1000);
114 int pthread_pause_reschedule(pthread_t thread
) {
115 //Decide if the thread should run and signal it
116 pthread_user_data_lock();
118 //Wait for semaphore which means signal queue is empty
119 pthread_pause_init(); //Make sure semaphore is init'd
120 sem_wait(&pthread_pause_sem
);
122 //Only call this if you already acquired pthread_pause_sem semaphore!!!!
123 //Otherwise call pthread_pause_reschedule()
125 //Check if thread has running flag
126 int run
= (pthread_user_data_internal(thread
)->running
);
127 //Check if privileged (single thread) mode is active
128 if(!pthread_equal(pthread_pause_holder
, PTHREAD_XNULL
) && !pthread_equal(pthread_pause_holder
, thread
)) {
132 //Send signal to initiate pause handler (keep trying while SigQueue is full)
133 //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
134 //printf("Sched %lu = %d (self: %lu, lck: %lu)\n", thread, run, pthread_self(), pthread_pause_holder);
135 while(pthread_sigqueue(thread
, PTHREAD_XSIG_STOP
,
136 (const union sigval
){.sival_int
=run
}
137 ) == EAGAIN
) usleep(1000);
139 //Wait for signal to be delivered
140 sem_wait(&pthread_pause_sem
);
141 sem_post(&pthread_pause_sem
);
142 pthread_user_data_unlock();
147 int pthread_extra_yield() {
148 //Yield to both schedulers
149 pthread_pause_reschedule(pthread_self());
150 return pthread_yield();
153 ///Sanity check to be sure there are no race conditions
154 inline void pthread_pause_assert_owner() {
155 if(!pthread_equal(pthread_pause_holder
,PTHREAD_XNULL
))
156 assert(pthread_equal(pthread_pause_holder
, pthread_self()));
159 ///Pause specified thread (block until it is paused)
160 int pthread_pause(pthread_t thread
) {
161 pthread_user_data_lock();
162 pthread_pause_assert_owner();
163 //if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self()));
164 //Set thread as paused and notify it via signal (wait when queue full)
165 pthread_user_data_internal(thread
)->running
= 0;
166 pthread_pause_reschedule(thread
);
167 pthread_user_data_unlock();
171 ///UnPause specified thread (block until it is unpaused)
172 int pthread_unpause(pthread_t thread
) {
173 pthread_user_data_lock();
174 pthread_pause_assert_owner();
175 //Set thread as running and notify it via signal (wait when queue full)
176 pthread_user_data_internal(thread
)->running
= 1;
177 pthread_pause_reschedule(thread
);
178 pthread_user_data_unlock();
182 ///Enter exclusive mode by pausing everyone else
183 int pthread_pause_all() {
184 //printf("Pause ALL\n");
185 pthread_user_data_lock();
186 //printf("Pause ALL+\n");
187 //printf("Pause %p == %p\n", (void *)pthread_pause_holder, (void *)pthread_self());
188 pthread_pause_assert_owner();
189 pthread_pause_holder
= pthread_self();
190 pthread_user_data_internal_iterate(&pthread_pause_reschedule
, NULL
);
191 //printf("Pause ALL!\n");
192 pthread_user_data_unlock();
196 ///Leave exclusive mode by unpausing everyone else
197 int pthread_unpause_all() {
198 //printf("UnPause ALL\n");
199 pthread_user_data_lock();
200 //printf("UnPause ALL+\n");
201 pthread_pause_assert_owner();
202 pthread_pause_holder
= PTHREAD_XNULL
;
203 pthread_user_data_internal_iterate(&pthread_pause_reschedule
, NULL
);
204 //printf("UnPause ALL!\n");
205 pthread_user_data_unlock();
210 // Wrappers ///////////////////////////////////////////////////////////
213 typedef struct pthread_extra_wrapper_t
{
214 void *(*start_routine
)(void *);
216 } pthread_extra_wrapper_t
;
218 void *pthread_extra_thread_wrapper(void *arg
) {
219 pthread_extra_wrapper_t task
= *((pthread_extra_wrapper_t
*)arg
);
222 pthread_pause_enable();
224 //Register new thread to user data structure
225 //pthread_user_data_internal(pthread_self()); //Perhaps already done in pthread_extra_yield() and pthread_pause_enable()??
227 //TODO: user_data should do this automaticaly?
228 pthread_cleanup_push(pthread_user_data_cleanup
, (void *)pthread_self());
230 //Check if we should be running according to pthread_pause sub-scheduler
231 //pthread_pause_reschedule(pthread_self());
232 pthread_extra_yield();
235 return task
.start_routine(task
.arg
);
237 pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro
240 int pthread_extra_create(pthread_t
*restrict thread
,
241 const pthread_attr_t
*restrict attr
,
242 void *(*start_routine
)(void *),
243 void *restrict arg
) {
245 pthread_extra_wrapper_t
*task
= malloc(sizeof(pthread_extra_wrapper_t
));
246 assert(task
!= NULL
);
247 task
->start_routine
=start_routine
;
249 return pthread_create(thread
, attr
, pthread_extra_thread_wrapper
, task
);