2017-11-26 02:00:33 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2006-03-01 06:37:34 +00:00
|
|
|
* Copyright (c) 2005 David Xu <davidxu@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "namespace.h"
|
|
|
|
#include <err.h>
|
2006-03-04 00:18:19 +00:00
|
|
|
#include <errno.h>
|
2006-03-01 06:37:34 +00:00
|
|
|
#include <ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
2013-06-30 08:59:33 +00:00
|
|
|
#include <stdatomic.h>
|
2006-03-01 06:37:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "un-namespace.h"
|
|
|
|
|
|
|
|
#include "sigev_thread.h"
|
|
|
|
|
|
|
|
LIST_HEAD(sigev_list_head, sigev_node);
|
|
|
|
#define HASH_QUEUES 17
|
|
|
|
#define HASH(t, id) ((((id) << 3) + (t)) % HASH_QUEUES)
|
2006-03-04 00:18:19 +00:00
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
static struct sigev_list_head sigev_hash[HASH_QUEUES];
|
|
|
|
static struct sigev_list_head sigev_all;
|
2006-03-07 08:28:07 +00:00
|
|
|
static LIST_HEAD(,sigev_thread) sigev_threads;
|
2013-06-30 08:59:33 +00:00
|
|
|
static atomic_int sigev_generation;
|
2006-03-01 06:37:34 +00:00
|
|
|
static pthread_mutex_t *sigev_list_mtx;
|
2006-03-04 00:18:19 +00:00
|
|
|
static pthread_once_t sigev_once = PTHREAD_ONCE_INIT;
|
|
|
|
static pthread_once_t sigev_once_default = PTHREAD_ONCE_INIT;
|
2006-03-07 08:28:07 +00:00
|
|
|
static struct sigev_thread *sigev_default_thread;
|
2006-03-01 06:37:34 +00:00
|
|
|
static pthread_attr_t sigev_default_attr;
|
2006-03-04 00:18:19 +00:00
|
|
|
static int atfork_registered;
|
2006-03-07 08:28:07 +00:00
|
|
|
|
|
|
|
static void __sigev_fork_prepare(void);
|
|
|
|
static void __sigev_fork_parent(void);
|
|
|
|
static void __sigev_fork_child(void);
|
|
|
|
static struct sigev_thread *sigev_thread_create(int);
|
|
|
|
static void *sigev_service_loop(void *);
|
|
|
|
static void *worker_routine(void *);
|
|
|
|
static void worker_cleanup(void *);
|
2006-03-01 06:37:34 +00:00
|
|
|
|
2006-03-29 04:20:53 +00:00
|
|
|
#pragma weak _pthread_create
|
2006-03-01 06:37:34 +00:00
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
static void
|
|
|
|
attrcopy(pthread_attr_t *src, pthread_attr_t *dst)
|
2006-03-01 06:37:34 +00:00
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
struct sched_param sched;
|
|
|
|
void *a;
|
|
|
|
size_t u;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
_pthread_attr_getschedpolicy(src, &v);
|
|
|
|
_pthread_attr_setschedpolicy(dst, v);
|
|
|
|
|
|
|
|
_pthread_attr_getinheritsched(src, &v);
|
|
|
|
_pthread_attr_setinheritsched(dst, v);
|
|
|
|
|
|
|
|
_pthread_attr_getschedparam(src, &sched);
|
|
|
|
_pthread_attr_setschedparam(dst, &sched);
|
|
|
|
|
|
|
|
_pthread_attr_getscope(src, &v);
|
|
|
|
_pthread_attr_setscope(dst, v);
|
|
|
|
|
|
|
|
_pthread_attr_getstacksize(src, &u);
|
|
|
|
_pthread_attr_setstacksize(dst, u);
|
|
|
|
|
|
|
|
_pthread_attr_getstackaddr(src, &a);
|
|
|
|
_pthread_attr_setstackaddr(src, a);
|
|
|
|
|
|
|
|
_pthread_attr_getguardsize(src, &u);
|
|
|
|
_pthread_attr_setguardsize(dst, u);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
have_threads(void)
|
|
|
|
{
|
2006-03-29 04:20:53 +00:00
|
|
|
return (&_pthread_create != NULL);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__sigev_thread_init(void)
|
|
|
|
{
|
2006-03-04 00:18:19 +00:00
|
|
|
static int inited = 0;
|
2006-03-07 08:28:07 +00:00
|
|
|
pthread_mutexattr_t mattr;
|
2006-03-01 06:37:34 +00:00
|
|
|
int i;
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_mutexattr_init(&mattr);
|
|
|
|
_pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
|
2006-03-01 06:37:34 +00:00
|
|
|
sigev_list_mtx = malloc(sizeof(pthread_mutex_t));
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_mutex_init(sigev_list_mtx, &mattr);
|
|
|
|
_pthread_mutexattr_destroy(&mattr);
|
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
for (i = 0; i < HASH_QUEUES; ++i)
|
|
|
|
LIST_INIT(&sigev_hash[i]);
|
|
|
|
LIST_INIT(&sigev_all);
|
2006-03-07 08:28:07 +00:00
|
|
|
LIST_INIT(&sigev_threads);
|
2006-03-04 00:18:19 +00:00
|
|
|
sigev_default_thread = NULL;
|
|
|
|
if (atfork_registered == 0) {
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_atfork(
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_fork_prepare,
|
|
|
|
__sigev_fork_parent,
|
|
|
|
__sigev_fork_child);
|
|
|
|
atfork_registered = 1;
|
|
|
|
}
|
|
|
|
if (!inited) {
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_attr_init(&sigev_default_attr);
|
|
|
|
_pthread_attr_setscope(&sigev_default_attr,
|
2006-03-04 00:18:19 +00:00
|
|
|
PTHREAD_SCOPE_SYSTEM);
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_attr_setdetachstate(&sigev_default_attr,
|
2006-03-01 06:37:34 +00:00
|
|
|
PTHREAD_CREATE_DETACHED);
|
2006-03-04 00:18:19 +00:00
|
|
|
inited = 1;
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
2006-03-07 08:28:07 +00:00
|
|
|
sigev_default_thread = sigev_thread_create(0);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__sigev_check_init(void)
|
|
|
|
{
|
|
|
|
if (!have_threads())
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
_pthread_once(&sigev_once, __sigev_thread_init);
|
2006-03-04 00:18:19 +00:00
|
|
|
return (sigev_default_thread != NULL) ? 0 : -1;
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 00:18:19 +00:00
|
|
|
static void
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_fork_prepare(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-03-04 00:18:19 +00:00
|
|
|
static void
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_fork_parent(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-03-04 00:18:19 +00:00
|
|
|
static void
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_fork_child(void)
|
|
|
|
{
|
2006-03-04 00:18:19 +00:00
|
|
|
/*
|
|
|
|
* This is a hack, the thread libraries really should
|
|
|
|
* check if the handlers were already registered in
|
|
|
|
* pthread_atfork().
|
|
|
|
*/
|
|
|
|
atfork_registered = 1;
|
|
|
|
memcpy(&sigev_once, &sigev_once_default, sizeof(sigev_once));
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_thread_init();
|
|
|
|
}
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
void
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_list_lock(void)
|
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_mutex_lock(sigev_list_mtx);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
void
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_list_unlock(void)
|
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_mutex_unlock(sigev_list_mtx);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sigev_node *
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_alloc(int type, const struct sigevent *evp, struct sigev_node *prev,
|
2006-03-07 08:28:07 +00:00
|
|
|
int usedefault)
|
2006-03-01 06:37:34 +00:00
|
|
|
{
|
|
|
|
struct sigev_node *sn;
|
|
|
|
|
|
|
|
sn = calloc(1, sizeof(*sn));
|
|
|
|
if (sn != NULL) {
|
|
|
|
sn->sn_value = evp->sigev_value;
|
2006-03-07 08:28:07 +00:00
|
|
|
sn->sn_func = evp->sigev_notify_function;
|
2013-06-30 08:59:33 +00:00
|
|
|
sn->sn_gen = atomic_fetch_add_explicit(&sigev_generation, 1,
|
|
|
|
memory_order_relaxed);
|
2006-03-07 08:28:07 +00:00
|
|
|
sn->sn_type = type;
|
|
|
|
_pthread_attr_init(&sn->sn_attr);
|
|
|
|
_pthread_attr_setdetachstate(&sn->sn_attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
if (evp->sigev_notify_attributes)
|
|
|
|
attrcopy(evp->sigev_notify_attributes, &sn->sn_attr);
|
|
|
|
if (prev) {
|
|
|
|
__sigev_list_lock();
|
|
|
|
prev->sn_tn->tn_refcount++;
|
|
|
|
__sigev_list_unlock();
|
|
|
|
sn->sn_tn = prev->sn_tn;
|
|
|
|
} else {
|
|
|
|
sn->sn_tn = sigev_thread_create(usedefault);
|
|
|
|
if (sn->sn_tn == NULL) {
|
|
|
|
_pthread_attr_destroy(&sn->sn_attr);
|
|
|
|
free(sn);
|
|
|
|
sn = NULL;
|
|
|
|
}
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (sn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__sigev_get_sigevent(struct sigev_node *sn, struct sigevent *newevp,
|
|
|
|
sigev_id_t id)
|
|
|
|
{
|
|
|
|
/*
|
2012-03-26 19:12:09 +00:00
|
|
|
* Build a new sigevent, and tell kernel to deliver SIGLIBRT
|
2006-03-01 06:37:34 +00:00
|
|
|
* signal to the new thread.
|
|
|
|
*/
|
|
|
|
newevp->sigev_notify = SIGEV_THREAD_ID;
|
2012-03-26 19:12:09 +00:00
|
|
|
newevp->sigev_signo = SIGLIBRT;
|
2006-03-01 06:37:34 +00:00
|
|
|
newevp->sigev_notify_thread_id = (lwpid_t)sn->sn_tn->tn_lwpid;
|
|
|
|
newevp->sigev_value.sival_ptr = (void *)id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__sigev_free(struct sigev_node *sn)
|
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_attr_destroy(&sn->sn_attr);
|
2006-03-01 06:37:34 +00:00
|
|
|
free(sn);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sigev_node *
|
|
|
|
__sigev_find(int type, sigev_id_t id)
|
|
|
|
{
|
|
|
|
struct sigev_node *sn;
|
|
|
|
int chain = HASH(type, id);
|
|
|
|
|
|
|
|
LIST_FOREACH(sn, &sigev_hash[chain], sn_link) {
|
|
|
|
if (sn->sn_type == type && sn->sn_id == id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (sn);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__sigev_register(struct sigev_node *sn)
|
|
|
|
{
|
|
|
|
int chain = HASH(sn->sn_type, sn->sn_id);
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&sigev_hash[chain], sn, sn_link);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__sigev_delete(int type, sigev_id_t id)
|
|
|
|
{
|
|
|
|
struct sigev_node *sn;
|
|
|
|
|
|
|
|
sn = __sigev_find(type, id);
|
|
|
|
if (sn != NULL)
|
|
|
|
return (__sigev_delete_node(sn));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__sigev_delete_node(struct sigev_node *sn)
|
|
|
|
{
|
|
|
|
LIST_REMOVE(sn, sn_link);
|
|
|
|
|
2006-03-04 00:18:19 +00:00
|
|
|
if (--sn->sn_tn->tn_refcount == 0)
|
2012-03-26 19:12:09 +00:00
|
|
|
_pthread_kill(sn->sn_tn->tn_thread, SIGLIBRT);
|
2006-03-01 06:37:34 +00:00
|
|
|
if (sn->sn_flags & SNF_WORKING)
|
|
|
|
sn->sn_flags |= SNF_REMOVED;
|
2006-03-07 08:28:07 +00:00
|
|
|
else
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_free(sn);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
static sigev_id_t
|
2006-03-01 06:37:34 +00:00
|
|
|
sigev_get_id(siginfo_t *si)
|
|
|
|
{
|
|
|
|
switch(si->si_code) {
|
|
|
|
case SI_TIMER:
|
|
|
|
return (si->si_timerid);
|
|
|
|
case SI_MESGQ:
|
|
|
|
return (si->si_mqd);
|
|
|
|
case SI_ASYNCIO:
|
|
|
|
return (sigev_id_t)si->si_value.sival_ptr;
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
static struct sigev_thread *
|
|
|
|
sigev_thread_create(int usedefault)
|
2006-03-01 06:37:34 +00:00
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
struct sigev_thread *tn;
|
2006-03-10 08:01:23 +00:00
|
|
|
sigset_t set, oset;
|
2006-03-01 06:37:34 +00:00
|
|
|
int ret;
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
if (usedefault && sigev_default_thread) {
|
|
|
|
__sigev_list_lock();
|
2006-03-04 00:18:19 +00:00
|
|
|
sigev_default_thread->tn_refcount++;
|
2006-03-07 08:28:07 +00:00
|
|
|
__sigev_list_unlock();
|
|
|
|
return (sigev_default_thread);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
2006-03-04 00:18:19 +00:00
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
tn = malloc(sizeof(*tn));
|
|
|
|
tn->tn_cur = NULL;
|
2006-03-04 00:18:19 +00:00
|
|
|
tn->tn_lwpid = -1;
|
|
|
|
tn->tn_refcount = 1;
|
2006-03-07 08:28:07 +00:00
|
|
|
_pthread_cond_init(&tn->tn_cv, NULL);
|
|
|
|
|
|
|
|
/* for debug */
|
|
|
|
__sigev_list_lock();
|
|
|
|
LIST_INSERT_HEAD(&sigev_threads, tn, tn_link);
|
|
|
|
__sigev_list_unlock();
|
|
|
|
|
2012-03-26 19:12:09 +00:00
|
|
|
sigfillset(&set); /* SIGLIBRT is masked. */
|
2006-03-10 08:01:23 +00:00
|
|
|
sigdelset(&set, SIGBUS);
|
|
|
|
sigdelset(&set, SIGILL);
|
|
|
|
sigdelset(&set, SIGFPE);
|
|
|
|
sigdelset(&set, SIGSEGV);
|
|
|
|
sigdelset(&set, SIGTRAP);
|
|
|
|
_sigprocmask(SIG_SETMASK, &set, &oset);
|
2006-03-29 04:20:53 +00:00
|
|
|
ret = _pthread_create(&tn->tn_thread, &sigev_default_attr,
|
2006-03-07 08:28:07 +00:00
|
|
|
sigev_service_loop, tn);
|
2006-03-10 08:01:23 +00:00
|
|
|
_sigprocmask(SIG_SETMASK, &oset, NULL);
|
2006-03-07 08:28:07 +00:00
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
if (ret != 0) {
|
2006-03-07 08:28:07 +00:00
|
|
|
__sigev_list_lock();
|
|
|
|
LIST_REMOVE(tn, tn_link);
|
|
|
|
__sigev_list_unlock();
|
2006-03-01 06:37:34 +00:00
|
|
|
free(tn);
|
|
|
|
tn = NULL;
|
|
|
|
} else {
|
|
|
|
/* wait the thread to get its lwpid */
|
|
|
|
|
|
|
|
__sigev_list_lock();
|
2006-03-07 08:28:07 +00:00
|
|
|
while (tn->tn_lwpid == -1)
|
|
|
|
_pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
|
2006-03-01 06:37:34 +00:00
|
|
|
__sigev_list_unlock();
|
|
|
|
}
|
2006-03-07 08:28:07 +00:00
|
|
|
return (tn);
|
2006-03-01 06:37:34 +00:00
|
|
|
}
|
|
|
|
|
2006-03-04 00:18:19 +00:00
|
|
|
/*
|
2006-03-07 08:28:07 +00:00
|
|
|
* The thread receives notification from kernel and creates
|
|
|
|
* a thread to call user callback function.
|
2006-03-04 00:18:19 +00:00
|
|
|
*/
|
2006-03-01 06:37:34 +00:00
|
|
|
static void *
|
|
|
|
sigev_service_loop(void *arg)
|
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
static int failure;
|
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
siginfo_t si;
|
|
|
|
sigset_t set;
|
2006-03-07 08:28:07 +00:00
|
|
|
struct sigev_thread *tn;
|
2006-03-01 06:37:34 +00:00
|
|
|
struct sigev_node *sn;
|
|
|
|
sigev_id_t id;
|
2006-03-07 08:28:07 +00:00
|
|
|
pthread_t td;
|
2006-03-04 00:18:19 +00:00
|
|
|
int ret;
|
2006-03-01 06:37:34 +00:00
|
|
|
|
|
|
|
tn = arg;
|
|
|
|
thr_self(&tn->tn_lwpid);
|
2006-03-07 08:28:07 +00:00
|
|
|
__sigev_list_lock();
|
|
|
|
_pthread_cond_broadcast(&tn->tn_cv);
|
|
|
|
__sigev_list_unlock();
|
2006-03-01 06:37:34 +00:00
|
|
|
|
|
|
|
sigemptyset(&set);
|
2012-03-26 19:12:09 +00:00
|
|
|
sigaddset(&set, SIGLIBRT);
|
2006-03-01 06:37:34 +00:00
|
|
|
for (;;) {
|
2006-03-04 00:18:19 +00:00
|
|
|
ret = sigwaitinfo(&set, &si);
|
2006-03-07 08:28:07 +00:00
|
|
|
|
|
|
|
__sigev_list_lock();
|
2006-03-04 00:18:19 +00:00
|
|
|
if (tn->tn_refcount == 0) {
|
2006-03-07 08:28:07 +00:00
|
|
|
LIST_REMOVE(tn, tn_link);
|
|
|
|
__sigev_list_unlock();
|
2006-03-04 00:18:19 +00:00
|
|
|
free(tn);
|
|
|
|
break;
|
|
|
|
}
|
2006-03-07 08:28:07 +00:00
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
__sigev_list_unlock();
|
2006-03-01 06:37:34 +00:00
|
|
|
continue;
|
2006-03-07 08:28:07 +00:00
|
|
|
}
|
|
|
|
|
2006-03-01 06:37:34 +00:00
|
|
|
id = sigev_get_id(&si);
|
|
|
|
sn = __sigev_find(si.si_code, id);
|
2006-03-07 08:28:07 +00:00
|
|
|
if (sn == NULL) {
|
2006-03-01 23:38:53 +00:00
|
|
|
__sigev_list_unlock();
|
2006-03-07 08:28:07 +00:00
|
|
|
continue;
|
2006-03-01 23:38:53 +00:00
|
|
|
}
|
2006-03-07 08:28:07 +00:00
|
|
|
|
|
|
|
sn->sn_info = si;
|
|
|
|
if (sn->sn_flags & SNF_SYNC)
|
|
|
|
tn->tn_cur = sn;
|
|
|
|
else
|
|
|
|
tn->tn_cur = NULL;
|
|
|
|
sn->sn_flags |= SNF_WORKING;
|
|
|
|
__sigev_list_unlock();
|
2006-03-04 00:18:19 +00:00
|
|
|
|
2006-03-29 04:20:53 +00:00
|
|
|
ret = _pthread_create(&td, &sn->sn_attr, worker_routine, sn);
|
2006-03-07 08:28:07 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
if (failure++ < 5)
|
|
|
|
warnc(ret, "%s:%s failed to create thread.\n",
|
|
|
|
__FILE__, __func__);
|
2006-03-04 00:18:19 +00:00
|
|
|
|
|
|
|
__sigev_list_lock();
|
2006-03-07 08:28:07 +00:00
|
|
|
sn->sn_flags &= ~SNF_WORKING;
|
|
|
|
if (sn->sn_flags & SNF_REMOVED)
|
|
|
|
__sigev_free(sn);
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_list_unlock();
|
2006-03-07 08:28:07 +00:00
|
|
|
} else if (tn->tn_cur) {
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_list_lock();
|
2006-03-07 08:28:07 +00:00
|
|
|
while (tn->tn_cur)
|
|
|
|
_pthread_cond_wait(&tn->tn_cv, sigev_list_mtx);
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_list_unlock();
|
|
|
|
}
|
|
|
|
}
|
2006-03-07 08:28:07 +00:00
|
|
|
return (0);
|
2006-03-04 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-07 08:28:07 +00:00
|
|
|
* newly created worker thread to call user callback function.
|
2006-03-04 00:18:19 +00:00
|
|
|
*/
|
|
|
|
static void *
|
2006-03-07 08:28:07 +00:00
|
|
|
worker_routine(void *arg)
|
2006-03-04 00:18:19 +00:00
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
struct sigev_node *sn = arg;
|
2006-03-04 00:18:19 +00:00
|
|
|
|
2009-11-18 01:35:36 +00:00
|
|
|
pthread_cleanup_push(worker_cleanup, sn);
|
2006-03-07 08:28:07 +00:00
|
|
|
sn->sn_dispatch(sn);
|
2009-11-18 01:35:36 +00:00
|
|
|
pthread_cleanup_pop(1);
|
2006-03-04 00:18:19 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-03-07 08:28:07 +00:00
|
|
|
/* clean up a notification after dispatch. */
|
2006-03-04 00:18:19 +00:00
|
|
|
static void
|
|
|
|
worker_cleanup(void *arg)
|
|
|
|
{
|
2006-03-07 08:28:07 +00:00
|
|
|
struct sigev_node *sn = arg;
|
2006-03-04 00:18:19 +00:00
|
|
|
|
|
|
|
__sigev_list_lock();
|
2006-03-07 08:28:07 +00:00
|
|
|
if (sn->sn_flags & SNF_SYNC) {
|
|
|
|
sn->sn_tn->tn_cur = NULL;
|
|
|
|
_pthread_cond_broadcast(&sn->sn_tn->tn_cv);
|
|
|
|
}
|
2006-03-04 00:18:19 +00:00
|
|
|
if (sn->sn_flags & SNF_REMOVED)
|
|
|
|
__sigev_free(sn);
|
2006-03-07 08:28:07 +00:00
|
|
|
else
|
|
|
|
sn->sn_flags &= ~SNF_WORKING;
|
2006-03-04 00:18:19 +00:00
|
|
|
__sigev_list_unlock();
|
|
|
|
}
|