2007-03-02 21:53:51 +03:00
|
|
|
/* $NetBSD: pthread.c,v 1.64 2007/03/02 18:53:51 ad Exp $ */
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/*-
|
2007-02-22 01:31:38 +03:00
|
|
|
* Copyright (c) 2001, 2002, 2003, 2006, 2007 The NetBSD Foundation, Inc.
|
2003-01-18 13:32:11 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2006-12-23 08:14:46 +03:00
|
|
|
* by Nathan J. Williams and Andrew Doran.
|
2003-01-18 13:32:11 +03:00
|
|
|
*
|
|
|
|
* 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, 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-03-08 11:03:34 +03:00
|
|
|
#include <sys/cdefs.h>
|
2007-03-02 21:53:51 +03:00
|
|
|
__RCSID("$NetBSD: pthread.c,v 1.64 2007/03/02 18:53:51 ad Exp $");
|
2003-03-08 11:03:34 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <lwp.h>
|
|
|
|
#include <signal.h>
|
2003-02-15 07:34:40 +03:00
|
|
|
#include <stdio.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
#include <syslog.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <ucontext.h>
|
2003-02-15 07:34:40 +03:00
|
|
|
#include <unistd.h>
|
2004-03-14 04:19:41 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
2003-02-15 07:34:40 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <sched.h>
|
|
|
|
#include "pthread.h"
|
|
|
|
#include "pthread_int.h"
|
|
|
|
|
|
|
|
#ifdef PTHREAD_MAIN_DEBUG
|
|
|
|
#define SDPRINTF(x) DPRINTF(x)
|
|
|
|
#else
|
|
|
|
#define SDPRINTF(x)
|
|
|
|
#endif
|
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/* Maximum number of LWPs to unpark in one operation. */
|
|
|
|
#define PTHREAD__UNPARK_MAX 128
|
|
|
|
|
|
|
|
/* How many times to try acquiring spin locks on MP systems. */
|
2007-03-02 20:40:55 +03:00
|
|
|
#define PTHREAD__NSPINS 1024
|
2006-12-24 21:39:45 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
static void pthread__create_tramp(void *(*start)(void *), void *arg);
|
2004-08-12 14:54:13 +04:00
|
|
|
static void pthread__dead(pthread_t, pthread_t);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
int pthread__started;
|
|
|
|
|
2004-07-19 01:24:52 +04:00
|
|
|
pthread_spin_t pthread__allqueue_lock = __SIMPLELOCK_UNLOCKED;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct pthread_queue_t pthread__allqueue;
|
|
|
|
|
2004-07-19 01:24:52 +04:00
|
|
|
pthread_spin_t pthread__deadqueue_lock = __SIMPLELOCK_UNLOCKED;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct pthread_queue_t pthread__deadqueue;
|
2004-03-14 04:19:41 +03:00
|
|
|
struct pthread_queue_t *pthread__reidlequeue;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
static int nthreads;
|
|
|
|
static int nextthread;
|
2004-07-19 01:24:52 +04:00
|
|
|
static pthread_spin_t nextthread_lock = __SIMPLELOCK_UNLOCKED;
|
2003-01-18 13:32:11 +03:00
|
|
|
static pthread_attr_t pthread_default_attr;
|
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
enum {
|
|
|
|
DIAGASSERT_ABORT = 1<<0,
|
|
|
|
DIAGASSERT_STDERR = 1<<1,
|
|
|
|
DIAGASSERT_SYSLOG = 1<<2
|
|
|
|
};
|
2003-04-23 23:35:47 +04:00
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
static int pthread__diagassert = DIAGASSERT_ABORT | DIAGASSERT_STDERR;
|
2003-04-23 23:35:47 +04:00
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
int pthread__concurrency, pthread__maxconcurrency, pthread__nspins;
|
|
|
|
int pthread__unpark_max = PTHREAD__UNPARK_MAX;
|
2004-03-14 04:19:41 +03:00
|
|
|
|
2006-04-24 22:39:36 +04:00
|
|
|
int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
__strong_alias(__libc_thr_self,pthread_self)
|
2003-01-20 00:58:21 +03:00
|
|
|
__strong_alias(__libc_thr_create,pthread_create)
|
|
|
|
__strong_alias(__libc_thr_exit,pthread_exit)
|
2003-01-18 13:32:11 +03:00
|
|
|
__strong_alias(__libc_thr_errno,pthread__errno)
|
2003-07-19 01:57:26 +04:00
|
|
|
__strong_alias(__libc_thr_setcancelstate,pthread_setcancelstate)
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Static library kludge. Place a reference to a symbol any library
|
|
|
|
* file which does not already have a reference here.
|
|
|
|
*/
|
|
|
|
extern int pthread__cancel_stub_binder;
|
|
|
|
|
|
|
|
void *pthread__static_lib_binder[] = {
|
|
|
|
&pthread__cancel_stub_binder,
|
|
|
|
pthread_cond_init,
|
|
|
|
pthread_mutex_init,
|
|
|
|
pthread_rwlock_init,
|
|
|
|
pthread_barrier_init,
|
|
|
|
pthread_key_create,
|
2003-08-13 22:52:01 +04:00
|
|
|
pthread_setspecific,
|
2003-01-18 13:32:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This needs to be started by the library loading code, before main()
|
|
|
|
* gets to run, for various things that use the state of the initial thread
|
|
|
|
* to work properly (thread-specific data is an application-visible example;
|
|
|
|
* spinlock counts for mutexes is an internal example).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pthread_init(void)
|
|
|
|
{
|
|
|
|
pthread_t first;
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
char *p;
|
2006-12-24 21:39:45 +03:00
|
|
|
int i, mib[2], ncpu;
|
2004-03-14 04:19:41 +03:00
|
|
|
size_t len;
|
2003-01-18 13:32:11 +03:00
|
|
|
extern int __isthreaded;
|
|
|
|
|
2004-03-14 04:19:41 +03:00
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
|
|
|
|
len = sizeof(ncpu);
|
|
|
|
sysctl(mib, 2, &ncpu, &len, NULL, 0);
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Initialize locks first; they're needed elsewhere. */
|
2004-03-14 04:19:41 +03:00
|
|
|
pthread__lockprim_init(ncpu);
|
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/*
|
|
|
|
* Get number of CPUs, and maximum number of LWPs that can be
|
|
|
|
* unparked at once.
|
|
|
|
*/
|
|
|
|
if ((pthread__concurrency = ncpu) > 1)
|
|
|
|
pthread__nspins = PTHREAD__NSPINS;
|
|
|
|
else
|
|
|
|
pthread__nspins = 1;
|
2007-02-10 02:53:24 +03:00
|
|
|
i = (int)_lwp_unpark_all(NULL, 0, NULL);
|
|
|
|
if (i == -1)
|
|
|
|
err(1, "_lwp_unpark_all");
|
2006-12-24 21:39:45 +03:00
|
|
|
if (i < pthread__unpark_max)
|
|
|
|
pthread__unpark_max = i;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/* Basic data structure setup */
|
|
|
|
pthread_attr_init(&pthread_default_attr);
|
|
|
|
PTQ_INIT(&pthread__allqueue);
|
|
|
|
PTQ_INIT(&pthread__deadqueue);
|
2006-12-24 21:39:45 +03:00
|
|
|
nthreads = 1;
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Create the thread structure corresponding to main() */
|
|
|
|
pthread__initmain(&first);
|
|
|
|
pthread__initthread(first, first);
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
first->pt_state = PT_STATE_RUNNING;
|
2006-12-23 08:14:46 +03:00
|
|
|
first->pt_lid = _lwp_self();
|
2003-01-18 13:32:11 +03:00
|
|
|
PTQ_INSERT_HEAD(&pthread__allqueue, first, pt_allq);
|
|
|
|
|
|
|
|
/* Start subsystems */
|
|
|
|
PTHREAD_MD_INIT
|
|
|
|
#ifdef PTHREAD__DEBUG
|
2004-03-14 04:19:41 +03:00
|
|
|
pthread__debug_init(ncpu);
|
2003-01-18 13:32:11 +03:00
|
|
|
#endif
|
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
for (p = getenv("PTHREAD_DIAGASSERT"); p && *p; p++) {
|
|
|
|
switch (*p) {
|
|
|
|
case 'a':
|
|
|
|
pthread__diagassert |= DIAGASSERT_ABORT;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
pthread__diagassert &= ~DIAGASSERT_ABORT;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
pthread__diagassert |= DIAGASSERT_STDERR;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
pthread__diagassert &= ~DIAGASSERT_STDERR;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
pthread__diagassert |= DIAGASSERT_SYSLOG;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
pthread__diagassert &= ~DIAGASSERT_SYSLOG;
|
|
|
|
break;
|
|
|
|
}
|
2003-04-23 23:35:47 +04:00
|
|
|
}
|
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Tell libc that we're here and it should role-play accordingly. */
|
|
|
|
__isthreaded = 1;
|
|
|
|
}
|
|
|
|
|
2003-04-08 01:29:48 +04:00
|
|
|
static void
|
|
|
|
pthread__child_callback(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Clean up data structures that a forked child process might
|
|
|
|
* trip over. Note that if threads have been created (causing
|
|
|
|
* this handler to be registered) the standards say that the
|
|
|
|
* child will trigger undefined behavior if it makes any
|
|
|
|
* pthread_* calls (or any other calls that aren't
|
|
|
|
* async-signal-safe), so we don't really have to clean up
|
|
|
|
* much. Anything that permits some pthread_* calls to work is
|
|
|
|
* merely being polite.
|
|
|
|
*/
|
|
|
|
pthread__started = 0;
|
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2005-10-19 06:15:03 +04:00
|
|
|
static void
|
2003-01-18 13:32:11 +03:00
|
|
|
pthread__start(void)
|
|
|
|
{
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_t self;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self(); /* should be the "main()" thread */
|
|
|
|
|
2003-04-28 21:46:30 +04:00
|
|
|
/*
|
|
|
|
* Per-process timers are cleared by fork(); despite the
|
|
|
|
* various restrictions on fork() and threads, it's legal to
|
|
|
|
* fork() before creating any threads.
|
|
|
|
*/
|
2003-04-08 01:29:48 +04:00
|
|
|
pthread_atfork(NULL, NULL, pthread__child_callback);
|
2003-01-18 13:32:11 +03:00
|
|
|
SDPRINTF(("(pthread__start %p) Started.\n", self));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* General-purpose thread data structure sanitization. */
|
|
|
|
void
|
|
|
|
pthread__initthread(pthread_t self, pthread_t t)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
|
|
|
|
pthread_spinlock(self, &nextthread_lock);
|
|
|
|
id = nextthread;
|
|
|
|
nextthread++;
|
|
|
|
pthread_spinunlock(self, &nextthread_lock);
|
|
|
|
t->pt_num = id;
|
|
|
|
|
|
|
|
t->pt_magic = PT_MAGIC;
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_lockinit(&t->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
t->pt_spinlocks = 0;
|
|
|
|
t->pt_exitval = NULL;
|
|
|
|
t->pt_flags = 0;
|
|
|
|
t->pt_cancel = 0;
|
|
|
|
t->pt_errno = 0;
|
2006-12-23 08:14:46 +03:00
|
|
|
t->pt_state = PT_STATE_RUNNING;
|
|
|
|
|
|
|
|
pthread_lockinit(&t->pt_statelock);
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
PTQ_INIT(&t->pt_joiners);
|
|
|
|
pthread_lockinit(&t->pt_join_lock);
|
|
|
|
PTQ_INIT(&t->pt_cleanup_stack);
|
|
|
|
memset(&t->pt_specific, 0, sizeof(int) * PTHREAD_KEYS_MAX);
|
2003-02-27 01:02:48 +03:00
|
|
|
t->pt_name = NULL;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*startfunc)(void *), void *arg)
|
|
|
|
{
|
|
|
|
pthread_t self, newthread;
|
|
|
|
pthread_attr_t nattr;
|
2003-02-27 01:02:48 +03:00
|
|
|
struct pthread_attr_private *p;
|
2007-01-20 07:56:07 +03:00
|
|
|
char * volatile name;
|
2007-03-02 21:53:51 +03:00
|
|
|
int ret, flag;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
PTHREADD_ADD(PTHREADD_CREATE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's okay to check this without a lock because there can
|
|
|
|
* only be one thread before it becomes true.
|
|
|
|
*/
|
|
|
|
if (pthread__started == 0) {
|
|
|
|
pthread__start();
|
|
|
|
pthread__started = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr == NULL)
|
|
|
|
nattr = pthread_default_attr;
|
2003-01-29 17:03:08 +03:00
|
|
|
else if (attr->pta_magic == PT_ATTR_MAGIC)
|
2003-01-18 13:32:11 +03:00
|
|
|
nattr = *attr;
|
|
|
|
else
|
|
|
|
return EINVAL;
|
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
/* Fetch misc. attributes from the attr structure. */
|
2003-02-27 02:41:01 +03:00
|
|
|
name = NULL;
|
|
|
|
if ((p = nattr.pta_private) != NULL)
|
|
|
|
if (p->ptap_name[0] != '\0')
|
2003-02-27 01:02:48 +03:00
|
|
|
if ((name = strdup(p->ptap_name)) == NULL)
|
|
|
|
return ENOMEM;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
pthread_spinlock(self, &pthread__deadqueue_lock);
|
2007-02-15 18:39:33 +03:00
|
|
|
newthread = PTQ_FIRST(&pthread__deadqueue);
|
|
|
|
if (newthread != NULL) {
|
2007-02-22 01:25:57 +03:00
|
|
|
if ((newthread->pt_flags & PT_FLAG_DETACHED) != 0 &&
|
|
|
|
(_lwp_kill(newthread->pt_lid, 0) == 0 || errno != ESRCH))
|
2007-02-15 18:39:33 +03:00
|
|
|
newthread = NULL;
|
|
|
|
else
|
|
|
|
PTQ_REMOVE(&pthread__deadqueue, newthread, pt_allq);
|
|
|
|
}
|
|
|
|
pthread_spinunlock(self, &pthread__deadqueue_lock);
|
|
|
|
if (newthread == NULL) {
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Set up a stack and allocate space for a pthread_st. */
|
|
|
|
ret = pthread__stackalloc(&newthread);
|
2003-12-18 18:39:56 +03:00
|
|
|
if (ret != 0) {
|
|
|
|
if (name)
|
|
|
|
free(name);
|
2003-01-18 13:32:11 +03:00
|
|
|
return ret;
|
2003-12-18 18:39:56 +03:00
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Set up state. */
|
|
|
|
pthread__initthread(self, newthread);
|
|
|
|
newthread->pt_flags = nattr.pta_flags;
|
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
/* 3. Set up misc. attributes. */
|
|
|
|
newthread->pt_name = name;
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/*
|
2003-02-27 01:02:48 +03:00
|
|
|
* 4. Set up context.
|
2003-01-18 13:32:11 +03:00
|
|
|
*
|
|
|
|
* The pt_uc pointer points to a location safely below the
|
|
|
|
* stack start; this is arranged by pthread__stackalloc().
|
|
|
|
*/
|
|
|
|
_INITCONTEXT_U(newthread->pt_uc);
|
2003-06-07 01:06:07 +04:00
|
|
|
#ifdef PTHREAD_MACHINE_HAS_ID_REGISTER
|
|
|
|
pthread__uc_id(newthread->pt_uc) = newthread;
|
|
|
|
#endif
|
2003-01-18 13:32:11 +03:00
|
|
|
newthread->pt_uc->uc_stack = newthread->pt_stack;
|
|
|
|
newthread->pt_uc->uc_link = NULL;
|
|
|
|
makecontext(newthread->pt_uc, pthread__create_tramp, 2,
|
|
|
|
startfunc, arg);
|
|
|
|
|
2007-01-16 07:19:02 +03:00
|
|
|
/* 5. Add to list of all threads. */
|
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
PTQ_INSERT_HEAD(&pthread__allqueue, newthread, pt_allq);
|
|
|
|
nthreads++;
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
|
|
|
|
/* 5a. Create the new LWP. */
|
2007-01-20 23:02:36 +03:00
|
|
|
newthread->pt_sleeponq = 0;
|
2006-12-24 21:39:45 +03:00
|
|
|
flag = 0;
|
|
|
|
if ((newthread->pt_flags & PT_FLAG_SUSPENDED) != 0)
|
|
|
|
flag |= LWP_SUSPENDED;
|
|
|
|
if ((newthread->pt_flags & PT_FLAG_DETACHED) != 0)
|
|
|
|
flag |= LWP_DETACHED;
|
2006-12-23 08:14:46 +03:00
|
|
|
ret = _lwp_create(newthread->pt_uc, (u_long)flag, &newthread->pt_lid);
|
|
|
|
if (ret != 0) {
|
|
|
|
SDPRINTF(("(pthread_create %p) _lwp_create: %s\n",
|
|
|
|
strerror(errno)));
|
|
|
|
free(name);
|
2007-01-16 07:19:02 +03:00
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
PTQ_REMOVE(&pthread__allqueue, newthread, pt_allq);
|
|
|
|
nthreads--;
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
pthread_spinlock(self, &pthread__deadqueue_lock);
|
|
|
|
PTQ_INSERT_HEAD(&pthread__deadqueue, newthread, pt_allq);
|
|
|
|
pthread_spinunlock(self, &pthread__deadqueue_lock);
|
2006-12-23 08:14:46 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDPRINTF(("(pthread_create %p) new thread %p (name %p, lid %d).\n",
|
|
|
|
self, newthread, newthread->pt_name,
|
|
|
|
(int)newthread->pt_lid));
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
*thread = newthread;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
pthread__create_tramp(void *(*start)(void *), void *arg)
|
|
|
|
{
|
|
|
|
void *retval;
|
|
|
|
|
2004-07-19 01:24:52 +04:00
|
|
|
retval = (*start)(arg);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
pthread_exit(retval);
|
|
|
|
|
2003-05-27 19:24:24 +04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
pthread__abort();
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2003-11-09 21:56:48 +03:00
|
|
|
int
|
|
|
|
pthread_suspend_np(pthread_t thread)
|
|
|
|
{
|
2005-10-16 04:37:52 +04:00
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
2003-11-09 21:56:48 +03:00
|
|
|
if (self == thread) {
|
|
|
|
return EDEADLK;
|
|
|
|
}
|
2005-10-16 04:37:52 +04:00
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
#endif
|
2006-12-23 08:14:46 +03:00
|
|
|
SDPRINTF(("(pthread_suspend_np %p) Suspend thread %p.\n",
|
|
|
|
self, thread));
|
|
|
|
return _lwp_suspend(thread->pt_lid);
|
2003-11-09 21:56:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_resume_np(pthread_t thread)
|
|
|
|
{
|
2005-10-16 04:37:52 +04:00
|
|
|
pthread_t self;
|
2003-11-09 21:56:48 +03:00
|
|
|
|
2005-10-16 04:37:52 +04:00
|
|
|
self = pthread__self();
|
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
#endif
|
2006-12-23 08:14:46 +03:00
|
|
|
SDPRINTF(("(pthread_resume_np %p) Resume thread %p.\n",
|
|
|
|
self, thread));
|
|
|
|
return _lwp_continue(thread->pt_lid);
|
2003-11-09 21:56:48 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
void
|
|
|
|
pthread_exit(void *retval)
|
|
|
|
{
|
2003-01-31 07:59:40 +03:00
|
|
|
pthread_t self;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct pt_clean_t *cleanup;
|
2003-02-27 01:02:48 +03:00
|
|
|
char *name;
|
2004-08-12 14:54:13 +04:00
|
|
|
int nt;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
2005-10-16 04:37:52 +04:00
|
|
|
SDPRINTF(("(pthread_exit %p) status %p, flags %x, cancel %d\n",
|
|
|
|
self, retval, self->pt_flags, self->pt_cancel));
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/* Disable cancellability. */
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_spinlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
self->pt_flags |= PT_FLAG_CS_DISABLED;
|
2003-02-22 03:53:29 +03:00
|
|
|
self->pt_cancel = 0;
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_spinunlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/* Call any cancellation cleanup handlers */
|
|
|
|
while (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
|
|
|
|
cleanup = PTQ_FIRST(&self->pt_cleanup_stack);
|
|
|
|
PTQ_REMOVE(&self->pt_cleanup_stack, cleanup, ptc_next);
|
|
|
|
(*cleanup->ptc_cleanup)(cleanup->ptc_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform cleanup of thread-specific data */
|
|
|
|
pthread__destroy_tsd(self);
|
|
|
|
|
|
|
|
self->pt_exitval = retval;
|
|
|
|
|
2004-08-12 14:54:13 +04:00
|
|
|
/*
|
|
|
|
* it's safe to check PT_FLAG_DETACHED without pt_flaglock
|
|
|
|
* because it's only set by pthread_detach with pt_join_lock held.
|
|
|
|
*/
|
|
|
|
pthread_spinlock(self, &self->pt_join_lock);
|
|
|
|
if (self->pt_flags & PT_FLAG_DETACHED) {
|
|
|
|
self->pt_state = PT_STATE_DEAD;
|
|
|
|
pthread_spinunlock(self, &self->pt_join_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
name = self->pt_name;
|
|
|
|
self->pt_name = NULL;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
PTQ_REMOVE(&pthread__allqueue, self, pt_allq);
|
|
|
|
nthreads--;
|
|
|
|
nt = nthreads;
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
|
|
|
|
if (nt == 0) {
|
|
|
|
/* Whoah, we're the last one. Time to go. */
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yeah, yeah, doing work while we're dead is tacky. */
|
|
|
|
pthread_spinlock(self, &pthread__deadqueue_lock);
|
2007-02-22 01:31:38 +03:00
|
|
|
PTQ_INSERT_TAIL(&pthread__deadqueue, self, pt_allq);
|
2006-12-24 21:39:45 +03:00
|
|
|
pthread_spinunlock(self, &pthread__deadqueue_lock);
|
2006-12-23 08:14:46 +03:00
|
|
|
_lwp_exit();
|
2003-01-18 13:32:11 +03:00
|
|
|
} else {
|
2004-08-12 14:54:13 +04:00
|
|
|
self->pt_state = PT_STATE_ZOMBIE;
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
/* Note: name will be freed by the joiner. */
|
2003-01-18 13:32:11 +03:00
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
nthreads--;
|
|
|
|
nt = nthreads;
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
if (nt == 0) {
|
|
|
|
/* Whoah, we're the last one. Time to go. */
|
|
|
|
exit(0);
|
|
|
|
}
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_spinunlock(self, &self->pt_join_lock);
|
|
|
|
_lwp_exit();
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2003-05-27 19:24:24 +04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
pthread__abort();
|
2003-01-18 13:32:11 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_join(pthread_t thread, void **valptr)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
2003-02-27 01:02:48 +03:00
|
|
|
char *name;
|
2006-12-23 08:14:46 +03:00
|
|
|
int num, retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
SDPRINTF(("(pthread_join %p) Joining %p.\n", self, thread));
|
|
|
|
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (thread == self)
|
|
|
|
return EDEADLK;
|
|
|
|
|
2006-12-23 08:14:46 +03:00
|
|
|
retval = 0;
|
|
|
|
name = NULL;
|
|
|
|
again:
|
|
|
|
pthread_spinlock(self, &thread->pt_join_lock);
|
|
|
|
switch (thread->pt_state) {
|
|
|
|
case PT_STATE_RUNNING:
|
|
|
|
pthread_spinunlock(self, &thread->pt_join_lock);
|
2006-12-24 21:39:45 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IEEE Std 1003.1, 2004 Edition:
|
|
|
|
*
|
|
|
|
* "The pthread_join() function shall not
|
|
|
|
* return an error code of [EINTR]."
|
|
|
|
*/
|
|
|
|
if (_lwp_wait(thread->pt_lid, &num) != 0 && errno != EINTR)
|
|
|
|
return errno;
|
2006-12-23 08:14:46 +03:00
|
|
|
goto again;
|
|
|
|
case PT_STATE_ZOMBIE:
|
|
|
|
if (valptr != NULL)
|
2006-12-24 21:39:45 +03:00
|
|
|
*valptr = thread->pt_exitval;
|
2006-12-23 08:14:46 +03:00
|
|
|
if (retval == 0) {
|
|
|
|
name = thread->pt_name;
|
|
|
|
thread->pt_name = NULL;
|
|
|
|
}
|
|
|
|
thread->pt_state = PT_STATE_DEAD;
|
|
|
|
pthread_spinunlock(self, &thread->pt_join_lock);
|
2007-01-16 08:22:55 +03:00
|
|
|
(void)_lwp_detach(thread->pt_lid);
|
2006-12-23 08:14:46 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pthread_spinunlock(self, &thread->pt_join_lock);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
SDPRINTF(("(pthread_join %p) Joined %p.\n", self, thread));
|
|
|
|
|
2004-08-12 14:54:13 +04:00
|
|
|
pthread__dead(self, thread);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
|
|
|
|
2006-12-23 08:14:46 +03:00
|
|
|
return retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_equal(pthread_t t1, pthread_t t2)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Nothing special here. */
|
|
|
|
return (t1 == t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_detach(pthread_t thread)
|
|
|
|
{
|
2003-01-31 07:59:40 +03:00
|
|
|
pthread_t self;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
2007-03-02 20:40:55 +03:00
|
|
|
pthread_spinlock(self, &self->pt_join_lock);
|
2007-02-22 01:31:38 +03:00
|
|
|
thread->pt_flags |= PT_FLAG_DETACHED;
|
2007-03-02 20:40:55 +03:00
|
|
|
pthread_spinunlock(self, &self->pt_join_lock);
|
2007-03-02 21:53:51 +03:00
|
|
|
|
2006-12-23 08:14:46 +03:00
|
|
|
return _lwp_detach(thread->pt_lid);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-12 14:54:13 +04:00
|
|
|
static void
|
|
|
|
pthread__dead(pthread_t self, pthread_t thread)
|
|
|
|
{
|
|
|
|
|
|
|
|
SDPRINTF(("(pthread__dead %p) Reclaimed %p.\n", self, thread));
|
|
|
|
pthread__assert(thread->pt_state == PT_STATE_DEAD);
|
|
|
|
pthread__assert(thread->pt_name == NULL);
|
|
|
|
|
|
|
|
/* Cleanup time. Move the dead thread from allqueue to the deadqueue */
|
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
PTQ_REMOVE(&pthread__allqueue, thread, pt_allq);
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
|
|
|
|
pthread_spinlock(self, &pthread__deadqueue_lock);
|
|
|
|
PTQ_INSERT_HEAD(&pthread__deadqueue, thread, pt_allq);
|
|
|
|
pthread_spinunlock(self, &pthread__deadqueue_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
int
|
|
|
|
pthread_getname_np(pthread_t thread, char *name, size_t len)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
pthread_spinlock(self, &thread->pt_join_lock);
|
|
|
|
if (thread->pt_name == NULL)
|
|
|
|
name[0] = '\0';
|
|
|
|
else
|
|
|
|
strlcpy(name, thread->pt_name, len);
|
|
|
|
pthread_spinunlock(self, &thread->pt_join_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setname_np(pthread_t thread, const char *name, void *arg)
|
|
|
|
{
|
2005-10-16 04:37:52 +04:00
|
|
|
pthread_t self;
|
2003-02-27 01:02:48 +03:00
|
|
|
char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP];
|
|
|
|
int namelen;
|
|
|
|
|
2005-10-16 04:37:52 +04:00
|
|
|
self = pthread__self();
|
2003-02-27 01:02:48 +03:00
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
namelen = snprintf(newname, sizeof(newname), name, arg);
|
|
|
|
if (namelen >= PTHREAD_MAX_NAMELEN_NP)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
cp = strdup(newname);
|
|
|
|
if (cp == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
pthread_spinlock(self, &thread->pt_join_lock);
|
|
|
|
oldname = thread->pt_name;
|
|
|
|
thread->pt_name = cp;
|
|
|
|
|
|
|
|
pthread_spinunlock(self, &thread->pt_join_lock);
|
|
|
|
|
|
|
|
if (oldname != NULL)
|
|
|
|
free(oldname);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/*
|
|
|
|
* XXX There should be a way for applications to use the efficent
|
|
|
|
* inline version, but there are opacity/namespace issues.
|
|
|
|
*/
|
|
|
|
pthread_t
|
|
|
|
pthread_self(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return pthread__self();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cancel(pthread_t thread)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
2005-10-16 04:37:52 +04:00
|
|
|
self = pthread__self();
|
|
|
|
#ifdef ERRORCHECK
|
|
|
|
if (pthread__find(self, thread) != 0)
|
|
|
|
return ESRCH;
|
|
|
|
#endif
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_spinlock(self, &thread->pt_flaglock);
|
|
|
|
thread->pt_flags |= PT_FLAG_CS_PENDING;
|
|
|
|
if ((thread->pt_flags & PT_FLAG_CS_DISABLED) == 0) {
|
|
|
|
thread->pt_cancel = 1;
|
|
|
|
pthread_spinunlock(self, &thread->pt_flaglock);
|
|
|
|
_lwp_wakeup(thread->pt_lid);
|
|
|
|
} else
|
|
|
|
pthread_spinunlock(self, &thread->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setcancelstate(int state, int *oldstate)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
2003-07-22 02:24:09 +04:00
|
|
|
int retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
2003-07-22 02:24:09 +04:00
|
|
|
retval = 0;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_spinlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
if (oldstate != NULL) {
|
2003-07-22 02:24:09 +04:00
|
|
|
if (self->pt_flags & PT_FLAG_CS_DISABLED)
|
2003-01-18 13:32:11 +03:00
|
|
|
*oldstate = PTHREAD_CANCEL_DISABLE;
|
|
|
|
else
|
|
|
|
*oldstate = PTHREAD_CANCEL_ENABLE;
|
|
|
|
}
|
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
if (state == PTHREAD_CANCEL_DISABLE) {
|
|
|
|
self->pt_flags |= PT_FLAG_CS_DISABLED;
|
|
|
|
if (self->pt_cancel) {
|
|
|
|
self->pt_flags |= PT_FLAG_CS_PENDING;
|
|
|
|
self->pt_cancel = 0;
|
|
|
|
}
|
|
|
|
} else if (state == PTHREAD_CANCEL_ENABLE) {
|
|
|
|
self->pt_flags &= ~PT_FLAG_CS_DISABLED;
|
2003-01-18 13:32:11 +03:00
|
|
|
/*
|
|
|
|
* If a cancellation was requested while cancellation
|
|
|
|
* was disabled, note that fact for future
|
|
|
|
* cancellation tests.
|
|
|
|
*/
|
2003-07-22 02:24:09 +04:00
|
|
|
if (self->pt_flags & PT_FLAG_CS_PENDING) {
|
2003-01-18 13:32:11 +03:00
|
|
|
self->pt_cancel = 1;
|
|
|
|
/* This is not a deferred cancellation point. */
|
2003-07-22 02:24:09 +04:00
|
|
|
if (self->pt_flags & PT_FLAG_CS_ASYNC) {
|
|
|
|
pthread_spinunlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2003-07-22 02:24:09 +04:00
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
} else
|
2003-07-22 02:24:09 +04:00
|
|
|
retval = EINVAL;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_spinunlock(self, &self->pt_flaglock);
|
|
|
|
return retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setcanceltype(int type, int *oldtype)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
2003-07-22 02:24:09 +04:00
|
|
|
int retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
2003-07-22 02:24:09 +04:00
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
pthread_spinlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
if (oldtype != NULL) {
|
2003-07-22 02:24:09 +04:00
|
|
|
if (self->pt_flags & PT_FLAG_CS_ASYNC)
|
2003-01-18 13:32:11 +03:00
|
|
|
*oldtype = PTHREAD_CANCEL_ASYNCHRONOUS;
|
|
|
|
else
|
|
|
|
*oldtype = PTHREAD_CANCEL_DEFERRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == PTHREAD_CANCEL_ASYNCHRONOUS) {
|
2003-07-22 02:24:09 +04:00
|
|
|
self->pt_flags |= PT_FLAG_CS_ASYNC;
|
|
|
|
if (self->pt_cancel) {
|
|
|
|
pthread_spinunlock(self, &self->pt_flaglock);
|
2003-01-18 13:32:11 +03:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2003-07-22 02:24:09 +04:00
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
} else if (type == PTHREAD_CANCEL_DEFERRED)
|
2003-07-22 02:24:09 +04:00
|
|
|
self->pt_flags &= ~PT_FLAG_CS_ASYNC;
|
2003-01-18 13:32:11 +03:00
|
|
|
else
|
2003-07-22 02:24:09 +04:00
|
|
|
retval = EINVAL;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
pthread_spinunlock(self, &self->pt_flaglock);
|
|
|
|
return retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread_testcancel()
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
if (self->pt_cancel)
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX requires that certain functions return an error rather than
|
|
|
|
* invoking undefined behavior even when handed completely bogus
|
|
|
|
* pthread_t values, e.g. stack garbage or (pthread_t)666. This
|
|
|
|
* utility routine searches the list of threads for the pthread_t
|
|
|
|
* value without dereferencing it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pthread__find(pthread_t self, pthread_t id)
|
|
|
|
{
|
|
|
|
pthread_t target;
|
|
|
|
|
|
|
|
pthread_spinlock(self, &pthread__allqueue_lock);
|
|
|
|
PTQ_FOREACH(target, &pthread__allqueue, pt_allq)
|
|
|
|
if (target == id)
|
|
|
|
break;
|
|
|
|
pthread_spinunlock(self, &pthread__allqueue_lock);
|
|
|
|
|
|
|
|
if (target == NULL)
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__testcancel(pthread_t self)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (self->pt_cancel)
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__cleanup_push(void (*cleanup)(void *), void *arg, void *store)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
struct pt_clean_t *entry;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
entry = store;
|
|
|
|
entry->ptc_cleanup = cleanup;
|
|
|
|
entry->ptc_arg = arg;
|
|
|
|
PTQ_INSERT_HEAD(&self->pt_cleanup_stack, entry, ptc_next);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__cleanup_pop(int ex, void *store)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
struct pt_clean_t *entry;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
entry = store;
|
|
|
|
|
|
|
|
PTQ_REMOVE(&self->pt_cleanup_stack, entry, ptc_next);
|
|
|
|
if (ex)
|
|
|
|
(*entry->ptc_cleanup)(entry->ptc_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int *
|
|
|
|
pthread__errno(void)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
return &(self->pt_errno);
|
|
|
|
}
|
2003-02-15 07:34:40 +03:00
|
|
|
|
2003-07-22 02:17:14 +04:00
|
|
|
ssize_t _sys_write(int, const void *, size_t);
|
|
|
|
|
2003-02-15 07:34:40 +03:00
|
|
|
void
|
2004-06-25 20:33:32 +04:00
|
|
|
pthread__assertfunc(const char *file, int line, const char *function,
|
|
|
|
const char *expr)
|
2003-02-15 07:34:40 +03:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int len;
|
|
|
|
|
2005-01-06 20:38:29 +03:00
|
|
|
SDPRINTF(("(af)\n"));
|
|
|
|
|
2003-02-15 07:34:40 +03:00
|
|
|
/*
|
|
|
|
* snprintf should not acquire any locks, or we could
|
|
|
|
* end up deadlocked if the assert caller held locks.
|
|
|
|
*/
|
|
|
|
len = snprintf(buf, 1024,
|
|
|
|
"assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
|
|
|
|
expr, file, line,
|
|
|
|
function ? ", function \"" : "",
|
|
|
|
function ? function : "",
|
|
|
|
function ? "\"" : "");
|
|
|
|
|
2003-07-22 02:17:14 +04:00
|
|
|
_sys_write(STDERR_FILENO, buf, (size_t)len);
|
2003-02-15 07:34:40 +03:00
|
|
|
(void)kill(getpid(), SIGABRT);
|
|
|
|
|
|
|
|
_exit(1);
|
|
|
|
}
|
2003-04-23 23:35:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2004-06-25 20:33:32 +04:00
|
|
|
pthread__errorfunc(const char *file, int line, const char *function,
|
|
|
|
const char *msg)
|
2003-04-23 23:35:47 +04:00
|
|
|
{
|
|
|
|
char buf[1024];
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
size_t len;
|
2003-04-23 23:35:47 +04:00
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
if (pthread__diagassert == 0)
|
2003-04-23 23:35:47 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* snprintf should not acquire any locks, or we could
|
|
|
|
* end up deadlocked if the assert caller held locks.
|
|
|
|
*/
|
|
|
|
len = snprintf(buf, 1024,
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
"%s: Error detected by libpthread: %s.\n"
|
|
|
|
"Detected by file \"%s\", line %d%s%s%s.\n"
|
|
|
|
"See pthread(3) for information.\n",
|
|
|
|
getprogname(), msg, file, line,
|
2003-04-23 23:35:47 +04:00
|
|
|
function ? ", function \"" : "",
|
|
|
|
function ? function : "",
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
function ? "\"" : "");
|
2003-04-23 23:35:47 +04:00
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
if (pthread__diagassert & DIAGASSERT_STDERR)
|
2003-07-22 02:17:14 +04:00
|
|
|
_sys_write(STDERR_FILENO, buf, len);
|
2003-04-23 23:35:47 +04:00
|
|
|
|
Implement a bunch of pthread_attr_() functions, which genuinely set and examine
pthread_attr_t objects, although most of the properties being set don't really
affect threads yet:
pthread_attr_{get,set}guardsize()
pthread_attr_{get,set}inheritsched()
pthread_attr_{get,set}scope()
pthread_attr_{get,set}stack()
pthread_attr_setstack{size,addr}()
Remove some useless assertions and error checks in the existing pthread_attr()
routines.
Implement pthread_attr_get_np(), to examine the attributes of an existing
thread. Idea and interface from FreeBSD.
Change PTHREAD_ERRORMODE environment variable to PTHREAD_DIAGASSERT, and
make it behave like libc's LIBC_DIAGASSERT. The way to disable error-checking
and aborting is now "PTHREAD_DIAGASSERT=AEL", rather than
"PTHREAD_ERRORMODE=ignore".
2003-07-19 02:12:30 +04:00
|
|
|
if (pthread__diagassert & DIAGASSERT_SYSLOG)
|
|
|
|
syslog(LOG_DEBUG | LOG_USER, "%s", buf);
|
|
|
|
|
|
|
|
if (pthread__diagassert & DIAGASSERT_ABORT) {
|
|
|
|
(void)kill(getpid(), SIGABRT);
|
2003-04-23 23:35:47 +04:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2006-12-23 12:48:18 +03:00
|
|
|
/*
|
2006-12-24 21:39:45 +03:00
|
|
|
* Thread park/unpark operations. The kernel operations are
|
|
|
|
* modelled after a brief description from "Multithreading in
|
|
|
|
* the Solaris Operating Environment":
|
2006-12-23 12:48:18 +03:00
|
|
|
*
|
|
|
|
* http://www.sun.com/software/whitepapers/solaris9/multithread.pdf
|
|
|
|
*/
|
|
|
|
|
2006-12-23 08:14:46 +03:00
|
|
|
#define OOPS(msg) \
|
2007-02-01 02:55:20 +03:00
|
|
|
pthread__errorfunc(__FILE__, __LINE__, __func__, msg)
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
pthread__park(pthread_t self, pthread_spin_t *lock,
|
|
|
|
void *obj, struct pthread_queue_t *queue,
|
2006-12-24 21:39:45 +03:00
|
|
|
const struct timespec *abstime, int tail,
|
|
|
|
int cancelpt)
|
2006-12-23 08:14:46 +03:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
SDPRINTF(("(pthread__park %p) obj %p enter\n", self, obj));
|
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/*
|
|
|
|
* Enter the object's queue.
|
|
|
|
*/
|
2006-12-23 08:14:46 +03:00
|
|
|
if (queue != NULL) {
|
|
|
|
if (tail)
|
|
|
|
PTQ_INSERT_TAIL(queue, self, pt_sleep);
|
|
|
|
else
|
|
|
|
PTQ_INSERT_HEAD(queue, self, pt_sleep);
|
2006-12-24 21:39:45 +03:00
|
|
|
self->pt_sleeponq = 1;
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
|
|
|
self->pt_sleepobj = obj;
|
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/*
|
|
|
|
* Wait until we are awoken by a pending unpark operation,
|
|
|
|
* a signal, an unpark posted after we have gone asleep,
|
|
|
|
* or an expired timeout.
|
|
|
|
*/
|
|
|
|
rv = 0;
|
2006-12-23 08:14:46 +03:00
|
|
|
do {
|
|
|
|
pthread_spinunlock(self, lock);
|
2007-01-16 04:35:16 +03:00
|
|
|
if (_lwp_park(abstime, NULL, obj) != 0) {
|
2006-12-24 21:39:45 +03:00
|
|
|
switch (rv = errno) {
|
|
|
|
case EINTR:
|
|
|
|
/* Check for cancellation. */
|
|
|
|
if (cancelpt && self->pt_cancel)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case EALREADY:
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case ETIMEDOUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OOPS("_lwp_park failed");
|
|
|
|
SDPRINTF(("(pthread__park %p) syscall rv=%d\n",
|
|
|
|
self, rv));
|
|
|
|
break;
|
|
|
|
}
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
|
|
|
pthread_spinlock(self, lock);
|
2006-12-24 21:39:45 +03:00
|
|
|
} while (self->pt_sleepobj != NULL && rv == 0);
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/*
|
|
|
|
* If we have been awoken early but are still on the queue,
|
|
|
|
* then remove ourself.
|
|
|
|
*/
|
|
|
|
if (queue != NULL && self->pt_sleeponq)
|
2006-12-23 08:14:46 +03:00
|
|
|
PTQ_REMOVE(queue, self, pt_sleep);
|
2006-12-24 21:39:45 +03:00
|
|
|
self->pt_sleepobj = NULL;
|
|
|
|
self->pt_sleeponq = 0;
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
SDPRINTF(("(pthread__park %p) obj %p exit\n", self, obj));
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__unpark(pthread_t self, pthread_spin_t *lock, void *obj,
|
|
|
|
pthread_t target)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (target != NULL) {
|
|
|
|
SDPRINTF(("(pthread__unpark %p) obj %p target %p\n", self, obj,
|
|
|
|
target));
|
|
|
|
|
2006-12-24 21:39:45 +03:00
|
|
|
/*
|
|
|
|
* Easy: the thread has already been removed from
|
|
|
|
* the queue, so just awaken it.
|
|
|
|
*/
|
2006-12-23 08:14:46 +03:00
|
|
|
target->pt_sleepobj = NULL;
|
2006-12-24 21:39:45 +03:00
|
|
|
target->pt_sleeponq = 0;
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_spinunlock(self, lock);
|
2007-01-16 04:35:16 +03:00
|
|
|
rv = _lwp_unpark(target->pt_lid, obj);
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
if (rv != 0 && errno != EALREADY && errno != EINTR) {
|
|
|
|
SDPRINTF(("(pthread__unpark %p) syscall rv=%d\n",
|
|
|
|
self, rv));
|
|
|
|
OOPS("_lwp_unpark failed");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
pthread_spinunlock(self, lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__unpark_all(pthread_t self, pthread_spin_t *lock, void *obj,
|
|
|
|
struct pthread_queue_t *queue)
|
|
|
|
{
|
2006-12-24 21:39:45 +03:00
|
|
|
lwpid_t waiters[PTHREAD__UNPARK_MAX];
|
|
|
|
int n, rv;
|
|
|
|
pthread_t thread, next;
|
|
|
|
|
|
|
|
if (PTQ_EMPTY(queue)) {
|
|
|
|
pthread_spinunlock(self, lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, clear all sleepobj pointers, since we can release the
|
|
|
|
* spin lock before awkening everybody, and must synchronise with
|
|
|
|
* pthread__park().
|
|
|
|
*/
|
|
|
|
PTQ_FOREACH(thread, queue, pt_sleep) {
|
|
|
|
thread->pt_sleepobj = NULL;
|
|
|
|
}
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
for (;;) {
|
2006-12-24 21:39:45 +03:00
|
|
|
thread = PTQ_FIRST(queue);
|
|
|
|
for (n = 0; n < pthread__unpark_max && thread != NULL;
|
|
|
|
thread = next) {
|
|
|
|
/*
|
|
|
|
* If the sleepobj pointer is non-NULL, it
|
|
|
|
* means one of two things:
|
|
|
|
*
|
|
|
|
* o The thread has awoken early, spun
|
|
|
|
* through application code and is
|
|
|
|
* once more asleep on this object.
|
|
|
|
*
|
|
|
|
* o This is a new thread that has blocked
|
|
|
|
* on the object after we have released
|
|
|
|
* the interlock in this loop.
|
|
|
|
*
|
|
|
|
* In both cases we shouldn't remove the
|
|
|
|
* thread from the queue.
|
|
|
|
*
|
|
|
|
* XXXLWP basic fairness issues here.
|
|
|
|
*/
|
|
|
|
next = PTQ_NEXT(thread, pt_sleep);
|
|
|
|
if (thread->pt_sleepobj != NULL)
|
|
|
|
continue;
|
|
|
|
thread->pt_sleeponq = 0;
|
|
|
|
waiters[n++] = thread->pt_lid;
|
2006-12-23 08:14:46 +03:00
|
|
|
PTQ_REMOVE(queue, thread, pt_sleep);
|
|
|
|
SDPRINTF(("(pthread__unpark_all %p) obj %p "
|
|
|
|
"unpark %p\n", self, obj, thread));
|
|
|
|
}
|
2006-12-24 21:39:45 +03:00
|
|
|
|
2006-12-23 08:14:46 +03:00
|
|
|
pthread_spinunlock(self, lock);
|
2006-12-24 21:39:45 +03:00
|
|
|
switch (n) {
|
2006-12-23 08:14:46 +03:00
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
case 1:
|
2007-01-16 04:35:16 +03:00
|
|
|
rv = _lwp_unpark(waiters[0], obj);
|
2006-12-23 08:14:46 +03:00
|
|
|
if (rv != 0 && errno != EALREADY && errno != EINTR) {
|
|
|
|
OOPS("_lwp_unpark failed");
|
|
|
|
SDPRINTF(("(pthread__unpark_all %p) "
|
|
|
|
"syscall rv=%d\n", self, rv));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
2007-01-16 04:35:16 +03:00
|
|
|
rv = _lwp_unpark_all(waiters, n, obj);
|
2006-12-23 08:14:46 +03:00
|
|
|
if (rv != 0 && errno != EINTR) {
|
|
|
|
OOPS("_lwp_unpark_all failed");
|
|
|
|
SDPRINTF(("(pthread__unpark_all %p) "
|
|
|
|
"syscall rv=%d\n", self, rv));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pthread_spinlock(self, lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef OOPS
|