2014-02-01 00:44:01 +04:00
|
|
|
/* $NetBSD: pthread.c,v 1.144 2014/01/31 20:44:01 christos Exp $ */
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/*-
|
2008-03-08 16:23:13 +03:00
|
|
|
* Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 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.
|
|
|
|
*
|
|
|
|
* 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>
|
2014-02-01 00:44:01 +04:00
|
|
|
__RCSID("$NetBSD: pthread.c,v 1.144 2014/01/31 20:44:01 christos Exp $");
|
2007-08-16 05:09:34 +04:00
|
|
|
|
|
|
|
#define __EXPOSE_STACK 1
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2012-03-02 22:06:05 +04:00
|
|
|
#include <sys/exec_elf.h>
|
2007-08-16 05:09:34 +04:00
|
|
|
#include <sys/mman.h>
|
2012-08-02 16:43:41 +04:00
|
|
|
#include <sys/lwp.h>
|
2007-11-13 20:20:08 +03:00
|
|
|
#include <sys/lwpctl.h>
|
2013-01-01 22:42:39 +04:00
|
|
|
#include <sys/resource.h>
|
2011-03-10 02:10:05 +03:00
|
|
|
#include <sys/tls.h>
|
2003-03-08 11:03:34 +03:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
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>
|
2012-11-04 03:42:27 +04:00
|
|
|
#include <stddef.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#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>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <sched.h>
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
#include "pthread.h"
|
|
|
|
#include "pthread_int.h"
|
2013-03-21 20:49:11 +04:00
|
|
|
#include "reentrant.h"
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-10-16 17:41:18 +04:00
|
|
|
pthread_rwlock_t pthread__alltree_lock = PTHREAD_RWLOCK_INITIALIZER;
|
2012-11-04 03:42:27 +04:00
|
|
|
static rb_tree_t pthread__alltree;
|
2007-10-16 17:41:18 +04:00
|
|
|
|
2012-11-04 03:42:27 +04:00
|
|
|
static signed int pthread__cmp(void *, const void *, const void *);
|
|
|
|
|
|
|
|
static const rb_tree_ops_t pthread__alltree_ops = {
|
|
|
|
.rbto_compare_nodes = pthread__cmp,
|
|
|
|
.rbto_compare_key = pthread__cmp,
|
|
|
|
.rbto_node_offset = offsetof(struct __pthread_st, pt_alltree),
|
|
|
|
.rbto_context = NULL
|
|
|
|
};
|
2007-10-16 17:41:18 +04:00
|
|
|
|
2009-05-17 18:49:00 +04:00
|
|
|
static void pthread__create_tramp(void *);
|
2007-08-04 17:37:48 +04:00
|
|
|
static void pthread__initthread(pthread_t);
|
2007-08-16 16:01:49 +04:00
|
|
|
static void pthread__scrubthread(pthread_t, char *, int);
|
2007-08-16 05:09:34 +04:00
|
|
|
static void pthread__initmain(pthread_t *);
|
2009-03-31 01:32:51 +04:00
|
|
|
static void pthread__fork_callback(void);
|
2007-12-24 17:46:28 +03:00
|
|
|
static void pthread__reap(pthread_t);
|
2008-01-08 23:56:08 +03:00
|
|
|
static void pthread__child_callback(void);
|
|
|
|
static void pthread__start(void);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-11-13 18:57:10 +03:00
|
|
|
void pthread__init(void);
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
int pthread__started;
|
2013-03-21 20:49:11 +04:00
|
|
|
int __uselibcstub = 1;
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_t pthread__deadqueue_lock = PTHREAD_MUTEX_INITIALIZER;
|
2007-08-04 17:37:48 +04:00
|
|
|
pthread_queue_t pthread__deadqueue;
|
2007-10-16 19:07:02 +04:00
|
|
|
pthread_queue_t pthread__allqueue;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
static pthread_attr_t pthread_default_attr;
|
2007-11-13 20:20:08 +03:00
|
|
|
static lwpctl_t pthread__dummy_lwpctl = { .lc_curcpu = LWPCTL_CPU_NONE };
|
2003-01-18 13:32:11 +03: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
|
|
|
enum {
|
|
|
|
DIAGASSERT_ABORT = 1<<0,
|
|
|
|
DIAGASSERT_STDERR = 1<<1,
|
|
|
|
DIAGASSERT_SYSLOG = 1<<2
|
|
|
|
};
|
2003-04-23 23:35:47 +04:00
|
|
|
|
2008-10-08 14:03:28 +04:00
|
|
|
static int pthread__diagassert;
|
2003-04-23 23:35:47 +04:00
|
|
|
|
2007-08-16 02:48:52 +04:00
|
|
|
int pthread__concurrency;
|
|
|
|
int pthread__nspins;
|
2006-12-24 21:39:45 +03:00
|
|
|
int pthread__unpark_max = PTHREAD__UNPARK_MAX;
|
2011-10-02 22:18:56 +04:00
|
|
|
int pthread__dbg; /* set by libpthread_dbg if active */
|
2004-03-14 04:19:41 +03:00
|
|
|
|
2007-08-16 05:09:34 +04:00
|
|
|
/*
|
|
|
|
* We have to initialize the pthread_stack* variables here because
|
|
|
|
* mutexes are used before pthread_init() and thus pthread__initmain()
|
|
|
|
* are called. Since mutexes only save the stack pointer and not a
|
|
|
|
* pointer to the thread data, it is safe to change the mapping from
|
|
|
|
* stack pointer to thread data afterwards.
|
|
|
|
*/
|
2012-03-02 22:06:05 +04:00
|
|
|
size_t pthread__stacksize;
|
|
|
|
size_t pthread__pagesize;
|
|
|
|
static struct __pthread_st pthread__main;
|
2007-08-16 05:09:34 +04: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)
|
2007-11-14 22:28:23 +03:00
|
|
|
__strong_alias(__libc_thr_equal,pthread_equal)
|
2007-11-13 18:57:10 +03:00
|
|
|
__strong_alias(__libc_thr_init,pthread__init)
|
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
|
|
|
};
|
|
|
|
|
2008-05-25 21:05:28 +04:00
|
|
|
#define NHASHLOCK 64
|
|
|
|
|
|
|
|
static union hashlock {
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
char pad[64];
|
|
|
|
} hashlocks[NHASHLOCK] __aligned(64);
|
|
|
|
|
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
|
2007-11-13 18:57:10 +03:00
|
|
|
pthread__init(void)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
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;
|
2012-08-02 16:43:41 +04:00
|
|
|
int i;
|
2003-01-18 13:32:11 +03:00
|
|
|
extern int __isthreaded;
|
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
__uselibcstub = 0;
|
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
pthread__pagesize = (size_t)sysconf(_SC_PAGESIZE);
|
2012-11-03 07:10:35 +04:00
|
|
|
pthread__concurrency = (int)sysconf(_SC_NPROCESSORS_CONF);
|
2007-08-07 23:04:21 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Initialize locks first; they're needed elsewhere. */
|
2007-08-16 16:01:49 +04:00
|
|
|
pthread__lockprim_init();
|
2008-05-25 21:05:28 +04:00
|
|
|
for (i = 0; i < NHASHLOCK; i++) {
|
|
|
|
pthread_mutex_init(&hashlocks[i].mutex, NULL);
|
|
|
|
}
|
2004-03-14 04:19:41 +03:00
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
/* Fetch parameters. */
|
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);
|
2007-10-16 19:07:02 +04:00
|
|
|
PTQ_INIT(&pthread__allqueue);
|
2003-01-18 13:32:11 +03:00
|
|
|
PTQ_INIT(&pthread__deadqueue);
|
2012-11-04 03:42:27 +04:00
|
|
|
|
|
|
|
rb_tree_init(&pthread__alltree, &pthread__alltree_ops);
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Create the thread structure corresponding to main() */
|
|
|
|
pthread__initmain(&first);
|
2007-08-04 17:37:48 +04:00
|
|
|
pthread__initthread(first);
|
2007-08-16 16:01:49 +04:00
|
|
|
pthread__scrubthread(first, NULL, 0);
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
first->pt_lid = _lwp_self();
|
2007-10-16 19:07:02 +04:00
|
|
|
PTQ_INSERT_HEAD(&pthread__allqueue, first, pt_allq);
|
2012-11-04 03:42:27 +04:00
|
|
|
(void)rb_tree_insert_node(&pthread__alltree, first);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2009-03-31 01:32:51 +04:00
|
|
|
if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &first->pt_lwpctl) != 0) {
|
|
|
|
err(1, "_lwp_ctl");
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Start subsystems */
|
|
|
|
PTHREAD_MD_INIT
|
|
|
|
|
2007-11-13 18:57:10 +03:00
|
|
|
for (p = pthread__getenv("PTHREAD_DIAGASSERT"); p && *p; p++) {
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Tell libc that we're here and it should role-play accordingly. */
|
2009-03-31 01:32:51 +04:00
|
|
|
pthread_atfork(NULL, NULL, pthread__fork_callback);
|
2003-01-18 13:32:11 +03:00
|
|
|
__isthreaded = 1;
|
|
|
|
}
|
|
|
|
|
2007-11-13 20:20:08 +03:00
|
|
|
static void
|
2009-03-31 01:32:51 +04:00
|
|
|
pthread__fork_callback(void)
|
2007-11-13 20:20:08 +03:00
|
|
|
{
|
2012-04-26 04:21:44 +04:00
|
|
|
struct __pthread_st *self = pthread__self();
|
2007-11-13 20:20:08 +03:00
|
|
|
|
|
|
|
/* lwpctl state is not copied across fork. */
|
2012-04-26 04:21:44 +04:00
|
|
|
if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
|
2009-03-31 01:32:51 +04:00
|
|
|
err(1, "_lwp_ctl");
|
|
|
|
}
|
2010-03-25 04:15:00 +03:00
|
|
|
self->pt_lid = _lwp_self();
|
2009-03-31 01:32:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pthread__child_callback(void)
|
|
|
|
{
|
2007-11-13 20:20:08 +03:00
|
|
|
|
2003-04-08 01:29:48 +04:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* General-purpose thread data structure sanitization. */
|
2007-08-04 17:37:48 +04:00
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
pthread__initthread(pthread_t t)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
|
2007-11-13 18:57:10 +03:00
|
|
|
t->pt_self = t;
|
2003-01-18 13:32:11 +03:00
|
|
|
t->pt_magic = PT_MAGIC;
|
2007-08-07 23:04:21 +04:00
|
|
|
t->pt_willpark = 0;
|
|
|
|
t->pt_unpark = 0;
|
2007-09-07 18:09:27 +04:00
|
|
|
t->pt_nwaiters = 0;
|
2007-08-07 23:04:21 +04:00
|
|
|
t->pt_sleepobj = NULL;
|
|
|
|
t->pt_signalled = 0;
|
2007-08-16 16:01:49 +04:00
|
|
|
t->pt_havespecific = 0;
|
2007-09-07 18:09:27 +04:00
|
|
|
t->pt_early = NULL;
|
2007-11-13 20:20:08 +03:00
|
|
|
t->pt_lwpctl = &pthread__dummy_lwpctl;
|
|
|
|
t->pt_blocking = 0;
|
2007-12-24 17:46:28 +03:00
|
|
|
t->pt_droplock = NULL;
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2007-11-13 18:57:10 +03:00
|
|
|
memcpy(&t->pt_lockops, pthread__lock_ops, sizeof(t->pt_lockops));
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_init(&t->pt_lock, NULL);
|
2003-01-18 13:32:11 +03:00
|
|
|
PTQ_INIT(&t->pt_cleanup_stack);
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread_cond_init(&t->pt_joiners, NULL);
|
2007-12-01 04:07:34 +03:00
|
|
|
memset(&t->pt_specific, 0, sizeof(t->pt_specific));
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
static void
|
|
|
|
pthread__scrubthread(pthread_t t, char *name, int flags)
|
|
|
|
{
|
|
|
|
|
|
|
|
t->pt_state = PT_STATE_RUNNING;
|
|
|
|
t->pt_exitval = NULL;
|
|
|
|
t->pt_flags = flags;
|
|
|
|
t->pt_cancel = 0;
|
|
|
|
t->pt_errno = 0;
|
|
|
|
t->pt_name = name;
|
|
|
|
t->pt_lid = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
static int
|
2012-03-13 00:16:52 +04:00
|
|
|
pthread__getstack(pthread_t newthread, const pthread_attr_t *attr)
|
2012-03-02 22:06:05 +04:00
|
|
|
{
|
2012-03-13 00:16:52 +04:00
|
|
|
void *stackbase, *stackbase2, *redzone;
|
2012-03-12 20:37:15 +04:00
|
|
|
size_t stacksize, guardsize;
|
2012-03-13 00:16:52 +04:00
|
|
|
bool allocated;
|
2012-03-08 20:40:45 +04:00
|
|
|
|
|
|
|
if (attr != NULL) {
|
|
|
|
pthread_attr_getstack(attr, &stackbase, &stacksize);
|
|
|
|
} else {
|
|
|
|
stackbase = NULL;
|
|
|
|
stacksize = 0;
|
|
|
|
}
|
|
|
|
if (stacksize == 0)
|
|
|
|
stacksize = pthread__stacksize;
|
2012-03-02 22:06:05 +04:00
|
|
|
|
2012-03-13 00:16:52 +04:00
|
|
|
if (newthread->pt_stack_allocated) {
|
2012-03-23 00:01:18 +04:00
|
|
|
if (stackbase == NULL &&
|
|
|
|
newthread->pt_stack.ss_size == stacksize)
|
2012-03-13 00:16:52 +04:00
|
|
|
return 0;
|
|
|
|
stackbase2 = newthread->pt_stack.ss_sp;
|
|
|
|
#ifndef __MACHINE_STACK_GROWS_UP
|
|
|
|
stackbase2 = (char *)stackbase2 - newthread->pt_guardsize;
|
|
|
|
#endif
|
|
|
|
munmap(stackbase2,
|
|
|
|
newthread->pt_stack.ss_size + newthread->pt_guardsize);
|
|
|
|
newthread->pt_stack.ss_sp = NULL;
|
|
|
|
newthread->pt_stack.ss_size = 0;
|
|
|
|
newthread->pt_guardsize = 0;
|
|
|
|
newthread->pt_stack_allocated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
newthread->pt_stack_allocated = false;
|
|
|
|
|
2012-03-08 20:40:45 +04:00
|
|
|
if (stackbase == NULL) {
|
2012-03-13 00:16:52 +04:00
|
|
|
stacksize = ((stacksize - 1) | (pthread__pagesize - 1)) + 1;
|
2012-03-12 20:37:15 +04:00
|
|
|
guardsize = pthread__pagesize;
|
|
|
|
stackbase = mmap(NULL, stacksize + guardsize,
|
2012-03-08 20:40:45 +04:00
|
|
|
PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
|
|
|
|
if (stackbase == MAP_FAILED)
|
|
|
|
return ENOMEM;
|
2012-03-13 00:16:52 +04:00
|
|
|
allocated = true;
|
2012-03-12 20:37:15 +04:00
|
|
|
} else {
|
|
|
|
guardsize = 0;
|
2012-03-13 00:16:52 +04:00
|
|
|
allocated = false;
|
2012-03-08 20:40:45 +04:00
|
|
|
}
|
2012-03-02 22:06:05 +04:00
|
|
|
#ifdef __MACHINE_STACK_GROWS_UP
|
2012-03-13 00:16:52 +04:00
|
|
|
redzone = (char *)stackbase + stacksize;
|
|
|
|
stackbase2 = (char *)stackbase;
|
2012-03-02 22:06:05 +04:00
|
|
|
#else
|
|
|
|
redzone = (char *)stackbase;
|
2012-03-13 00:16:52 +04:00
|
|
|
stackbase2 = (char *)stackbase + guardsize;
|
2012-03-02 22:06:05 +04:00
|
|
|
#endif
|
2012-03-13 00:16:52 +04:00
|
|
|
if (allocated && guardsize &&
|
|
|
|
mprotect(redzone, guardsize, PROT_NONE) == -1) {
|
2012-03-12 20:37:15 +04:00
|
|
|
munmap(stackbase, stacksize + guardsize);
|
2012-03-02 22:06:05 +04:00
|
|
|
return EPERM;
|
|
|
|
}
|
2012-03-13 00:16:52 +04:00
|
|
|
newthread->pt_stack.ss_size = stacksize;
|
|
|
|
newthread->pt_stack.ss_sp = stackbase2;
|
|
|
|
newthread->pt_guardsize = guardsize;
|
|
|
|
newthread->pt_stack_allocated = allocated;
|
2012-03-02 22:06:05 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|
|
|
void *(*startfunc)(void *), void *arg)
|
|
|
|
{
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread_t newthread;
|
2003-01-18 13:32:11 +03:00
|
|
|
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-08-16 04:41:23 +04:00
|
|
|
unsigned long flag;
|
2011-03-10 02:10:05 +03:00
|
|
|
void *private_area;
|
2007-08-16 04:41:23 +04:00
|
|
|
int ret;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub)) {
|
|
|
|
pthread__errorfunc(__FILE__, __LINE__, __func__,
|
|
|
|
"pthread_create() requires linking with -lpthread");
|
|
|
|
return __libc_thr_create_stub(thread, attr, startfunc, arg);
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2007-08-04 17:43:46 +04:00
|
|
|
newthread = NULL;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
/*
|
|
|
|
* Try to reclaim a dead thread.
|
|
|
|
*/
|
2007-08-04 17:43:46 +04:00
|
|
|
if (!PTQ_EMPTY(&pthread__deadqueue)) {
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&pthread__deadqueue_lock);
|
2009-10-04 03:49:50 +04:00
|
|
|
PTQ_FOREACH(newthread, &pthread__deadqueue, pt_deadq) {
|
2007-12-24 17:46:28 +03:00
|
|
|
/* Still running? */
|
2009-10-04 03:49:50 +04:00
|
|
|
if (newthread->pt_lwpctl->lc_curcpu ==
|
|
|
|
LWPCTL_CPU_EXITED ||
|
|
|
|
(_lwp_kill(newthread->pt_lid, 0) == -1 &&
|
|
|
|
errno == ESRCH))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (newthread)
|
|
|
|
PTQ_REMOVE(&pthread__deadqueue, newthread, pt_deadq);
|
|
|
|
pthread_mutex_unlock(&pthread__deadqueue_lock);
|
2011-03-30 04:03:26 +04:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
if (newthread && newthread->pt_tls) {
|
|
|
|
_rtld_tls_free(newthread->pt_tls);
|
|
|
|
newthread->pt_tls = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-15 18:39:33 +03:00
|
|
|
}
|
2007-08-04 17:43:46 +04:00
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
/*
|
|
|
|
* If necessary set up a stack, allocate space for a pthread_st,
|
|
|
|
* and initialize it.
|
|
|
|
*/
|
2007-02-15 18:39:33 +03:00
|
|
|
if (newthread == NULL) {
|
2012-03-02 22:06:05 +04:00
|
|
|
newthread = malloc(sizeof(*newthread));
|
|
|
|
if (newthread == NULL) {
|
|
|
|
free(name);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2012-03-13 00:16:52 +04:00
|
|
|
newthread->pt_stack_allocated = false;
|
2012-03-02 22:06:05 +04:00
|
|
|
|
2012-03-13 00:16:52 +04:00
|
|
|
if (pthread__getstack(newthread, attr)) {
|
2012-03-02 22:06:05 +04:00
|
|
|
free(newthread);
|
|
|
|
free(name);
|
|
|
|
return ENOMEM;
|
2003-12-18 18:39:56 +03:00
|
|
|
}
|
2007-08-16 04:41:23 +04:00
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
/* This is used only when creating the thread. */
|
2007-08-16 04:41:23 +04:00
|
|
|
_INITCONTEXT_U(&newthread->pt_uc);
|
|
|
|
newthread->pt_uc.uc_stack = newthread->pt_stack;
|
|
|
|
newthread->pt_uc.uc_link = NULL;
|
2011-03-30 04:03:26 +04:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
newthread->pt_tls = NULL;
|
|
|
|
#endif
|
2007-08-16 16:01:49 +04:00
|
|
|
|
|
|
|
/* Add to list of all threads. */
|
2007-10-16 17:41:18 +04:00
|
|
|
pthread_rwlock_wrlock(&pthread__alltree_lock);
|
2007-10-16 19:07:02 +04:00
|
|
|
PTQ_INSERT_TAIL(&pthread__allqueue, newthread, pt_allq);
|
2012-11-04 03:42:27 +04:00
|
|
|
(void)rb_tree_insert_node(&pthread__alltree, newthread);
|
2007-10-16 17:41:18 +04:00
|
|
|
pthread_rwlock_unlock(&pthread__alltree_lock);
|
2007-08-16 16:01:49 +04:00
|
|
|
|
|
|
|
/* Will be reset by the thread upon exit. */
|
|
|
|
pthread__initthread(newthread);
|
2012-03-13 00:16:52 +04:00
|
|
|
} else {
|
|
|
|
if (pthread__getstack(newthread, attr)) {
|
|
|
|
pthread_mutex_lock(&pthread__deadqueue_lock);
|
|
|
|
PTQ_INSERT_TAIL(&pthread__deadqueue, newthread, pt_deadq);
|
|
|
|
pthread_mutex_unlock(&pthread__deadqueue_lock);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
_INITCONTEXT_U(&newthread->pt_uc);
|
|
|
|
newthread->pt_uc.uc_stack = newthread->pt_stack;
|
|
|
|
newthread->pt_uc.uc_link = NULL;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2007-08-16 16:01:49 +04:00
|
|
|
/*
|
|
|
|
* Create the new LWP.
|
|
|
|
*/
|
|
|
|
pthread__scrubthread(newthread, name, nattr.pta_flags);
|
2009-05-17 18:49:00 +04:00
|
|
|
newthread->pt_func = startfunc;
|
|
|
|
newthread->pt_arg = arg;
|
2011-03-10 02:10:05 +03:00
|
|
|
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
|
|
|
|
private_area = newthread->pt_tls = _rtld_tls_allocate();
|
|
|
|
newthread->pt_tls->tcb_pthread = newthread;
|
|
|
|
#else
|
|
|
|
private_area = newthread;
|
|
|
|
#endif
|
|
|
|
|
2009-05-17 18:49:00 +04:00
|
|
|
_lwp_makecontext(&newthread->pt_uc, pthread__create_tramp,
|
2011-03-10 02:10:05 +03:00
|
|
|
newthread, private_area, newthread->pt_stack.ss_sp,
|
2009-05-17 18:49:00 +04:00
|
|
|
newthread->pt_stack.ss_size);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-12-24 17:46:28 +03:00
|
|
|
flag = LWP_DETACHED;
|
2008-06-28 14:29:37 +04:00
|
|
|
if ((newthread->pt_flags & PT_FLAG_SUSPENDED) != 0 ||
|
2008-06-28 14:36:12 +04:00
|
|
|
(nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0)
|
2006-12-24 21:39:45 +03:00
|
|
|
flag |= LWP_SUSPENDED;
|
2007-08-16 04:41:23 +04:00
|
|
|
ret = _lwp_create(&newthread->pt_uc, flag, &newthread->pt_lid);
|
2006-12-23 08:14:46 +03:00
|
|
|
if (ret != 0) {
|
2012-08-15 17:28:32 +04:00
|
|
|
ret = errno;
|
2010-07-08 19:13:35 +04:00
|
|
|
pthread_mutex_lock(&newthread->pt_lock);
|
|
|
|
/* Will unlock and free name. */
|
|
|
|
pthread__reap(newthread);
|
2006-12-23 08:14:46 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-28 14:36:12 +04:00
|
|
|
if ((nattr.pta_flags & PT_FLAG_EXPLICIT_SCHED) != 0) {
|
2008-06-28 14:29:37 +04:00
|
|
|
if (p != NULL) {
|
|
|
|
(void)pthread_setschedparam(newthread, p->ptap_policy,
|
|
|
|
&p->ptap_sp);
|
|
|
|
}
|
|
|
|
if ((newthread->pt_flags & PT_FLAG_SUSPENDED) == 0) {
|
|
|
|
(void)_lwp_continue(newthread->pt_lid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
*thread = newthread;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-16 20:05:58 +04:00
|
|
|
__dead static void
|
2009-05-17 18:49:00 +04:00
|
|
|
pthread__create_tramp(void *cookie)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
2009-05-17 18:49:00 +04:00
|
|
|
pthread_t self;
|
2003-01-18 13:32:11 +03:00
|
|
|
void *retval;
|
|
|
|
|
2009-05-17 18:49:00 +04:00
|
|
|
self = cookie;
|
2007-11-13 20:20:08 +03:00
|
|
|
|
2007-08-04 17:37:48 +04:00
|
|
|
/*
|
|
|
|
* Throw away some stack in a feeble attempt to reduce cache
|
|
|
|
* thrash. May help for SMT processors. XXX We should not
|
|
|
|
* be allocating stacks on fixed 2MB boundaries. Needs a
|
2009-05-17 18:49:00 +04:00
|
|
|
* thread register or decent thread local storage.
|
|
|
|
*
|
|
|
|
* Note that we may race with the kernel in _lwp_create(),
|
|
|
|
* and so pt_lid can be unset at this point, but we don't
|
2007-11-07 03:55:22 +03:00
|
|
|
* care.
|
2007-08-04 17:37:48 +04:00
|
|
|
*/
|
2007-11-07 03:55:22 +03:00
|
|
|
(void)alloca(((unsigned)self->pt_lid & 7) << 8);
|
|
|
|
|
|
|
|
if (self->pt_name != NULL) {
|
|
|
|
pthread_mutex_lock(&self->pt_lock);
|
|
|
|
if (self->pt_name != NULL)
|
2007-11-13 20:20:08 +03:00
|
|
|
(void)_lwp_setname(0, self->pt_name);
|
2007-11-07 03:55:22 +03:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
|
|
|
}
|
2007-08-04 17:37:48 +04:00
|
|
|
|
2008-03-08 16:23:13 +03:00
|
|
|
if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
|
|
|
|
err(1, "_lwp_ctl");
|
|
|
|
}
|
2007-11-13 20:20:08 +03:00
|
|
|
|
2009-05-17 18:49:00 +04:00
|
|
|
retval = (*self->pt_func)(self->pt_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;
|
|
|
|
}
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2005-10-16 04:37:52 +04:00
|
|
|
return ESRCH;
|
2007-08-17 18:28:31 +04:00
|
|
|
if (_lwp_suspend(thread->pt_lid) == 0)
|
|
|
|
return 0;
|
|
|
|
return errno;
|
2003-11-09 21:56:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_resume_np(pthread_t thread)
|
|
|
|
{
|
2007-08-16 17:54:16 +04:00
|
|
|
|
|
|
|
if (pthread__find(thread) != 0)
|
2005-10-16 04:37:52 +04:00
|
|
|
return ESRCH;
|
2007-08-17 18:28:31 +04:00
|
|
|
if (_lwp_continue(thread->pt_lid) == 0)
|
|
|
|
return 0;
|
|
|
|
return errno;
|
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;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub)) {
|
|
|
|
__libc_thr_exit_stub(retval);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
/* Disable cancellability. */
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&self->pt_lock);
|
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-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/* Call any cancellation cleanup handlers */
|
2007-12-24 17:46:28 +03:00
|
|
|
if (!PTQ_EMPTY(&self->pt_cleanup_stack)) {
|
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&self->pt_lock);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform cleanup of thread-specific data */
|
|
|
|
pthread__destroy_tsd(self);
|
|
|
|
|
2007-12-24 17:46:28 +03:00
|
|
|
/* Signal our exit. */
|
2003-01-18 13:32:11 +03:00
|
|
|
self->pt_exitval = retval;
|
2004-08-12 14:54:13 +04:00
|
|
|
if (self->pt_flags & PT_FLAG_DETACHED) {
|
|
|
|
self->pt_state = PT_STATE_DEAD;
|
2003-02-27 01:02:48 +03:00
|
|
|
name = self->pt_name;
|
|
|
|
self->pt_name = NULL;
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread_mutex_lock(&pthread__deadqueue_lock);
|
|
|
|
PTQ_INSERT_TAIL(&pthread__deadqueue, self, pt_deadq);
|
|
|
|
pthread_mutex_unlock(&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;
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread_cond_broadcast(&self->pt_joiners);
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
/* Note: name will be freed by the joiner. */
|
2006-12-23 08:14:46 +03:00
|
|
|
_lwp_exit();
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
out:
|
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;
|
2007-12-24 17:46:28 +03:00
|
|
|
int error;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2003-01-18 13:32:11 +03:00
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (thread == self)
|
|
|
|
return EDEADLK;
|
|
|
|
|
2007-12-24 17:46:28 +03:00
|
|
|
self->pt_droplock = &thread->pt_lock;
|
|
|
|
pthread_mutex_lock(&thread->pt_lock);
|
|
|
|
for (;;) {
|
|
|
|
if (thread->pt_state == PT_STATE_ZOMBIE)
|
|
|
|
break;
|
|
|
|
if (thread->pt_state == PT_STATE_DEAD) {
|
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
|
|
|
self->pt_droplock = NULL;
|
|
|
|
return ESRCH;
|
|
|
|
}
|
|
|
|
if ((thread->pt_flags & PT_FLAG_DETACHED) != 0) {
|
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
|
|
|
self->pt_droplock = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
error = pthread_cond_wait(&thread->pt_joiners,
|
|
|
|
&thread->pt_lock);
|
2007-12-24 19:04:20 +03:00
|
|
|
if (error != 0) {
|
|
|
|
pthread__errorfunc(__FILE__, __LINE__,
|
|
|
|
__func__, "unexpected return from cond_wait()");
|
2007-12-24 17:46:28 +03:00
|
|
|
}
|
2007-12-24 19:04:20 +03:00
|
|
|
|
2007-08-16 02:48:52 +04:00
|
|
|
}
|
2008-06-25 15:06:34 +04:00
|
|
|
pthread__testcancel(self);
|
2007-08-16 02:48:52 +04:00
|
|
|
if (valptr != NULL)
|
|
|
|
*valptr = thread->pt_exitval;
|
2007-12-24 17:46:28 +03:00
|
|
|
/* pthread__reap() will drop the lock. */
|
|
|
|
pthread__reap(thread);
|
|
|
|
self->pt_droplock = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pthread__reap(pthread_t thread)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
|
2007-08-16 02:48:52 +04:00
|
|
|
name = thread->pt_name;
|
|
|
|
thread->pt_name = NULL;
|
|
|
|
thread->pt_state = PT_STATE_DEAD;
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&pthread__deadqueue_lock);
|
2007-08-16 16:01:49 +04:00
|
|
|
PTQ_INSERT_HEAD(&pthread__deadqueue, thread, pt_deadq);
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&pthread__deadqueue_lock);
|
2007-12-24 17:46:28 +03:00
|
|
|
|
2007-08-16 02:48:52 +04:00
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_equal(pthread_t t1, pthread_t t2)
|
|
|
|
{
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub))
|
|
|
|
return __libc_thr_equal_stub(t1, t2);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/* Nothing special here. */
|
|
|
|
return (t1 == t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_detach(pthread_t thread)
|
|
|
|
{
|
|
|
|
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2003-01-18 13:32:11 +03:00
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&thread->pt_lock);
|
2007-02-22 01:31:38 +03:00
|
|
|
thread->pt_flags |= PT_FLAG_DETACHED;
|
2007-12-24 17:46:28 +03:00
|
|
|
if (thread->pt_state == PT_STATE_ZOMBIE) {
|
|
|
|
/* pthread__reap() will drop the lock. */
|
|
|
|
pthread__reap(thread);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Not valid for threads to be waiting in
|
|
|
|
* pthread_join() (there are intractable
|
|
|
|
* sync issues from the application
|
|
|
|
* perspective), but give those threads
|
|
|
|
* a chance anyway.
|
|
|
|
*/
|
|
|
|
pthread_cond_broadcast(&thread->pt_joiners);
|
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
|
|
|
}
|
2007-03-02 21:53:51 +03:00
|
|
|
|
2007-12-24 17:46:28 +03:00
|
|
|
return 0;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-27 01:02:48 +03:00
|
|
|
int
|
|
|
|
pthread_getname_np(pthread_t thread, char *name, size_t len)
|
|
|
|
{
|
|
|
|
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2003-02-27 01:02:48 +03:00
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
if (thread->pt_magic != PT_MAGIC)
|
|
|
|
return EINVAL;
|
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&thread->pt_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
if (thread->pt_name == NULL)
|
|
|
|
name[0] = '\0';
|
|
|
|
else
|
|
|
|
strlcpy(name, thread->pt_name, len);
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_setname_np(pthread_t thread, const char *name, void *arg)
|
|
|
|
{
|
|
|
|
char *oldname, *cp, newname[PTHREAD_MAX_NAMELEN_NP];
|
|
|
|
int namelen;
|
|
|
|
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2003-02-27 01:02:48 +03:00
|
|
|
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;
|
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&thread->pt_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
oldname = thread->pt_name;
|
|
|
|
thread->pt_name = cp;
|
2007-11-07 03:55:22 +03:00
|
|
|
(void)_lwp_setname(thread->pt_lid, cp);
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
2003-02-27 01:02:48 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub))
|
|
|
|
return (pthread_t)__libc_thr_self_stub();
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
return pthread__self();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_cancel(pthread_t thread)
|
|
|
|
{
|
|
|
|
|
2007-08-16 17:54:16 +04:00
|
|
|
if (pthread__find(thread) != 0)
|
2005-10-16 04:37:52 +04:00
|
|
|
return ESRCH;
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&thread->pt_lock);
|
2006-12-23 08:14:46 +03:00
|
|
|
thread->pt_flags |= PT_FLAG_CS_PENDING;
|
|
|
|
if ((thread->pt_flags & PT_FLAG_CS_DISABLED) == 0) {
|
|
|
|
thread->pt_cancel = 1;
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
2006-12-23 08:14:46 +03:00
|
|
|
_lwp_wakeup(thread->pt_lid);
|
|
|
|
} else
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&thread->pt_lock);
|
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
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub))
|
|
|
|
return __libc_thr_setcancelstate_stub(state, oldstate);
|
|
|
|
|
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
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&self->pt_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
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) {
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread__cancelled();
|
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
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
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;
|
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_lock(&self->pt_lock);
|
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) {
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread__cancelled();
|
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
|
|
|
|
2007-09-09 02:49:50 +04:00
|
|
|
pthread_mutex_unlock(&self->pt_lock);
|
2007-08-04 17:37:48 +04:00
|
|
|
|
2003-07-22 02:24:09 +04:00
|
|
|
return retval;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread_testcancel(void)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
if (self->pt_cancel)
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread__cancelled();
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* POSIX requires that certain functions return an error rather than
|
|
|
|
* invoking undefined behavior even when handed completely bogus
|
2012-11-04 03:42:27 +04:00
|
|
|
* pthread_t values, e.g. stack garbage.
|
2003-01-18 13:32:11 +03:00
|
|
|
*/
|
|
|
|
int
|
2007-08-16 17:54:16 +04:00
|
|
|
pthread__find(pthread_t id)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
pthread_t target;
|
2012-11-04 03:42:27 +04:00
|
|
|
int error;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2007-10-16 17:41:18 +04:00
|
|
|
pthread_rwlock_rdlock(&pthread__alltree_lock);
|
2012-11-04 03:42:27 +04:00
|
|
|
target = rb_tree_find_node(&pthread__alltree, id);
|
|
|
|
error = (target && target->pt_state != PT_STATE_DEAD) ? 0 : ESRCH;
|
2007-10-16 17:41:18 +04:00
|
|
|
pthread_rwlock_unlock(&pthread__alltree_lock);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2012-11-04 03:42:27 +04:00
|
|
|
return error;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__testcancel(pthread_t self)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (self->pt_cancel)
|
2007-12-24 17:46:28 +03:00
|
|
|
pthread__cancelled();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
pthread__cancelled(void)
|
|
|
|
{
|
|
|
|
pthread_mutex_t *droplock;
|
|
|
|
pthread_t self;
|
|
|
|
|
|
|
|
self = pthread__self();
|
|
|
|
droplock = self->pt_droplock;
|
|
|
|
self->pt_droplock = NULL;
|
|
|
|
|
|
|
|
if (droplock != NULL && pthread_mutex_held_np(droplock))
|
|
|
|
pthread_mutex_unlock(droplock);
|
|
|
|
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-22 17:19:27 +03:00
|
|
|
int *
|
|
|
|
pthread__errno(void)
|
|
|
|
{
|
|
|
|
pthread_t self;
|
|
|
|
|
2013-03-21 20:49:11 +04:00
|
|
|
if (__predict_false(__uselibcstub)) {
|
|
|
|
pthread__errorfunc(__FILE__, __LINE__, __func__,
|
|
|
|
"pthread__errno() requires linking with -lpthread");
|
|
|
|
return __libc_thr_errno_stub();
|
|
|
|
}
|
|
|
|
|
2008-03-22 17:19:27 +03:00
|
|
|
self = pthread__self();
|
|
|
|
|
|
|
|
return &(self->pt_errno);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread__park(pthread_t self, pthread_mutex_t *lock,
|
2007-08-04 17:37:48 +04:00
|
|
|
pthread_queue_t *queue, const struct timespec *abstime,
|
|
|
|
int cancelpt, const void *hint)
|
2006-12-23 08:14:46 +03:00
|
|
|
{
|
2007-08-07 23:04:21 +04:00
|
|
|
int rv, error;
|
2007-09-07 18:09:27 +04:00
|
|
|
void *obj;
|
2006-12-23 08:14:46 +03:00
|
|
|
|
2007-11-13 20:20:08 +03:00
|
|
|
/*
|
|
|
|
* For non-interlocked release of mutexes we need a store
|
|
|
|
* barrier before incrementing pt_blocking away from zero.
|
2008-05-25 21:05:28 +04:00
|
|
|
* This is provided by pthread_mutex_unlock().
|
2007-11-13 20:20:08 +03:00
|
|
|
*/
|
2008-05-25 21:05:28 +04:00
|
|
|
self->pt_willpark = 1;
|
|
|
|
pthread_mutex_unlock(lock);
|
|
|
|
self->pt_willpark = 0;
|
2007-11-13 20:20:08 +03:00
|
|
|
self->pt_blocking++;
|
|
|
|
|
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.
|
2007-08-04 17:37:48 +04:00
|
|
|
*
|
2008-05-25 21:05:28 +04:00
|
|
|
* It is fine to test the value of pt_sleepobj without
|
|
|
|
* holding any locks, because:
|
2007-08-04 17:37:48 +04:00
|
|
|
*
|
|
|
|
* o Only the blocking thread (this thread) ever sets them
|
|
|
|
* to a non-NULL value.
|
|
|
|
*
|
|
|
|
* o Other threads may set them NULL, but if they do so they
|
|
|
|
* must also make this thread return from _lwp_park.
|
|
|
|
*
|
|
|
|
* o _lwp_park, _lwp_unpark and _lwp_unpark_all are system
|
|
|
|
* calls and all make use of spinlocks in the kernel. So
|
|
|
|
* these system calls act as full memory barriers, and will
|
|
|
|
* ensure that the calling CPU's store buffers are drained.
|
|
|
|
* In combination with the spinlock release before unpark,
|
|
|
|
* this means that modification of pt_sleepobj/onq by another
|
|
|
|
* thread will become globally visible before that thread
|
|
|
|
* schedules an unpark operation on this thread.
|
2007-08-07 23:04:21 +04:00
|
|
|
*
|
|
|
|
* Note: the test in the while() statement dodges the park op if
|
|
|
|
* we have already been awoken, unless there is another thread to
|
|
|
|
* awaken. This saves a syscall - if we were already awakened,
|
|
|
|
* the next call to _lwp_park() would need to return early in order
|
|
|
|
* to eat the previous wakeup.
|
2006-12-24 21:39:45 +03:00
|
|
|
*/
|
|
|
|
rv = 0;
|
2008-05-25 21:05:28 +04:00
|
|
|
do {
|
2007-08-07 23:04:21 +04:00
|
|
|
/*
|
|
|
|
* If we deferred unparking a thread, arrange to
|
|
|
|
* have _lwp_park() restart it before blocking.
|
|
|
|
*/
|
2014-02-01 00:44:01 +04:00
|
|
|
error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, abstime,
|
|
|
|
self->pt_unpark, hint, hint);
|
2007-08-07 23:04:21 +04:00
|
|
|
self->pt_unpark = 0;
|
|
|
|
if (error != 0) {
|
2006-12-24 21:39:45 +03:00
|
|
|
switch (rv = errno) {
|
|
|
|
case EINTR:
|
|
|
|
case EALREADY:
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case ETIMEDOUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OOPS("_lwp_park failed");
|
|
|
|
break;
|
|
|
|
}
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
2007-03-15 02:33:42 +03:00
|
|
|
/* Check for cancellation. */
|
2007-08-16 17:54:16 +04:00
|
|
|
if (cancelpt && self->pt_cancel)
|
|
|
|
rv = EINTR;
|
2008-05-25 21:05:28 +04: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,
|
2007-08-04 17:37:48 +04:00
|
|
|
* then remove ourself. Again, it's safe to do the test
|
|
|
|
* without holding any locks.
|
2006-12-24 21:39:45 +03:00
|
|
|
*/
|
2008-05-25 21:05:28 +04:00
|
|
|
if (__predict_false(self->pt_sleepobj != NULL)) {
|
|
|
|
pthread_mutex_lock(lock);
|
|
|
|
if ((obj = self->pt_sleepobj) != NULL) {
|
2007-08-04 17:37:48 +04:00
|
|
|
PTQ_REMOVE(queue, self, pt_sleep);
|
|
|
|
self->pt_sleepobj = NULL;
|
2007-09-07 18:09:27 +04:00
|
|
|
if (obj != NULL && self->pt_early != NULL)
|
|
|
|
(*self->pt_early)(obj);
|
2007-08-04 17:37:48 +04:00
|
|
|
}
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread_mutex_unlock(lock);
|
2007-03-06 02:55:40 +03:00
|
|
|
}
|
2007-09-07 18:09:27 +04:00
|
|
|
self->pt_early = NULL;
|
2007-11-13 20:20:08 +03:00
|
|
|
self->pt_blocking--;
|
2008-05-25 21:05:28 +04:00
|
|
|
membar_sync();
|
2006-12-23 08:14:46 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread__unpark(pthread_queue_t *queue, pthread_t self,
|
|
|
|
pthread_mutex_t *interlock)
|
2006-12-23 08:14:46 +03:00
|
|
|
{
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread_t target;
|
2008-08-02 20:02:26 +04:00
|
|
|
u_int max;
|
|
|
|
size_t nwaiters;
|
2008-05-25 21:05:28 +04:00
|
|
|
|
|
|
|
max = pthread__unpark_max;
|
|
|
|
nwaiters = self->pt_nwaiters;
|
|
|
|
target = PTQ_FIRST(queue);
|
|
|
|
if (nwaiters == max) {
|
|
|
|
/* Overflow. */
|
|
|
|
(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
|
|
|
|
__UNVOLATILE(&interlock->ptm_waiters));
|
|
|
|
nwaiters = 0;
|
2007-03-15 02:33:42 +03:00
|
|
|
}
|
|
|
|
target->pt_sleepobj = NULL;
|
2008-05-25 21:05:28 +04:00
|
|
|
self->pt_waiters[nwaiters++] = target->pt_lid;
|
|
|
|
PTQ_REMOVE(queue, target, pt_sleep);
|
|
|
|
self->pt_nwaiters = nwaiters;
|
|
|
|
pthread__mutex_deferwake(self, interlock);
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread__unpark_all(pthread_queue_t *queue, pthread_t self,
|
|
|
|
pthread_mutex_t *interlock)
|
2006-12-23 08:14:46 +03:00
|
|
|
{
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread_t target;
|
2008-08-02 20:02:26 +04:00
|
|
|
u_int max;
|
|
|
|
size_t nwaiters;
|
2008-05-25 21:05:28 +04:00
|
|
|
|
|
|
|
max = pthread__unpark_max;
|
|
|
|
nwaiters = self->pt_nwaiters;
|
|
|
|
PTQ_FOREACH(target, queue, pt_sleep) {
|
|
|
|
if (nwaiters == max) {
|
|
|
|
/* Overflow. */
|
|
|
|
(void)_lwp_unpark_all(self->pt_waiters, nwaiters,
|
|
|
|
__UNVOLATILE(&interlock->ptm_waiters));
|
|
|
|
nwaiters = 0;
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
2008-05-25 21:05:28 +04:00
|
|
|
target->pt_sleepobj = NULL;
|
|
|
|
self->pt_waiters[nwaiters++] = target->pt_lid;
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
2008-05-25 21:05:28 +04:00
|
|
|
self->pt_nwaiters = nwaiters;
|
|
|
|
PTQ_INIT(queue);
|
|
|
|
pthread__mutex_deferwake(self, interlock);
|
2006-12-23 08:14:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef OOPS
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
static void
|
|
|
|
pthread__initmainstack(void)
|
2007-08-16 05:09:34 +04:00
|
|
|
{
|
2012-03-02 22:06:05 +04:00
|
|
|
struct rlimit slimit;
|
|
|
|
const AuxInfo *aux;
|
2012-03-08 20:33:45 +04:00
|
|
|
size_t size;
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
_DIAGASSERT(_dlauxinfo() != NULL);
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
if (getrlimit(RLIMIT_STACK, &slimit) == -1)
|
|
|
|
err(1, "Couldn't get stack resource consumption limits");
|
2012-03-08 20:33:45 +04:00
|
|
|
size = slimit.rlim_cur;
|
|
|
|
pthread__main.pt_stack.ss_size = size;
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) {
|
|
|
|
if (aux->a_type == AT_STACKBASE) {
|
|
|
|
pthread__main.pt_stack.ss_sp = (void *)aux->a_v;
|
2012-03-08 20:33:45 +04:00
|
|
|
#ifdef __MACHINE_STACK_GROWS_UP
|
|
|
|
pthread__main.pt_stack.ss_sp = (void *)aux->a_v;
|
|
|
|
#else
|
|
|
|
pthread__main.pt_stack.ss_sp = (char *)aux->a_v - size;
|
|
|
|
#endif
|
2012-03-02 22:06:05 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 05:09:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the slightly special stack for the "initial" thread, which
|
|
|
|
* runs on the normal system stack, and thus gets slightly different
|
|
|
|
* treatment.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pthread__initmain(pthread_t *newt)
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
pthread__initmainstack();
|
2007-11-13 18:57:10 +03:00
|
|
|
|
|
|
|
value = pthread__getenv("PTHREAD_STACKSIZE");
|
|
|
|
if (value != NULL) {
|
2007-08-16 05:09:34 +04:00
|
|
|
pthread__stacksize = atoi(value) * 1024;
|
2012-03-02 22:06:05 +04:00
|
|
|
if (pthread__stacksize > pthread__main.pt_stack.ss_size)
|
|
|
|
pthread__stacksize = pthread__main.pt_stack.ss_size;
|
2007-08-16 05:09:34 +04:00
|
|
|
}
|
|
|
|
if (pthread__stacksize == 0)
|
2012-03-02 22:06:05 +04:00
|
|
|
pthread__stacksize = pthread__main.pt_stack.ss_size;
|
|
|
|
pthread__stacksize += pthread__pagesize - 1;
|
2012-03-09 16:06:44 +04:00
|
|
|
pthread__stacksize &= ~(pthread__pagesize - 1);
|
2012-03-02 22:06:05 +04:00
|
|
|
if (pthread__stacksize < 4 * pthread__pagesize)
|
2007-08-16 05:09:34 +04:00
|
|
|
errx(1, "Stacksize limit is too low, minimum %zd kbyte.",
|
2012-03-02 22:06:05 +04:00
|
|
|
4 * pthread__pagesize / 1024);
|
2007-08-16 05:09:34 +04:00
|
|
|
|
2012-03-02 22:06:05 +04:00
|
|
|
*newt = &pthread__main;
|
2012-05-04 16:26:33 +04:00
|
|
|
#ifdef __HAVE___LWP_GETTCB_FAST
|
2012-03-02 22:06:05 +04:00
|
|
|
pthread__main.pt_tls = __lwp_gettcb_fast();
|
2012-05-04 16:26:33 +04:00
|
|
|
#else
|
2012-03-02 22:06:05 +04:00
|
|
|
pthread__main.pt_tls = _lwp_getprivate();
|
2011-03-10 02:10:05 +03:00
|
|
|
#endif
|
2012-05-04 16:26:33 +04:00
|
|
|
pthread__main.pt_tls->tcb_pthread = &pthread__main;
|
2007-08-16 05:09:34 +04:00
|
|
|
}
|
|
|
|
|
2012-11-04 03:42:27 +04:00
|
|
|
static signed int
|
2012-11-15 03:25:05 +04:00
|
|
|
/*ARGSUSED*/
|
2012-11-04 03:42:27 +04:00
|
|
|
pthread__cmp(void *ctx, const void *n1, const void *n2)
|
2007-10-16 17:41:18 +04:00
|
|
|
{
|
2012-11-06 13:03:03 +04:00
|
|
|
const uintptr_t p1 = (const uintptr_t)n1;
|
|
|
|
const uintptr_t p2 = (const uintptr_t)n2;
|
2009-04-01 14:13:24 +04:00
|
|
|
|
2012-11-04 03:42:27 +04:00
|
|
|
if (p1 < p2)
|
|
|
|
return -1;
|
|
|
|
if (p1 > p2)
|
2009-04-01 14:13:24 +04:00
|
|
|
return 1;
|
2012-11-04 03:42:27 +04:00
|
|
|
return 0;
|
2007-10-16 17:41:18 +04:00
|
|
|
}
|
|
|
|
|
2007-11-13 18:57:10 +03:00
|
|
|
/* Because getenv() wants to use locks. */
|
|
|
|
char *
|
|
|
|
pthread__getenv(const char *name)
|
|
|
|
{
|
2010-11-15 01:25:23 +03:00
|
|
|
extern char **environ;
|
|
|
|
size_t l_name, offset;
|
|
|
|
|
|
|
|
l_name = strlen(name);
|
|
|
|
for (offset = 0; environ[offset] != NULL; offset++) {
|
|
|
|
if (strncmp(name, environ[offset], l_name) == 0 &&
|
|
|
|
environ[offset][l_name] == '=') {
|
|
|
|
return environ[offset] + l_name + 1;
|
|
|
|
}
|
|
|
|
}
|
2007-11-13 18:57:10 +03:00
|
|
|
|
2010-11-15 01:25:23 +03:00
|
|
|
return NULL;
|
2007-11-13 18:57:10 +03:00
|
|
|
}
|
|
|
|
|
2008-05-25 21:05:28 +04:00
|
|
|
pthread_mutex_t *
|
|
|
|
pthread__hashlock(volatile const void *p)
|
|
|
|
{
|
|
|
|
uintptr_t v;
|
2007-11-13 18:57:10 +03:00
|
|
|
|
2008-05-25 21:05:28 +04:00
|
|
|
v = (uintptr_t)p;
|
|
|
|
return &hashlocks[((v >> 9) ^ (v >> 3)) & (NHASHLOCK - 1)].mutex;
|
|
|
|
}
|
2008-06-28 14:29:37 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
pthread__checkpri(int pri)
|
|
|
|
{
|
2008-08-02 20:02:26 +04:00
|
|
|
static int havepri;
|
|
|
|
static long min, max;
|
2008-06-28 14:29:37 +04:00
|
|
|
|
|
|
|
if (!havepri) {
|
|
|
|
min = sysconf(_SC_SCHED_PRI_MIN);
|
|
|
|
max = sysconf(_SC_SCHED_PRI_MAX);
|
|
|
|
havepri = 1;
|
|
|
|
}
|
|
|
|
return (pri < min || pri > max) ? EINVAL : 0;
|
|
|
|
}
|