From 24286ba9a7ae3a8c6f5eefe9455e18bd758e742b Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Tue, 29 Jun 2021 13:15:11 +0200 Subject: [PATCH] Fixed deadlocks in pthread_pause --- c/pthread_extra/Makefile | 2 +- c/pthread_extra/README.md | 4 ++-- c/pthread_extra/pthread_pause.c | 2 +- c/pthread_extra/test_pause.c | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/c/pthread_extra/Makefile b/c/pthread_extra/Makefile index 0b96b41..1197cca 100644 --- a/c/pthread_extra/Makefile +++ b/c/pthread_extra/Makefile @@ -1,5 +1,5 @@ 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) +#COPT=-g -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 diff --git a/c/pthread_extra/README.md b/c/pthread_extra/README.md index 0824f47..60876f2 100644 --- a/c/pthread_extra/README.md +++ b/c/pthread_extra/README.md @@ -5,7 +5,7 @@ Currently will probably only work on Linux. Mostly experimental code. ### Components + * pthread_pause - implements suspend/resume functionality for pthreads (no know issues, recently fixed some deadlocks, but not really tested) * 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) + * pthread_multi - lock multiple mutexes at once (might cause deadlocks in complex scenarios???) diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 662d78f..6df2e3a 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -19,7 +19,7 @@ void pthread_pause_handler() { sigset_t sigset; sigfillset(&sigset); sigdelset(&sigset, PTHREAD_XSIG_STOP); - while(!pthread_user_data_internal(pthread_self())->running) { + if(!pthread_user_data_internal(pthread_self())->running) { sigsuspend(&sigset); } } diff --git a/c/pthread_extra/test_pause.c b/c/pthread_extra/test_pause.c index 47f2db0..4673289 100644 --- a/c/pthread_extra/test_pause.c +++ b/c/pthread_extra/test_pause.c @@ -22,6 +22,22 @@ int main() { //That way you can be sure it is pausable immediately pthread_create(&a, NULL, thread_test, " A"); pthread_create(&b, NULL, thread_test, " B"); + //sleep(1); + + //printf("OK\n"); + /* + for(int32_t i = 1;i>0;i++) { + pthread_pause(a); + pthread_pause(a); + pthread_unpause(a); + pthread_unpause(a); + } + */ + /* + exit(23); + pthread_pause(a); + pthread_unpause(b); + */ while(1) { pthread_pause(b); -- 2.30.2