summaryrefslogtreecommitdiff
path: root/src/time/timer_create.c
AgeCommit message (Collapse)AuthorLines
2023-11-06timer_create: volatile static -> static volatileAlex Xu (Hello71)-1/+1
C11 6.11.5p1: > The placement of a storage-class specifier other than at the > beginning of the declaration specifiers in a declaration is an > obsolescent feature. gcc also warns about this.
2022-09-19fix thread leak on timer_create(SIGEV_THREAD) failureAlexey Izbyshev-1/+5
After commit 5b74eed3b301e2227385f3bf26d3bb7c2d822cf8 the timer thread doesn't check whether timer_create() actually created the timer, proceeding to wait for a signal that might never arrive. We can't fix this by simply checking for a negative timer_id after pthread_barrier_wait() because we have no way to distinguish a timer creation failure and a request to delete a timer with INT_MAX id if it happens to arrive quickly (a variation of this bug existed before 5b74eed3b301e2227385f3bf26d3bb7c2d822cf8, where the timer would be leaked in this case). So (ab)use cancel field of pthread_t instead.
2020-10-28add support for SIGEV_THREAD_ID timersJames Y Knight-2/+6
This is like SIGEV_SIGNAL, but targeted to a particular thread's tid, rather than the process.
2020-10-14drop use of pthread_once in timer_createRich Felker-10/+7
this makes the code slightly smaller and eliminates timer_create from relevance to possible future changes to multithreaded fork. the barrier of a_store isn't technically needed here, but a_store is used anyway for internal consistency of the memory model.
2020-10-14remove unused SIGTIMER handler in timer_createRich Felker-6/+1
this was leftover from when the actual SIGEV_THREAD timer logic was in the signal handler. commit 5b74eed3b301e2227385f3bf26d3bb7c2d822cf8 replaced that with use of sigwaitinfo, with the actual signal left blocked, so the no-op signal handler was no longer serving any purpose. the signal disposition reset to SIG_DFL is still needed, however, in case we inherited SIG_IGN from a foreign-libc process.
2019-09-25fix data race in timer_create with SIGEV_THREAD notificationRich Felker-2/+2
in the timer thread start function, self->timer_id was accessed without synchronization; the timer thread could fail to see the store from the calling thread, resulting in timer_delete failing to delete the correct kernel-level timer. this fix is based on a patch by changdiankang, but with the load moved to after receiving the timer_delete signal rather than just after the start barrier, so as not to retain the possibility of data race with timer_delete.
2019-02-15always block signals for starting new threads, refactor start argsRich Felker-1/+0
whether signals need to be blocked at thread start, and whether unblocking is necessary in the entry point function, has historically depended on intricacies of the cancellation design and on whether there are scheduling operations to perform on the new thread before its successful creation can be committed. future changes to track an AS-safe list of live threads will require signals to be blocked whenever changes are made to the list, so ... prior to commits b8742f32602add243ee2ce74d804015463726899 and 40bae2d32fd6f3ffea437fa745ad38a1fe77b27e, a signal mask for the entry function to restore was part of the pthread structure. it was removed to trim down the size of the structure, which both saved a small amount of stack space and improved code generation on archs where small immediate displacements are less costly than arbitrary ones, by limiting the range of offsets between the base of the thread structure, its members, and the thread pointer. these commits moved the saved mask to a special structure used only when special scheduling was needed, in which case the pthread_create caller and new thread had to synchronize with each other and could use this memory to pass a mask. this commit partially reverts the above two commits, but instead of putting the mask back in the pthread structure, it moves all "start argument" members out of the pthread structure, trimming it down further, and puts them in a separate structure passed on the new thread's stack. the code path for explicit scheduling of the new thread is also changed to synchronize with the calling thread in such a way to avoid spurious futex wakes.
2019-02-15for SIGEV_THREAD timer threads, replace signal handler with sigwaitinfoRich Felker-20/+15
this eliminates some ugly hacks that were repurposing the start function and start argument fields in the pthread structure for timer use, and the need to longjmp out of a signal handler.
2019-01-21fix call to __pthread_tsd_run_dtors with too many argumentsRich Felker-1/+1
commit a6054e3c94aa0491d7366e4b05ae0d73f661bfe2 removed the argument, making it a constraint violation to pass one. caught by cparser/firm; other compilers seem to ignore it.
2018-09-12move declarations of tls setup/access functions to pthread_impl.hRich Felker-2/+0
it's already included in all places where these are needed, and aside from __tls_get_addr, they're all implementation internals.
2017-11-09fix mismatched type of __pthread_tsd_run_dtors weak definitionRich Felker-2/+2
commit a6054e3c94aa0491d7366e4b05ae0d73f661bfe2 changed this function not to take an argument, but the weak definition used by timer_create was not updated to match. reported by Pascal Cuoq.
2013-08-03have new timer threads unblock their own SIGTIMERRich Felker-2/+2
unblocking it in the pthread_once init function is not sufficient, since multiple threads, some of them with the signal blocked, could already exist before this is called; timers started from such threads would be non-functional.
2013-08-03add system for resetting TLS to initial valuesRich Felker-0/+3
this is needed for reused threads in the SIGEV_THREAD timer notification system, and could be reused elsewhere in the future if needed, though it should be refactored for such use. for static linking, __init_tls.c is simply modified to export the TLS info in a structure with external linkage, rather than using statics. this perhaps makes the code more clear, since the statics were poorly named for statics. the new __reset_tls.c is only linked if it is used. for dynamic linking, the code is in dynlink.c. sharing code with __copy_tls is not practical since __reset_tls must also re-zero thread-local bss.
2013-08-03fix multiple bugs in SIGEV_THREAD timersRich Felker-14/+15
1. the thread result field was reused for storing a kernel timer id, but would be overwritten if the application code exited or cancelled the thread. 2. low pointer values were used as the indicator that the timer id is a kernel timer id rather than a thread id. this is not portable, as mmap may return low pointers on some conditions. instead, use the fact that pointers must be aligned and kernel timer ids must be non-negative to map pointers into the negative integer space. 3. signals were not blocked until after the timer thread started, so a race condition could allow a signal handler to run in the timer thread when it's not supposed to exist. this is mainly problematic if the calling thread was the only thread where the signal was unblocked and the signal handler assumes it runs in that thread.
2013-04-06silence nonsensical warnings in timer_createRich Felker-2/+2
2013-03-26remove __SYSCALL_SSLEN arch macro in favor of using public _NSIGRich Felker-1/+1
the issue at hand is that many syscalls require as an argument the kernel-ABI size of sigset_t, intended to allow the kernel to switch to a larger sigset_t in the future. previously, each arch was defining this size in syscall_arch.h, which was redundant with the definition of _NSIG in bits/signal.h. as it's used in some not-quite-portable application code as well, _NSIG is much more likely to be recognized and understood immediately by someone reading the code, and it's also shorter and less cluttered. note that _NSIG is actually 65/129, not 64/128, but the division takes care of throwing away the off-by-one part.
2012-11-08clean up sloppy nested inclusion from pthread_impl.hRich Felker-0/+1
this mirrors the stdio_impl.h cleanup. one header which is not strictly needed, errno.h, is left in pthread_impl.h, because since pthread functions return their error codes rather than using errno, nearly every single pthread function needs the errno constants. in a few places, rather than bringing in string.h to use memset, the memset was replaced by direct assignment. this seems to generate much better code anyway, and makes many functions which were previously non-leaf functions into leaf functions (possibly eliminating a great deal of bloat on some platforms where non-leaf functions require ugly prologue and/or epilogue).
2012-09-06use restrict everywhere it's required by c99 and/or posix 2008Rich Felker-1/+1
to deal with the fact that the public headers may be used with pre-c99 compilers, __restrict is used in place of restrict, and defined appropriately for any supported compiler. we also avoid the form [restrict] since older versions of gcc rejected it due to a bug in the original c99 standard, and instead use the form *restrict.
2012-08-09fix (hopefully) all hard-coded 8's for kernel sigset_t sizeRich Felker-1/+2
some minor changes to how hard-coded sets for thread-related purposes are handled were also needed, since the old object sizes were not necessarily sufficient. things have gotten a bit ugly in this area, and i think a cleanup is in order at some point, but for now the goal is just to get the code working on all supported archs including mips, which was badly broken by linux rejecting syscalls with the wrong sigset_t size.
2011-08-12more efficient signal blocking for timer threadsRich Felker-4/+4
due to the barrier, it's safe just to block signals in the new thread, rather than blocking and unblocking in the parent thread.
2011-08-11normal exit from timer thread should run dtors, restore cancel stateRich Felker-1/+1
2011-08-11block signals in timer threadsRich Felker-0/+4
if a timer thread leaves signals unblocked, any future attempt by the main thread to prevent the process from being terminated by blocking signals will fail, since the signal can still be delivered to the timer thread.
2011-05-07optimize compound-literal sigset_t's not to contain useless hurd bitsRich Felker-1/+1
2011-05-07overhaul implementation-internal signal protectionsRich Felker-2/+1
the new approach relies on the fact that the only ways to create sigset_t objects without invoking UB are to use the sig*set() functions, or from the masks returned by sigprocmask, sigaction, etc. or in the ucontext_t argument to a signal handler. thus, as long as sigfillset and sigaddset avoid adding the "protected" signals, there is no way the application will ever obtain a sigset_t including these bits, and thus no need to add the overhead of checking/clearing them when sigprocmask or sigaction is called. note that the old code actually *failed* to remove the bits from sa_mask when sigaction was called. the new implementations are also significantly smaller, simpler, and faster due to ignoring the useless "GNU HURD signals" 65-1024, which are not used and, if there's any sanity in the world, never will be used.
2011-04-14use a separate signal from SIGCANCEL for SIGEV_THREAD timersRich Felker-7/+25
otherwise we cannot support an application's desire to use asynchronous cancellation within the callback function. this change also slightly debloats pthread_create.c.
2011-04-09run pthread tsd destructors when a timer thread pretends to exitRich Felker-0/+6
2011-04-09greatly improve SIGEV_THREAD timersRich Felker-14/+14
calling pthread_exit from, or pthread_cancel on, the timer callback thread will no longer destroy the timer.
2011-04-06fix signal-based timers with null sigevent argumentRich Felker-19/+14
since timer_create is no longer allocating a structure for the timer_t and simply using the kernel timer id, it was impossible to specify the timer_t as the argument to the signal handler. the solution is to pass the null sigevent pointer on to the kernel, rather than filling it in userspace, so that the kernel does the right thing. however, that precludes the clever timerid-versus-threadid encoding we were doing. instead, just assume timerids are below 1M and thread pointers are above 1M. (in perspective: timerids are sequentially allocated and seem limited to 32k, and thread pointers are at roughly 3G.)
2011-04-03timer threads should sleep and stay asleep... a long timeRich Felker-1/+1
2011-04-03revert to deleting kernel-level timer from cancellation handlerRich Felker-0/+8
this is necessary in order to avoid breaking timer_getoverrun in the last run of the timer event handler, if it has not yet finished.
2011-04-03simplify calling of timer signal handlerRich Felker-3/+1
2011-03-30avoid all malloc/free in timer creation/destructionRich Felker-20/+4
instead of allocating a userspace structure for signal-based timers, simply use the kernel timer id. we use the fact that thread pointers will always be zero in the low bit (actually more) to encode integer timerid values as pointers. also, this change ensures that the timer_destroy syscall has completed before the library timer_destroy function returns, in case it matters.
2011-03-30optimize timer creation and possibly protect against some minor racesRich Felker-14/+19
the major idea of this patch is not to depend on having the timer pointer delivered to the signal handler, and instead use the thread pointer to get the callback function address and argument. this way, the parent thread can make the timer_create syscall while the child thread is starting, and it should never have to block waiting for the barrier.
2011-03-29reorder timer initialization so that timer_create does not depend on freeRich Felker-8/+16
this allows small programs which only create times, but never delete them, to use simple_malloc instead of the full malloc.
2011-03-29implement POSIX timersRich Felker-0/+110
this implementation is superior to the glibc/nptl implementation, in that it gives true realtime behavior. there is no risk of timer expiration events being lost due to failed thread creation or failed malloc, because the thread is created as time creation time, and reused until the timer is deleted.