662d78fdd99a949e041a15c093f862c01d1f5a5d
1 #define __PTHREAD_EXTRA_INTERNAL
4 #include <pthread_extra.h>
8 #include <sys/resource.h>
10 //#include <sys/time.h>
12 void pthread_pause_handler() {
13 //Do nothing when there are more signals pending (to cleanup the queue)
16 if(sigismember(&pending
, PTHREAD_XSIG_STOP
)) return;
18 //Keep waiting for signals until we are supposed to be running
21 sigdelset(&sigset
, PTHREAD_XSIG_STOP
);
22 while(!pthread_user_data_internal(pthread_self())->running
) {
27 void pthread_pause_enable() {
28 //Nesting signals too deep is not good for stack
29 //You can get runtime stats using following command:
30 //grep -i sig /proc/$(pgrep binary)/status
31 struct rlimit sigq
= {.rlim_cur
= 32, .rlim_max
=32};
32 setrlimit(RLIMIT_SIGPENDING
, &sigq
);
34 //Setup signal handler
35 signal(PTHREAD_XSIG_STOP
, pthread_pause_handler
);
40 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
41 pthread_sigmask(SIG_UNBLOCK
, &sigset
, NULL
);
44 void pthread_pause_disable() {
48 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
49 pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
52 int pthread_pause(pthread_t thread
) {
53 //Set thread as paused and notify it via signal (wait when queue full)
54 pthread_user_data_internal(thread
)->running
= 0;
55 while(pthread_kill(thread
, PTHREAD_XSIG_STOP
) == EAGAIN
) usleep(1000);
59 int pthread_unpause(pthread_t thread
) {
60 //Set thread as running and notify it via signal (wait when queue full)
61 pthread_user_data_internal(thread
)->running
= 1;
62 while(pthread_kill(thread
, PTHREAD_XSIG_STOP
) == EAGAIN
) usleep(1000);
This page took 0.297028 seconds and 3 git commands to generate.