1 #define __PTHREAD_EXTRA_INTERNAL
4 #include <pthread_extra.h>
8 #include <sys/resource.h>
12 //#include <sys/time.h>
14 void pthread_pause_handler() {
15 //Do nothing when there are more signals pending (to cleanup the queue)
18 if(sigismember(&pending
, PTHREAD_XSIG_STOP
)) return;
20 //Keep waiting for signals until we are supposed to be running
23 sigdelset(&sigset
, PTHREAD_XSIG_STOP
);
24 if(!pthread_user_data_internal(pthread_self())->running
) {
29 void pthread_pause_enable() {
30 //Add thread to internal registry
31 pthread_user_data_internal(pthread_self());
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
);
39 //Setup signal handler
40 signal(PTHREAD_XSIG_STOP
, pthread_pause_handler
);
45 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
46 pthread_sigmask(SIG_UNBLOCK
, &sigset
, NULL
);
49 void pthread_pause_disable() {
50 //Add thread to internal registry
51 pthread_user_data_internal(pthread_self());
56 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
57 pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
60 int pthread_pause_reschedule(pthread_t thread
) {
61 //Send signal to initiate pause handler
62 while(pthread_kill(thread
, PTHREAD_XSIG_STOP
) == EAGAIN
) usleep(1000);
66 int pthread_extra_yield() {
67 //Yield to both schedulers
68 pthread_pause_reschedule(pthread_self());
69 return pthread_yield();
72 int pthread_pause(pthread_t thread
) {
73 //Set thread as paused and notify it via signal (wait when queue full)
74 pthread_user_data_internal(thread
)->running
= 0;
75 pthread_pause_reschedule(thread
);
79 int pthread_unpause(pthread_t thread
) {
80 //Set thread as running and notify it via signal (wait when queue full)
81 pthread_user_data_internal(thread
)->running
= 1;
82 pthread_pause_reschedule(thread
);
88 // Wrappers ///////////////////////////////////////////////////////////
91 typedef struct pthread_extra_wrapper_t
{
92 void *(*start_routine
)(void *);
94 } pthread_extra_wrapper_t
;
96 void *pthread_extra_thread_wrapper(void *arg
) {
97 pthread_extra_wrapper_t task
= *((pthread_extra_wrapper_t
*)arg
);
100 //Register new thread to user data structure
101 pthread_user_data_internal(pthread_self());
103 //TODO: user_data should do this automaticaly?
104 pthread_cleanup_push(pthread_user_data_cleanup
, (void *)pthread_self());
106 //Check if we should be running according to pthread_pause sub-scheduler
107 pthread_pause_reschedule(pthread_self());
109 return task
.start_routine(task
.arg
);
111 pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro
114 int pthread_extra_create(pthread_t
*restrict thread
,
115 const pthread_attr_t
*restrict attr
,
116 void *(*start_routine
)(void *),
117 void *restrict arg
) {
119 pthread_extra_wrapper_t
*task
= malloc(sizeof(pthread_extra_wrapper_t
));
120 assert(task
!= NULL
);
121 task
->start_routine
=start_routine
;
123 return pthread_create(thread
, attr
, pthread_extra_thread_wrapper
, task
);