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>
31 #include <machine/atomic.h>
33 #include "namespace.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 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_fetchadd_int(&sigev_generation, 1);
201 _pthread_attr_init(&sn->sn_attr);
202 _pthread_attr_setdetachstate(&sn->sn_attr, PTHREAD_CREATE_DETACHED);
203 if (evp->sigev_notify_attributes)
204 attrcopy(evp->sigev_notify_attributes, &sn->sn_attr);
207 prev->sn_tn->tn_refcount++;
208 __sigev_list_unlock();
209 sn->sn_tn = prev->sn_tn;
211 sn->sn_tn = sigev_thread_create(usedefault);
212 if (sn->sn_tn == NULL) {
213 _pthread_attr_destroy(&sn->sn_attr);
223 __sigev_get_sigevent(struct sigev_node *sn, struct sigevent *newevp,
227 * Build a new sigevent, and tell kernel to deliver SIGSERVICE
228 * signal to the new thread.
230 newevp->sigev_notify = SIGEV_THREAD_ID;
231 newevp->sigev_signo = SIGSERVICE;
232 newevp->sigev_notify_thread_id = (lwpid_t)sn->sn_tn->tn_lwpid;
233 newevp->sigev_value.sival_ptr = (void *)id;
237 __sigev_free(struct sigev_node *sn)
239 _pthread_attr_destroy(&sn->sn_attr);
244 __sigev_find(int type, sigev_id_t id)
246 struct sigev_node *sn;
247 int chain = HASH(type, id);
249 LIST_FOREACH(sn, &sigev_hash[chain], sn_link) {
250 if (sn->sn_type == type && sn->sn_id == id)
257 __sigev_register(struct sigev_node *sn)
259 int chain = HASH(sn->sn_type, sn->sn_id);
261 LIST_INSERT_HEAD(&sigev_hash[chain], sn, sn_link);
266 __sigev_delete(int type, sigev_id_t id)
268 struct sigev_node *sn;
270 sn = __sigev_find(type, id);
272 return (__sigev_delete_node(sn));
277 __sigev_delete_node(struct sigev_node *sn)
279 LIST_REMOVE(sn, sn_link);
281 if (--sn->sn_tn->tn_refcount == 0)
282 _pthread_kill(sn->sn_tn->tn_thread, SIGSERVICE);
283 if (sn->sn_flags & SNF_WORKING)
284 sn->sn_flags |= SNF_REMOVED;
291 sigev_get_id(siginfo_t *si)
293 switch(si->si_code) {
295 return (si->si_timerid);
299 return (sigev_id_t)si->si_value.sival_ptr;
304 static struct sigev_thread *
305 sigev_thread_create(int usedefault)
307 struct sigev_thread *tn;
311 if (usedefault && sigev_default_thread) {
313 sigev_default_thread->tn_refcount++;
314 __sigev_list_unlock();
315 return (sigev_default_thread);
318 tn = malloc(sizeof(*tn));
322 _pthread_cond_init(&tn->tn_cv, NULL);
326 LIST_INSERT_HEAD(&sigev_threads, tn, tn_link);
327 __sigev_list_unlock();
329 sigfillset(&set); /* SIGSERVICE is masked. */
330 sigdelset(&set, SIGBUS);
331 sigdelset(&set, SIGILL);
332 sigdelset(&set, SIGFPE);
333 sigdelset(&set, SIGSEGV);
334 sigdelset(&set, SIGTRAP);
335 _sigprocmask(SIG_SETMASK, &set, &oset);
336 ret = _pthread_create(&tn->tn_thread, &sigev_default_attr,
337 sigev_service_loop, tn);
338 _sigprocmask(SIG_SETMASK, &oset, NULL);
342 LIST_REMOVE(tn, tn_link);
343 __sigev_list_unlock();
347 /* wait the thread to get its lwpid */
350 while (tn->tn_lwpid == -1)
351 _pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
352 __sigev_list_unlock();
358 * The thread receives notification from kernel and creates
359 * a thread to call user callback function.
362 sigev_service_loop(void *arg)
368 struct sigev_thread *tn;
369 struct sigev_node *sn;
375 thr_self(&tn->tn_lwpid);
377 _pthread_cond_broadcast(&tn->tn_cv);
378 __sigev_list_unlock();
381 sigaddset(&set, SIGSERVICE);
383 ret = sigwaitinfo(&set, &si);
386 if (tn->tn_refcount == 0) {
387 LIST_REMOVE(tn, tn_link);
388 __sigev_list_unlock();
394 __sigev_list_unlock();
398 id = sigev_get_id(&si);
399 sn = __sigev_find(si.si_code, id);
401 __sigev_list_unlock();
406 if (sn->sn_flags & SNF_SYNC)
410 sn->sn_flags |= SNF_WORKING;
411 __sigev_list_unlock();
413 ret = _pthread_create(&td, &sn->sn_attr, worker_routine, sn);
416 warnc(ret, "%s:%s failed to create thread.\n",
420 sn->sn_flags &= ~SNF_WORKING;
421 if (sn->sn_flags & SNF_REMOVED)
423 __sigev_list_unlock();
424 } else if (tn->tn_cur) {
427 _pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
428 __sigev_list_unlock();
435 * newly created worker thread to call user callback function.
438 worker_routine(void *arg)
440 struct sigev_node *sn = arg;
442 _pthread_cleanup_push(worker_cleanup, sn);
444 _pthread_cleanup_pop(1);
449 /* clean up a notification after dispatch. */
451 worker_cleanup(void *arg)
453 struct sigev_node *sn = arg;
456 if (sn->sn_flags & SNF_SYNC) {
457 sn->sn_tn->tn_cur = NULL;
458 _pthread_cond_broadcast(&sn->sn_tn->tn_cv);
460 if (sn->sn_flags & SNF_REMOVED)
463 sn->sn_flags &= ~SNF_WORKING;
464 __sigev_list_unlock();