aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/timer_delete.c
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2023-06-19 14:11:58 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2025-04-04 14:57:36 -0300
commit5bc7f090051c094656b71c9cbeb737040c1aedf6 (patch)
tree5b1b74c3b9dc4987699b416b51d12a2f43be7dfa /sysdeps/unix/sysv/linux/timer_delete.c
parentc8e73a1492b01b9b0c189d6a5c53a5a697827bae (diff)
downloadglibc-azanella/bz30558-posix_timer.tar.xz
glibc-azanella/bz30558-posix_timer.zip
linux: Do not spawn a new thread for SIGEV_THREAD (BZ 30558, 27895, 29705, 32833)azanella/bz30558-posix_timer
The current timer_create SIGEV_THREAD implementation has some downsides: 1. There is no way to report failure at thread creation when a timer triggers. It means that it might occur unreported and with missed events depending of the system load. 2. The backgroup thread also kept in backgroun even when there is no more timers, consuming resources and also misleading memory profile tools (BZ 29705). 3. There is a lot of metadata that required to be kept: a control variable for helper thread creation, a list of active SIGEV_THREAD timers, atfork handlers to cleanup the list. 4. timer_create does not propagate all thread attributes to the new thread (BZ 27895). 5. Kernel might deliver in-flight events for a timer after it was destroyed by timer_delete. The timer_helper_thread mechanism to handle it does not cover all possible issue, which leads to callbacks being wrong triggered (BZ 32833). This new implementation moves the thread creation to timer_create, so any failure is reported to the caller. Also, the same thread will issues the multiple timers, thus there is no unreported missed events. Also, avoiding parallel timer activation also avoid possible parallel timer invocation to see the same overrun value. To implement using SIGTIMER internally as SIGCANCEL, it requires to mask out SIGCANCEL on thread creation. It essentially disable async thread cancellation, but POSIX requires that SIGEV_THREAD is always created in detached mode and cancelling detached thread s UB (glibc check the internal tid, but the memory referenced by pthread_t might not always be valid as the momento of pthread_cancel call). And to avoid the need to recreate the thread for pthread_exit call (and having possible unreported missed due failed thread creation), the SIGEV_THREAD install a cleanup handler that reset all internal thread state. It also prevents the re-use issue when a newly-allocated timer has in-flight event being delivered by the kernel (BZ 32833). Performance-wise it see it uses less CPU timer for multiple thread activation, although each thread now requires a sigwaitinfo which generate more context-switches/page-faults (check comment 7 from BZ 30558). I would expect that latency should improve, since it avoid a thread creation for each timer expiration. Checked on x86_64-linux-gnu and i686-linux-gnu.
Diffstat (limited to 'sysdeps/unix/sysv/linux/timer_delete.c')
-rw-r--r--sysdeps/unix/sysv/linux/timer_delete.c46
1 files changed, 11 insertions, 35 deletions
diff --git a/sysdeps/unix/sysv/linux/timer_delete.c b/sysdeps/unix/sysv/linux/timer_delete.c
index 69f26b266b..0fd3cb15f1 100644
--- a/sysdeps/unix/sysv/linux/timer_delete.c
+++ b/sysdeps/unix/sysv/linux/timer_delete.c
@@ -15,10 +15,8 @@
License along with the GNU C Library; see the file COPYING.LIB. If
not, see <https://www.gnu.org/licenses/>. */
-#include <errno.h>
-#include <stdlib.h>
+#include <unistd.h>
#include <time.h>
-#include <sysdep.h>
#include "kernel-posix-timers.h"
#include <pthreadP.h>
#include <shlib-compat.h>
@@ -26,42 +24,20 @@
int
___timer_delete (timer_t timerid)
{
- kernel_timer_t ktimerid = timerid_to_kernel_timer (timerid);
- int res = INLINE_SYSCALL_CALL (timer_delete, ktimerid);
-
- if (res == 0)
+ if (timer_is_sigev_thread (timerid))
{
- if (timer_is_sigev_thread (timerid))
- {
- struct timer *kt = timerid_to_timer (timerid);
-
- /* Remove the timer from the list. */
- __pthread_mutex_lock (&__timer_active_sigev_thread_lock);
- if (__timer_active_sigev_thread == kt)
- __timer_active_sigev_thread = kt->next;
- else
- {
- struct timer *prevp = __timer_active_sigev_thread;
- while (prevp->next != NULL)
- if (prevp->next == kt)
- {
- prevp->next = kt->next;
- break;
- }
- else
- prevp = prevp->next;
- }
- __pthread_mutex_unlock (&__timer_active_sigev_thread_lock);
-
- free (kt);
- }
+ struct pthread *th = timerid_to_pthread (timerid);
+ /* The helper thread itself will be responsible to call the
+ timer_delete syscall. */
+ timerid_signal_delete (&th->timerid);
+ /* We can send the signal directly instead of through
+ __pthread_kill_internal because the thread is not user-visible
+ and it blocks SIGTIMER. */
+ INTERNAL_SYSCALL_CALL (tgkill, __getpid (), th->tid, SIGTIMER);
return 0;
}
-
- /* The kernel timer is not known or something else bad happened.
- Return the error. */
- return -1;
+ return INLINE_SYSCALL_CALL (timer_delete, timerid);
}
versioned_symbol (libc, ___timer_delete, timer_delete, GLIBC_2_34);
libc_hidden_ver (___timer_delete, __timer_delete)