1 #define __PTHREAD_EXTRA_INTERNAL
4 #include <pthread_extra.h>
8 #include <sys/resource.h>
9 //#include <sys/siginfo.h>
13 //#include <sys/time.h>
15 void pthread_pause_handler(const int signal
, siginfo_t
*info
, void *ptr
) {
16 (void)signal
; (void)info
; (void)ptr
;
17 pthread_user_data_internal_t
*td
= (pthread_user_data_internal_t
*)(info
->si_value
.sival_ptr
);
20 //Do nothing when there are more signals pending (to cleanup the queue)
23 if(sigismember(&pending
, PTHREAD_XSIG_STOP
)) return;
25 //Keep waiting for signals until we are supposed to be running
28 sigdelset(&sigset
, PTHREAD_XSIG_STOP
);
30 //printf("RCV: %p = %p\n", (void *)pthread_user_data_internal(pthread_self()), (void *)td);
32 //if(!pthread_user_data_internal(pthread_self())->running) {
38 void pthread_pause_enable() {
39 //Add thread to internal registry
40 //pthread_user_data_internal(pthread_self());
42 //Nesting signals too deep is not good for stack
43 //You can get runtime stats using following command:
44 //grep -i sig /proc/$(pgrep binary)/status
45 struct rlimit sigq
= {.rlim_cur
= 32, .rlim_max
=32};
46 setrlimit(RLIMIT_SIGPENDING
, &sigq
);
51 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
53 //Setup signal handler
54 //signal(PTHREAD_XSIG_STOP, pthread_pause_handler);
55 const struct sigaction pause_sa
= {
56 .sa_sigaction
= pthread_pause_handler
,
58 .sa_flags
= SA_SIGINFO
,
61 sigaction(PTHREAD_XSIG_STOP
, &pause_sa
, NULL
);
64 pthread_sigmask(SIG_UNBLOCK
, &sigset
, NULL
);
67 void pthread_pause_disable() {
68 //Add thread to internal registry
69 //pthread_user_data_internal(pthread_self());
74 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
75 pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
78 int pthread_pause_reschedule(pthread_t thread
) {
79 //Send signal to initiate pause handler
80 //printf("SND: %p\n", (void *)pthread_user_data_internal(thread));
81 //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
82 while(pthread_sigqueue(thread
, PTHREAD_XSIG_STOP
,
83 (const union sigval
){.sival_ptr
=pthread_user_data_internal(thread
)}
84 ) == EAGAIN
) usleep(1000);
88 int pthread_extra_yield() {
89 //Yield to both schedulers
90 pthread_pause_reschedule(pthread_self());
91 return pthread_yield();
94 int pthread_pause(pthread_t thread
) {
95 //Set thread as paused and notify it via signal (wait when queue full)
96 pthread_user_data_internal(thread
)->running
= 0;
97 pthread_pause_reschedule(thread
);
101 int pthread_unpause(pthread_t thread
) {
102 //Set thread as running and notify it via signal (wait when queue full)
103 pthread_user_data_internal(thread
)->running
= 1;
104 pthread_pause_reschedule(thread
);
110 // Wrappers ///////////////////////////////////////////////////////////
113 typedef struct pthread_extra_wrapper_t
{
114 void *(*start_routine
)(void *);
116 } pthread_extra_wrapper_t
;
118 void *pthread_extra_thread_wrapper(void *arg
) {
119 pthread_extra_wrapper_t task
= *((pthread_extra_wrapper_t
*)arg
);
122 //Register new thread to user data structure
123 pthread_user_data_internal(pthread_self());
125 //TODO: user_data should do this automaticaly?
126 pthread_cleanup_push(pthread_user_data_cleanup
, (void *)pthread_self());
128 //Check if we should be running according to pthread_pause sub-scheduler
129 pthread_pause_reschedule(pthread_self());
131 return task
.start_routine(task
.arg
);
133 pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro
136 int pthread_extra_create(pthread_t
*restrict thread
,
137 const pthread_attr_t
*restrict attr
,
138 void *(*start_routine
)(void *),
139 void *restrict arg
) {
141 pthread_extra_wrapper_t
*task
= malloc(sizeof(pthread_extra_wrapper_t
));
142 assert(task
!= NULL
);
143 task
->start_routine
=start_routine
;
145 return pthread_create(thread
, attr
, pthread_extra_thread_wrapper
, task
);