2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
32 #include "namespace.h"
37 #include <stdatomic.h>
43 #include "un-namespace.h"
45 #include "sigev_thread.h"
47 LIST_HEAD(sigev_list_head, sigev_node);
48 #define HASH_QUEUES 17
49 #define HASH(t, id) ((((id) << 3) + (t)) % HASH_QUEUES)
51 static struct sigev_list_head sigev_hash[HASH_QUEUES];
52 static struct sigev_list_head sigev_all;
53 static LIST_HEAD(,sigev_thread) sigev_threads;
54 static atomic_int sigev_generation;
55 static pthread_mutex_t *sigev_list_mtx;
56 static pthread_once_t sigev_once = PTHREAD_ONCE_INIT;
57 static pthread_once_t sigev_once_default = PTHREAD_ONCE_INIT;
58 static struct sigev_thread *sigev_default_thread;
59 static pthread_attr_t sigev_default_attr;
60 static int atfork_registered;
62 static void __sigev_fork_prepare(void);
63 static void __sigev_fork_parent(void);
64 static void __sigev_fork_child(void);
65 static struct sigev_thread *sigev_thread_create(int);
66 static void *sigev_service_loop(void *);
67 static void *worker_routine(void *);
68 static void worker_cleanup(void *);
70 #pragma weak _pthread_create
73 attrcopy(pthread_attr_t *src, pthread_attr_t *dst)
75 struct sched_param sched;
80 _pthread_attr_getschedpolicy(src, &v);
81 _pthread_attr_setschedpolicy(dst, v);
83 _pthread_attr_getinheritsched(src, &v);
84 _pthread_attr_setinheritsched(dst, v);
86 _pthread_attr_getschedparam(src, &sched);
87 _pthread_attr_setschedparam(dst, &sched);
89 _pthread_attr_getscope(src, &v);
90 _pthread_attr_setscope(dst, v);
92 _pthread_attr_getstacksize(src, &u);
93 _pthread_attr_setstacksize(dst, u);
95 _pthread_attr_getstackaddr(src, &a);
96 _pthread_attr_setstackaddr(src, a);
98 _pthread_attr_getguardsize(src, &u);
99 _pthread_attr_setguardsize(dst, u);
105 return (&_pthread_create != NULL);
109 __sigev_thread_init(void)
111 static int inited = 0;
112 pthread_mutexattr_t mattr;
115 _pthread_mutexattr_init(&mattr);
116 _pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
117 sigev_list_mtx = malloc(sizeof(pthread_mutex_t));
118 _pthread_mutex_init(sigev_list_mtx, &mattr);
119 _pthread_mutexattr_destroy(&mattr);
121 for (i = 0; i < HASH_QUEUES; ++i)
122 LIST_INIT(&sigev_hash[i]);
123 LIST_INIT(&sigev_all);
124 LIST_INIT(&sigev_threads);
125 sigev_default_thread = NULL;
126 if (atfork_registered == 0) {
128 __sigev_fork_prepare,
131 atfork_registered = 1;
134 _pthread_attr_init(&sigev_default_attr);
135 _pthread_attr_setscope(&sigev_default_attr,
136 PTHREAD_SCOPE_SYSTEM);
137 _pthread_attr_setdetachstate(&sigev_default_attr,
138 PTHREAD_CREATE_DETACHED);
141 sigev_default_thread = sigev_thread_create(0);
145 __sigev_check_init(void)
150 _pthread_once(&sigev_once, __sigev_thread_init);
151 return (sigev_default_thread != NULL) ? 0 : -1;
155 __sigev_fork_prepare(void)
160 __sigev_fork_parent(void)
165 __sigev_fork_child(void)
168 * This is a hack, the thread libraries really should
169 * check if the handlers were already registered in
172 atfork_registered = 1;
173 memcpy(&sigev_once, &sigev_once_default, sizeof(sigev_once));
174 __sigev_thread_init();
178 __sigev_list_lock(void)
180 _pthread_mutex_lock(sigev_list_mtx);
184 __sigev_list_unlock(void)
186 _pthread_mutex_unlock(sigev_list_mtx);
190 __sigev_alloc(int type, const struct sigevent *evp, struct sigev_node *prev,
193 struct sigev_node *sn;
195 sn = calloc(1, sizeof(*sn));
197 sn->sn_value = evp->sigev_value;
198 sn->sn_func = evp->sigev_notify_function;
199 sn->sn_gen = atomic_fetch_add_explicit(&sigev_generation, 1,
200 memory_order_relaxed);
202 _pthread_attr_init(&sn->sn_attr);
203 _pthread_attr_setdetachstate(&sn->sn_attr, PTHREAD_CREATE_DETACHED);
204 if (evp->sigev_notify_attributes)
205 attrcopy(evp->sigev_notify_attributes, &sn->sn_attr);
208 prev->sn_tn->tn_refcount++;
209 __sigev_list_unlock();
210 sn->sn_tn = prev->sn_tn;
212 sn->sn_tn = sigev_thread_create(usedefault);
213 if (sn->sn_tn == NULL) {
214 _pthread_attr_destroy(&sn->sn_attr);
224 __sigev_get_sigevent(struct sigev_node *sn, struct sigevent *newevp,
228 * Build a new sigevent, and tell kernel to deliver SIGLIBRT
229 * signal to the new thread.
231 newevp->sigev_notify = SIGEV_THREAD_ID;
232 newevp->sigev_signo = SIGLIBRT;
233 newevp->sigev_notify_thread_id = (lwpid_t)sn->sn_tn->tn_lwpid;
234 newevp->sigev_value.sival_ptr = (void *)id;
238 __sigev_free(struct sigev_node *sn)
240 _pthread_attr_destroy(&sn->sn_attr);
245 __sigev_find(int type, sigev_id_t id)
247 struct sigev_node *sn;
248 int chain = HASH(type, id);
250 LIST_FOREACH(sn, &sigev_hash[chain], sn_link) {
251 if (sn->sn_type == type && sn->sn_id == id)
258 __sigev_register(struct sigev_node *sn)
260 int chain = HASH(sn->sn_type, sn->sn_id);
262 LIST_INSERT_HEAD(&sigev_hash[chain], sn, sn_link);
267 __sigev_delete(int type, sigev_id_t id)
269 struct sigev_node *sn;
271 sn = __sigev_find(type, id);
273 return (__sigev_delete_node(sn));
278 __sigev_delete_node(struct sigev_node *sn)
280 LIST_REMOVE(sn, sn_link);
282 if (--sn->sn_tn->tn_refcount == 0)
283 _pthread_kill(sn->sn_tn->tn_thread, SIGLIBRT);
284 if (sn->sn_flags & SNF_WORKING)
285 sn->sn_flags |= SNF_REMOVED;
292 sigev_get_id(siginfo_t *si)
294 switch(si->si_code) {
296 return (si->si_timerid);
300 return (sigev_id_t)si->si_value.sival_ptr;
305 static struct sigev_thread *
306 sigev_thread_create(int usedefault)
308 struct sigev_thread *tn;
312 if (usedefault && sigev_default_thread) {
314 sigev_default_thread->tn_refcount++;
315 __sigev_list_unlock();
316 return (sigev_default_thread);
319 tn = malloc(sizeof(*tn));
323 _pthread_cond_init(&tn->tn_cv, NULL);
327 LIST_INSERT_HEAD(&sigev_threads, tn, tn_link);
328 __sigev_list_unlock();
330 sigfillset(&set); /* SIGLIBRT is masked. */
331 sigdelset(&set, SIGBUS);
332 sigdelset(&set, SIGILL);
333 sigdelset(&set, SIGFPE);
334 sigdelset(&set, SIGSEGV);
335 sigdelset(&set, SIGTRAP);
336 _sigprocmask(SIG_SETMASK, &set, &oset);
337 ret = _pthread_create(&tn->tn_thread, &sigev_default_attr,
338 sigev_service_loop, tn);
339 _sigprocmask(SIG_SETMASK, &oset, NULL);
343 LIST_REMOVE(tn, tn_link);
344 __sigev_list_unlock();
348 /* wait the thread to get its lwpid */
351 while (tn->tn_lwpid == -1)
352 _pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
353 __sigev_list_unlock();
359 * The thread receives notification from kernel and creates
360 * a thread to call user callback function.
363 sigev_service_loop(void *arg)
369 struct sigev_thread *tn;
370 struct sigev_node *sn;
376 thr_self(&tn->tn_lwpid);
378 _pthread_cond_broadcast(&tn->tn_cv);
379 __sigev_list_unlock();
382 sigaddset(&set, SIGLIBRT);
384 ret = sigwaitinfo(&set, &si);
387 if (tn->tn_refcount == 0) {
388 LIST_REMOVE(tn, tn_link);
389 __sigev_list_unlock();
395 __sigev_list_unlock();
399 id = sigev_get_id(&si);
400 sn = __sigev_find(si.si_code, id);
402 __sigev_list_unlock();
407 if (sn->sn_flags & SNF_SYNC)
411 sn->sn_flags |= SNF_WORKING;
412 __sigev_list_unlock();
414 ret = _pthread_create(&td, &sn->sn_attr, worker_routine, sn);
417 warnc(ret, "%s:%s failed to create thread.\n",
421 sn->sn_flags &= ~SNF_WORKING;
422 if (sn->sn_flags & SNF_REMOVED)
424 __sigev_list_unlock();
425 } else if (tn->tn_cur) {
428 _pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
429 __sigev_list_unlock();
436 * newly created worker thread to call user callback function.
439 worker_routine(void *arg)
441 struct sigev_node *sn = arg;
443 pthread_cleanup_push(worker_cleanup, sn);
445 pthread_cleanup_pop(1);
450 /* clean up a notification after dispatch. */
452 worker_cleanup(void *arg)
454 struct sigev_node *sn = arg;
457 if (sn->sn_flags & SNF_SYNC) {
458 sn->sn_tn->tn_cur = NULL;
459 _pthread_cond_broadcast(&sn->sn_tn->tn_cv);
461 if (sn->sn_flags & SNF_REMOVED)
464 sn->sn_flags &= ~SNF_WORKING;
465 __sigev_list_unlock();