/*-
* Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
* The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Nathan J. Williams and Andrew Doran.
*
* 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.
*/
/*
* The size of this structure needs to be no larger than struct
* __pthread_cleanup_store, defined in pthread.h.
*/
struct pt_clean_t {
PTQ_ENTRY(pt_clean_t) ptc_next;
void (*ptc_cleanup)(void *);
void *ptc_arg;
};
/* Private data for pthread_attr_t */
struct pthread_attr_private {
char ptap_name[PTHREAD_MAX_NAMELEN_NP];
void *ptap_namearg;
void *ptap_stackaddr;
size_t ptap_stacksize;
size_t ptap_guardsize;
struct sched_param ptap_sp;
int ptap_policy;
};
struct __pthread_st {
pthread_t pt_self; /* Must be first. */
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
struct tls_tcb *pt_tls; /* Thread Local Storage area */
#endif
unsigned int pt_magic; /* Magic number */
int pt_state; /* running, blocked, etc. */
int pt_flags; /* see PT_FLAG_* below */
_Atomic unsigned int pt_cancel; /* Cancellation */
int pt_errno; /* Thread-specific errno. */
stack_t pt_stack; /* Our stack */
bool pt_stack_allocated;
size_t pt_guardsize;
void *pt_exitval; /* Read by pthread_join() */
char *pt_name; /* Thread's name, set by the app. */
struct pthread_lock_ops pt_lockops;/* Cached to avoid PIC overhead */
void *(*pt_func)(void *);/* Function to call at start. */
void *pt_arg; /* Argument to pass at start. */
/* Stack of cancellation cleanup handlers and their arguments */
PTQ_HEAD(, pt_clean_t) pt_cleanup_stack;
/* LWP ID and entry on the list of all threads. */
lwpid_t pt_lid;
PTQ_ENTRY(__pthread_st) pt_deadq;
/*
* rbtree node and entry on the list of all threads. pt_alltree in
* its own cacheline, so pthread__find() is not needlessly impacted
* by threads going about their normal business. pt_allq is
* adjusted at the same time as pt_alltree.
*/
rb_node_t pt_alltree __aligned(COHERENCY_UNIT);
PTQ_ENTRY(__pthread_st) pt_allq;
/* Lock on state also gets its own line. */
pthread_mutex_t pt_lock __aligned(COHERENCY_UNIT);
/*
* General synchronization data. We try to align, as threads
* on other CPUs will access this data frequently.
*/
int pt_dummy1 __aligned(COHERENCY_UNIT);
struct lwpctl *pt_lwpctl; /* Kernel/user comms area */
volatile int pt_rwlocked; /* Handed rwlock successfully */
void * volatile pt_sleepobj; /* Object slept on */
PTQ_ENTRY(__pthread_st) pt_sleep;
/* Thread-specific data. Large so it sits close to the end. */
int pt_havespecific __aligned(COHERENCY_UNIT);
struct pt_specific {
void *pts_value;
PTQ_ENTRY(pt_specific) pts_next;
} pt_specific[];
};
/* Flag to be used in a ucontext_t's uc_flags indicating that
* the saved register state is "user" state only, not full
* trap state.
*/
#define _UC_USER_BIT 30
#define _UC_USER (1LU << _UC_USER_BIT)
#define _INITCONTEXT_U(ucp) do { \
(ucp)->uc_flags = _UC_CPU | _UC_STACK; \
_INITCONTEXT_U_MD(ucp) \
} while (0)
#if !defined(__HAVE_TLS_VARIANT_I) && !defined(__HAVE_TLS_VARIANT_II)
#error Either __HAVE_TLS_VARIANT_I or __HAVE_TLS_VARIANT_II must be defined
#endif
/*
* Bits in the owner field of the lock that indicate lock state. If the
* WRITE_LOCKED bit is clear, then the owner field is actually a count of
* the number of readers.
*/
#define RW_HAS_WAITERS 0x01 /* lock has waiters */
#define RW_WRITE_WANTED 0x02 /* >= 1 waiter is a writer */
#define RW_WRITE_LOCKED 0x04 /* lock is currently write locked */
#define RW_UNUSED 0x08 /* currently unused */