From: Tomas Mudrunka Date: Thu, 8 Jul 2021 11:42:54 +0000 (+0200) Subject: More strict ownership asserts X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=46a7758b58eeff9b507e6e173b03b7fd909e22c3;p=mirrors%2FPrograms.git More strict ownership asserts --- diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index f8fb296..950d6fa 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -150,9 +150,17 @@ int pthread_extra_yield() { return pthread_yield(); } +///Sanity check to be sure there are no race conditions +inline void pthread_pause_assert_owner() { + if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) + assert(pthread_equal(pthread_pause_holder, pthread_self())); +} + ///Pause specified thread (block until it is paused) int pthread_pause(pthread_t thread) { pthread_user_data_lock(); + pthread_pause_assert_owner(); + //if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self())); //Set thread as paused and notify it via signal (wait when queue full) pthread_user_data_internal(thread)->running = 0; pthread_pause_reschedule(thread); @@ -163,6 +171,7 @@ int pthread_pause(pthread_t thread) { ///UnPause specified thread (block until it is unpaused) int pthread_unpause(pthread_t thread) { pthread_user_data_lock(); + pthread_pause_assert_owner(); //Set thread as running and notify it via signal (wait when queue full) pthread_user_data_internal(thread)->running = 1; pthread_pause_reschedule(thread); @@ -176,7 +185,7 @@ int pthread_pause_all() { pthread_user_data_lock(); //printf("Pause ALL+\n"); //printf("Pause %p == %p\n", (void *)pthread_pause_holder, (void *)pthread_self()); - if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self())); + pthread_pause_assert_owner(); pthread_pause_holder = pthread_self(); pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); //printf("Pause ALL!\n"); @@ -189,7 +198,7 @@ int pthread_unpause_all() { //printf("UnPause ALL\n"); pthread_user_data_lock(); //printf("UnPause ALL+\n"); - if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self())); + pthread_pause_assert_owner(); pthread_pause_holder = PTHREAD_XNULL; pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); //printf("UnPause ALL!\n");