public inbox for glibc-cvs@sourceware.org help / color / mirror / Atom feed
From: Florian Weimer <fw@sourceware.org> To: glibc-cvs@sourceware.org Subject: [glibc] support: Add delayed__exit (with two underscores) Date: Tue, 6 Jun 2023 11:17:43 +0000 (GMT) [thread overview] Message-ID: <20230606111743.29079385800C@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=047703fbb88eb38fbe973f3abedb279382f181d0 commit 047703fbb88eb38fbe973f3abedb279382f181d0 Author: Florian Weimer <fweimer@redhat.com> Date: Tue Jun 6 11:37:30 2023 +0200 support: Add delayed__exit (with two underscores) It calls _exit instead of exit once the timeout expires. Diff: --- support/delayed_exit.c | 41 +++++++++++++++++++++++++++++++++-------- support/xthread.h | 9 ++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/support/delayed_exit.c b/support/delayed_exit.c index 53aa40c275..bc64ff0d3e 100644 --- a/support/delayed_exit.c +++ b/support/delayed_exit.c @@ -23,33 +23,58 @@ #include <stdio.h> #include <stdlib.h> #include <support/check.h> +#include <support/support.h> #include <time.h> +#include <unistd.h> + +struct delayed_exit_request +{ + void (*exitfunc) (int); + int seconds; +}; static void * -delayed_exit_thread (void *seconds_as_ptr) +delayed_exit_thread (void *closure) { - int seconds = (uintptr_t) seconds_as_ptr; - struct timespec delay = { seconds, 0 }; + struct delayed_exit_request *request = closure; + void (*exitfunc) (int) = request->exitfunc; + struct timespec delay = { request->seconds, 0 }; struct timespec remaining = { 0 }; + free (request); + if (nanosleep (&delay, &remaining) != 0) FAIL_EXIT1 ("nanosleep: %m"); /* Exit the process successfully. */ - exit (0); + exitfunc (0); return NULL; } -void -delayed_exit (int seconds) +static void +delayed_exit_1 (int seconds, void (*exitfunc) (int)) { /* Create the new thread with all signals blocked. */ sigset_t all_blocked; sigfillset (&all_blocked); sigset_t old_set; xpthread_sigmask (SIG_SETMASK, &all_blocked, &old_set); + struct delayed_exit_request *request = xmalloc (sizeof (*request)); + request->seconds = seconds; + request->exitfunc = exitfunc; /* Create a detached thread. */ - pthread_t thr = xpthread_create - (NULL, delayed_exit_thread, (void *) (uintptr_t) seconds); + pthread_t thr = xpthread_create (NULL, delayed_exit_thread, request); xpthread_detach (thr); /* Restore the original signal mask. */ xpthread_sigmask (SIG_SETMASK, &old_set, NULL); } + +void +delayed_exit (int seconds) +{ + delayed_exit_1 (seconds, exit); +} + +void +delayed__exit (int seconds) +{ + delayed_exit_1 (seconds, _exit); +} diff --git a/support/xthread.h b/support/xthread.h index b1e85a9b56..402df561aa 100644 --- a/support/xthread.h +++ b/support/xthread.h @@ -25,11 +25,14 @@ __BEGIN_DECLS -/* Terminate the process (with exit status 0) after SECONDS have - elapsed, from a helper thread. The process is terminated with the - exit function, so atexit handlers are executed. */ +/* Terminate the process (with exit (0)) after SECONDS have elapsed, + from a helper thread. The process is terminated with the exit + function, so atexit handlers are executed. */ void delayed_exit (int seconds); +/* Like delayed_exit, but use _exit (0). */ +void delayed__exit (int seconds); + /* Returns true if Priority Inheritance support CLOCK_MONOTONIC. */ bool support_mutex_pi_monotonic (void);
reply other threads:[~2023-06-06 11:17 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230606111743.29079385800C@sourceware.org \ --to=fw@sourceware.org \ --cc=glibc-cvs@sourceware.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: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).