[Xenomai-git] Oliver Schlenker : Fork-safe rt_print
Module: xenomai-abe Branch: analogy Commit: 656f8a85325ff6fe6aa84310c0c3b0728a5fa4fc URL: http://git.xenomai.org/?p=xenomai-abe.git;a=commit;h=656f8a85325ff6fe6aa84310c0c3b0728a5fa4fc Author: Oliver Schlenker oliver.schlen...@smmotioncontrol.de Date: Fri Oct 23 20:44:01 2009 +0200 Fork-safe rt_print Fork-safe initialisation of rt_print library, necessary child initialisation are done via pthread_atfork() when child process is started. Signed-off-by: Oliver Schlenker oliver.schlen...@smmotioncontrol.de Signed-off-by: Jan Kiszka jan.kis...@siemens.com --- src/rtdk/rt_print.c | 33 + 1 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/rtdk/rt_print.c b/src/rtdk/rt_print.c index 0615247..dca9689 100644 --- a/src/rtdk/rt_print.c +++ b/src/rtdk/rt_print.c @@ -417,9 +417,35 @@ static void *printer_loop(void *arg) } } -void __rt_print_init(void) +static void spawn_printer_thread(void) { pthread_attr_t thattr; + + pthread_attr_init(thattr); + pthread_attr_setstacksize(thattr, PTHREAD_STACK_MIN); + pthread_create(printer_thread, thattr, printer_loop, NULL); +} + +static void forked_child_init(void) +{ + struct print_buffer *my_buffer = pthread_getspecific(buffer_key); + struct print_buffer **pbuffer = first_buffer; + + /* re-init to avoid finding it locked by some parent thread */ + pthread_mutex_init(buffer_lock, NULL); + + while (*pbuffer) { + if (*pbuffer == my_buffer) + pbuffer = (*pbuffer)-next; + else + cleanup_buffer(*pbuffer); + } + + spawn_printer_thread(); +} + +void __rt_print_init(void) +{ const char *value_str; unsigned long long period; @@ -456,7 +482,6 @@ void __rt_print_init(void) pthread_cond_init(printer_wakeup, NULL); - pthread_attr_init(thattr); - pthread_attr_setstacksize(thattr, PTHREAD_STACK_MIN); - pthread_create(printer_thread, thattr, printer_loop, NULL); + spawn_printer_thread(); + pthread_atfork(NULL, NULL, forked_child_init); } ___ Xenomai-git mailing list Xenomai-git@gna.org https://mail.gna.org/listinfo/xenomai-git
[Xenomai-git] Oliver Schlenker : Fork-safe rt_print
Module: xenomai-gch Branch: for-head Commit: 656f8a85325ff6fe6aa84310c0c3b0728a5fa4fc URL: http://git.xenomai.org/?p=xenomai-gch.git;a=commit;h=656f8a85325ff6fe6aa84310c0c3b0728a5fa4fc Author: Oliver Schlenker oliver.schlen...@smmotioncontrol.de Date: Fri Oct 23 20:44:01 2009 +0200 Fork-safe rt_print Fork-safe initialisation of rt_print library, necessary child initialisation are done via pthread_atfork() when child process is started. Signed-off-by: Oliver Schlenker oliver.schlen...@smmotioncontrol.de Signed-off-by: Jan Kiszka jan.kis...@siemens.com --- src/rtdk/rt_print.c | 33 + 1 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/rtdk/rt_print.c b/src/rtdk/rt_print.c index 0615247..dca9689 100644 --- a/src/rtdk/rt_print.c +++ b/src/rtdk/rt_print.c @@ -417,9 +417,35 @@ static void *printer_loop(void *arg) } } -void __rt_print_init(void) +static void spawn_printer_thread(void) { pthread_attr_t thattr; + + pthread_attr_init(thattr); + pthread_attr_setstacksize(thattr, PTHREAD_STACK_MIN); + pthread_create(printer_thread, thattr, printer_loop, NULL); +} + +static void forked_child_init(void) +{ + struct print_buffer *my_buffer = pthread_getspecific(buffer_key); + struct print_buffer **pbuffer = first_buffer; + + /* re-init to avoid finding it locked by some parent thread */ + pthread_mutex_init(buffer_lock, NULL); + + while (*pbuffer) { + if (*pbuffer == my_buffer) + pbuffer = (*pbuffer)-next; + else + cleanup_buffer(*pbuffer); + } + + spawn_printer_thread(); +} + +void __rt_print_init(void) +{ const char *value_str; unsigned long long period; @@ -456,7 +482,6 @@ void __rt_print_init(void) pthread_cond_init(printer_wakeup, NULL); - pthread_attr_init(thattr); - pthread_attr_setstacksize(thattr, PTHREAD_STACK_MIN); - pthread_create(printer_thread, thattr, printer_loop, NULL); + spawn_printer_thread(); + pthread_atfork(NULL, NULL, forked_child_init); } ___ Xenomai-git mailing list Xenomai-git@gna.org https://mail.gna.org/listinfo/xenomai-git