From 883427014d5349442395c1059af5a1abade951ad Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Mon, 28 Jun 2021 17:44:20 +0200 Subject: [PATCH] Pthread pause based on pthread user data --- c/pthread_extra/Makefile | 6 +-- c/pthread_extra/README.md | 3 +- c/pthread_extra/pthread_extra.h | 29 +++++++++++--- c/pthread_extra/pthread_pause.c | 60 +++++++++++++++++++++++++---- c/pthread_extra/pthread_user_data.c | 43 +++++++++++++++++++++ 5 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 c/pthread_extra/pthread_user_data.c diff --git a/c/pthread_extra/Makefile b/c/pthread_extra/Makefile index cf4e674..0b96b41 100644 --- a/c/pthread_extra/Makefile +++ b/c/pthread_extra/Makefile @@ -1,4 +1,4 @@ -COPT=-O2 -flto -ftree-vectorize -ftree-slp-vectorize -fvisibility=hidden -D_FORTIFY_SOURCE=2 -DMG_ENABLE_FS=0 -fstack-protector-strong -fno-delete-null-pointer-checks $(CARCH) +COPT=-g -O2 -flto -ftree-vectorize -ftree-slp-vectorize -fvisibility=hidden -D_FORTIFY_SOURCE=2 -DMG_ENABLE_FS=0 -fstack-protector-strong -fno-delete-null-pointer-checks $(CARCH) #COPT=-fvisibility=hidden -DMG_ENABLE_FS=0 -fstack-protector-strong $(CARCH) CDEF=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE=1 -D_GNU_SOURCE CERR=-Wall -Wextra -Werror -pedantic -Werror=date-time -Wformat-security -Wduplicated-cond -Wfloat-equal -Wshadow -Wlogical-not-parentheses -Wnull-dereference @@ -11,14 +11,14 @@ CXXFLAGS+=$(COPT) $(CERR) $(CDEF) -std=c++11 $(CLIB) LDFLAGS+=$(COPT) $(LIB) BIN=test -OBJ=pthread_pause.o pthread_multi.o pthread_msgqueue.o test.o +OBJ=pthread_user_data.o pthread_pause.o pthread_multi.o pthread_msgqueue.o test.o all: $(BIN) $(BIN): $(OBJ) $(CXX) -o $(BIN) $(OBJ) $(LDFLAGS) strip --strip-unneeded $(BIN) - gcc -lpthread -I . test_pause.c pthread_pause.o -o test_pause + gcc -lpthread -I . test_pause.c pthread_user_data.o pthread_pause.o -o test_pause clean: rm -f $(BIN) $(OBJ) $(DEPS) diff --git a/c/pthread_extra/README.md b/c/pthread_extra/README.md index bf17f7e..0824f47 100644 --- a/c/pthread_extra/README.md +++ b/c/pthread_extra/README.md @@ -1,10 +1,11 @@ # pthread_extra Library implementing extra features on top of POSIX threads. -Currently only tested on Linux. Mostly experimental code. +Currently will probably only work on Linux. Mostly experimental code. ### Components * pthread_msgqueue - implements message queues, more features than POSIX mqueue (no known issues, but not really tested) + * pthread_user_data - allows user to store and retreive custom data using thread handle as a key (suboptimal, used internaly, not tested) * pthread_multi - lock multiple mutexes at once (might cause deadlocks in complex scenarios) * pthread_pause - implements suspend/resume functionality for pthreads (causes deadlocks under high load) diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index bd1baad..0652e0a 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -2,26 +2,45 @@ #define __PTHREAD_EXTRA_H__ #include -#include #include #include #include +//#include #define PTHREAD_XTIME_NOBLOCK (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) #define PTHREAD_XTIME_FOREVER NULL -//Pausing +// User data +#define PTHREAD_XTHREADS_MAX 65535 +#define PTHREAD_XNULL ((pthread_t)NULL) + +#ifdef __PTHREAD_EXTRA_INTERNAL +typedef struct pthread_user_data_internal_t { + pthread_t tid; //Thread ID + sig_atomic_t running; //Internaly used by pthread_pause + void *usr; //User pointer +} pthread_user_data_internal_t; + +pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread); +#endif //__PTHREAD_EXTRA_INTERNAL + +void** pthread_user_data_ptr(pthread_t thread); +void* pthread_user_data_get(pthread_t thread); +void pthread_user_data_set(pthread_t thread, void *usr); + +// Pausing + +//GDB: handle SIG34 nostop noprint #define PTHREAD_XSIG_STOP (SIGRTMIN+0) #define PTHREAD_XSIG_CONT (SIGRTMIN+1) #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal -#define pthread_pause(t) (pthread_kill((t), PTHREAD_XSIG_STOP)); -#define pthread_unpause(t) (pthread_kill((t), PTHREAD_XSIG_CONT)); - void pthread_unpause_handler(); void pthread_pause_handler(); void pthread_pause_enable(); +int pthread_pause(pthread_t thread); +int pthread_unpause(pthread_t thread); // Message queues diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 67a5023..662d78f 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -1,20 +1,64 @@ +#define __PTHREAD_EXTRA_INTERNAL + #include #include #include - -void pthread_unpause_handler() { - //NOP -} +#include +#include +#include +//#include +//#include void pthread_pause_handler() { + //Do nothing when there are more signals pending (to cleanup the queue) + sigset_t pending; + sigpending(&pending); + if(sigismember(&pending, PTHREAD_XSIG_STOP)) return; + + //Keep waiting for signals until we are supposed to be running sigset_t sigset; sigfillset(&sigset); - sigdelset(&sigset, PTHREAD_XSIG_CONT); - sigsuspend(&sigset); - //int sig; sigwait(&sigset, &sig); + sigdelset(&sigset, PTHREAD_XSIG_STOP); + while(!pthread_user_data_internal(pthread_self())->running) { + sigsuspend(&sigset); + } } void pthread_pause_enable() { + //Nesting signals too deep is not good for stack + //You can get runtime stats using following command: + //grep -i sig /proc/$(pgrep binary)/status + struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; + setrlimit(RLIMIT_SIGPENDING, &sigq); + + //Setup signal handler signal(PTHREAD_XSIG_STOP, pthread_pause_handler); - signal(PTHREAD_XSIG_CONT, pthread_unpause_handler); + + //Unblock signal + sigset_t sigset; + sigemptyset(&sigset); + sigaddset(&sigset, PTHREAD_XSIG_STOP); + pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); +} + +void pthread_pause_disable() { + //Block signal + sigset_t sigset; + sigemptyset(&sigset); + sigaddset(&sigset, PTHREAD_XSIG_STOP); + pthread_sigmask(SIG_BLOCK, &sigset, NULL); +} + +int pthread_pause(pthread_t thread) { + //Set thread as paused and notify it via signal (wait when queue full) + pthread_user_data_internal(thread)->running = 0; + while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + return 0; +} + +int pthread_unpause(pthread_t thread) { + //Set thread as running and notify it via signal (wait when queue full) + pthread_user_data_internal(thread)->running = 1; + while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + return 0; } diff --git a/c/pthread_extra/pthread_user_data.c b/c/pthread_extra/pthread_user_data.c new file mode 100644 index 0000000..b86c4a3 --- /dev/null +++ b/c/pthread_extra/pthread_user_data.c @@ -0,0 +1,43 @@ +#define __PTHREAD_EXTRA_INTERNAL + +#include +#include + +//Static array with user data for all thread handles +//TODO: perhaps use something more sophisticated like linked list? +pthread_user_data_internal_t pthread_user_data[PTHREAD_XTHREADS_MAX+1] = {{.tid=PTHREAD_XNULL}}; + +//Get pointer to internal record tied to specified thread +pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { + //Return NULL if requested thread handle is NULL + if(pthread_equal(thread, PTHREAD_XNULL)) return NULL; + + //Find if the thread is already registered, add it if not + //FIXME: recycle slots of destroyed threads!!! + pthread_t i; + for(i = 0; iusr; +} + +//Set user specified pointer for thread +void pthread_user_data_set(pthread_t thread, void *usr) { + *(pthread_user_data_ptr(thread)) = usr; +} + +//Get user specified pointer for thread +void* pthread_user_data_get(pthread_t thread) { + return *(pthread_user_data_ptr(thread)); +} -- 2.30.2