All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@domain.hid>
To: Philippe Gerum <rpm@xenomai.org>
Cc: xenomai@xenomai.org, Oliver Schlenker <oliver.schlenker@domain.hid>
Subject: [Xenomai-core] [PATCH 1/2] Fork-safe rt_print
Date: Sat, 24 Oct 2009 10:03:04 +0200	[thread overview]
Message-ID: <20091024080304.22784.93196.stgit@domain.hid> (raw)
In-Reply-To: <20091024080304.22784.46688.stgit@domain.hid>

From: Oliver Schlenker <oliver.schlenker@domain.hid>

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.schlenker@domain.hid>
Signed-off-by: Jan Kiszka <jan.kiszka@domain.hid>
---

 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);
 }



  reply	other threads:[~2009-10-24  8:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-24  8:03 [Xenomai-core] [PATCH 0/2] fork-safe rt_print, rt_syslog extension Jan Kiszka
2009-10-24  8:03 ` Jan Kiszka [this message]
2009-10-24  8:03 ` [Xenomai-core] [PATCH 2/2] Syslog extension of rt_print Jan Kiszka
2009-10-24 16:38 ` [Xenomai-core] [PATCH 0/2] fork-safe rt_print, rt_syslog extension Philippe Gerum
2009-10-26  8:14   ` Jan Kiszka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20091024080304.22784.93196.stgit@domain.hid \
    --to=jan.kiszka@domain.hid \
    --cc=oliver.schlenker@domain.hid \
    --cc=rpm@xenomai.org \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.