1/* Copyright (C) 2003-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
18
19#include <pthread.h>
20#include <setjmp.h>
21#include <signal.h>
22#include <sys/types.h>
23
24
25/* Nonzero if the system calls are not available. */
26extern int __no_posix_timers attribute_hidden;
27
28/* Callback to start helper thread. */
29extern void __timer_start_helper_thread (void) attribute_hidden;
30
31/* Control variable for helper thread creation. */
32extern pthread_once_t __timer_helper_once attribute_hidden;
33
34/* Called from fork so that the new subprocess re-creates the
35 notification thread if necessary. */
36void __timer_fork_subprocess (void) attribute_hidden;
37
38/* TID of the helper thread. */
39extern pid_t __timer_helper_tid attribute_hidden;
40
41/* List of active SIGEV_THREAD timers. */
42extern struct timer *__timer_active_sigev_thread attribute_hidden;
43
44/* Lock for __timer_active_sigev_thread. */
45extern pthread_mutex_t __timer_active_sigev_thread_lock attribute_hidden;
46
47extern __typeof (timer_create) __timer_create;
48libc_hidden_proto (__timer_create)
49extern __typeof (timer_delete) __timer_delete;
50libc_hidden_proto (__timer_delete)
51extern __typeof (timer_getoverrun) __timer_getoverrun;
52libc_hidden_proto (__timer_getoverrun)
53
54/* Type of timers in the kernel. */
55typedef int kernel_timer_t;
56
57/* Internal representation of SIGEV_THREAD timer. */
58struct timer
59{
60 kernel_timer_t ktimerid;
61
62 void (*thrfunc) (sigval_t);
63 sigval_t sival;
64 pthread_attr_t attr;
65
66 /* Next element in list of active SIGEV_THREAD timers. */
67 struct timer *next;
68};
69
70
71/* For !SIGEV_THREAD, the resulting 'timer_t' is the returned kernel timer
72 identifer (kernel_timer_t), while for SIGEV_THREAD it uses the fact malloc
73 returns at least _Alignof (max_align_t) pointers plus that valid
74 kernel_timer_t are always positive to set the MSB bit of the returned
75 'timer_t' to indicate the timer handles a SIGEV_THREAD. */
76
77static inline timer_t
78kernel_timer_to_timerid (kernel_timer_t ktimerid)
79{
80 return (timer_t) ((intptr_t) ktimerid);
81}
82
83static inline timer_t
84timer_to_timerid (struct timer *ptr)
85{
86 return (timer_t) (INTPTR_MIN | (uintptr_t) ptr >> 1);
87}
88
89static inline bool
90timer_is_sigev_thread (timer_t timerid)
91{
92 return (intptr_t) timerid < 0;
93}
94
95static inline struct timer *
96timerid_to_timer (timer_t timerid)
97{
98 return (struct timer *)((uintptr_t) timerid << 1);
99}
100
101static inline kernel_timer_t
102timerid_to_kernel_timer (timer_t timerid)
103{
104 if (timer_is_sigev_thread (timerid))
105 return timerid_to_timer (timerid)->ktimerid;
106 else
107 return (kernel_timer_t) ((uintptr_t) timerid);
108}
109
110/* New targets use int instead of timer_t. The difference only
111 matters on 64-bit targets. */
112#include <timer_t_was_int_compat.h>
113
114#if TIMER_T_WAS_INT_COMPAT
115# define OLD_TIMER_MAX 256
116extern timer_t __timer_compat_list[OLD_TIMER_MAX];
117#endif
118