1eacdf498d049aa2f50e73219f0042375612b11d
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 if(!pthread_user_data_internal(pthread_self())->running
) {
27 void pthread_pause_enable() {
28 //Add thread to internal registry
29 pthread_user_data_internal(pthread_self());
31 //Nesting signals too deep is not good for stack
32 //You can get runtime stats using following command:
33 //grep -i sig /proc/$(pgrep binary)/status
34 struct rlimit sigq
= {.rlim_cur
= 32, .rlim_max
=32};
35 setrlimit(RLIMIT_SIGPENDING
, &sigq
);
37 //Setup signal handler
38 signal(PTHREAD_XSIG_STOP
, pthread_pause_handler
);
43 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
44 pthread_sigmask(SIG_UNBLOCK
, &sigset
, NULL
);
47 void pthread_pause_disable() {
48 //Add thread to internal registry
49 pthread_user_data_internal(pthread_self());
54 sigaddset(&sigset
, PTHREAD_XSIG_STOP
);
55 pthread_sigmask(SIG_BLOCK
, &sigset
, NULL
);
58 int pthread_pause(pthread_t thread
) {
59 //Set thread as paused and notify it via signal (wait when queue full)
60 pthread_user_data_internal(thread
)->running
= 0;
61 while(pthread_kill(thread
, PTHREAD_XSIG_STOP
) == EAGAIN
) usleep(1000);
65 int pthread_unpause(pthread_t thread
) {
66 //Set thread as running and notify it via signal (wait when queue full)
67 pthread_user_data_internal(thread
)->running
= 1;
68 while(pthread_kill(thread
, PTHREAD_XSIG_STOP
) == EAGAIN
) usleep(1000);
This page took 0.25761 seconds and 3 git commands to generate.