From 9b4be8e8c190f7734a7611c2f354e2616e0f2c2f Mon Sep 17 00:00:00 2001 From: Tomas Mudrunka Date: Thu, 1 Jul 2021 15:36:31 +0200 Subject: [PATCH] Implementace vyhradniho rezimu --- c/pthread_extra/pthread_extra.h | 3 +++ c/pthread_extra/pthread_pause.c | 14 +++++++------- c/pthread_extra/pthread_user_data.c | 13 +++++++++++++ c/pthread_extra/test_pause.c | 7 +++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index ca5de70..4df45d9 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -32,6 +32,7 @@ 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); void pthread_user_data_cleanup(void * arg); +int pthread_user_data_internal_iterate(int (*routine)(pthread_t), void *arg); // Pausing @@ -50,6 +51,8 @@ void pthread_pause_enable(); void pthread_pause_disable(); int pthread_pause(pthread_t thread); int pthread_unpause(pthread_t thread); +int pthread_pause_all(); +int pthread_unpause_all(); int pthread_pause_reschedule(pthread_t thread); int pthread_extra_yield(); diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 4107b38..63ccc6a 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -14,7 +14,7 @@ ///When this variable is nonzero, only referenced thread is allowed to run ///Access has to be protected by pthread_user_data_lock() -pthread_t pthread_pause_holder = 0; +pthread_t pthread_pause_holder = PTHREAD_XNULL; void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) { (void)signal; (void)info; (void)ptr; @@ -98,7 +98,7 @@ int pthread_pause_reschedule(pthread_t thread) { //Check if thread has running flag int run = (pthread_user_data_internal(thread)->running); //Check if privileged (single thread) mode is active - if((pthread_pause_holder != 0) && !pthread_equal(pthread_pause_holder, thread)) { + if((pthread_pause_holder != PTHREAD_XNULL) && !pthread_equal(pthread_pause_holder, thread)) { run = 0; } pthread_user_data_unlock(); @@ -137,19 +137,19 @@ int pthread_unpause(pthread_t thread) { int pthread_pause_all() { pthread_user_data_lock(); - if(pthread_pause_holder!=0) assert(pthread_equal(pthread_pause_holder, pthread_self())); + if(pthread_pause_holder!=PTHREAD_XNULL) assert(pthread_equal(pthread_pause_holder, pthread_self())); pthread_pause_holder = pthread_self(); pthread_user_data_unlock(); - //todo reschedule all + pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); return 0; } int pthread_unpause_all() { pthread_user_data_lock(); - if(pthread_pause_holder!=0) assert(pthread_equal(pthread_pause_holder, pthread_self())); - pthread_pause_holder = 0; + if(pthread_pause_holder!=PTHREAD_XNULL) assert(pthread_equal(pthread_pause_holder, pthread_self())); + pthread_pause_holder = PTHREAD_XNULL; pthread_user_data_unlock(); - //todo reschedule + pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); return 0; } diff --git a/c/pthread_extra/pthread_user_data.c b/c/pthread_extra/pthread_user_data.c index a1e1ebd..cf97a65 100644 --- a/c/pthread_extra/pthread_user_data.c +++ b/c/pthread_extra/pthread_user_data.c @@ -48,6 +48,19 @@ pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { return &pthread_user_data[i]; } +//Iterate specified callback over all registered threads +int pthread_user_data_internal_iterate(int (*routine)(pthread_t), void *arg) { + (void) arg; + int i; + pthread_user_data_lock(); + for(i = 0; iusr; diff --git a/c/pthread_extra/test_pause.c b/c/pthread_extra/test_pause.c index f17be8a..ed60fcc 100644 --- a/c/pthread_extra/test_pause.c +++ b/c/pthread_extra/test_pause.c @@ -54,6 +54,13 @@ int main() { pthread_unpause(a); pthread_unpause(b); sleep(1); + + printf("SWITCH MAIN ONLY:\n"); + pthread_pause_all(); + sleep(1); + printf("SWITCH MAIN A+B:\n"); + pthread_unpause_all(); + sleep(1); } pthread_join(a, NULL); -- 2.30.2