#ifndef _LIB_PTHREAD_INT_H
#define _LIB_PTHREAD_INT_H
#include <sys/tls.h>
#include "pthread_types.h"
#include "pthread_queue.h"
#include "pthread_md.h"
#include <sys/atomic.h>
#include <sys/rbtree.h>
#include <sys/param.h>
#include <limits.h>
#include <lwp.h>
#include <signal.h>
#include <stdbool.h>
#include <machine/lwp_private.h>
#ifdef __GNUC__
#define PTHREAD_HIDE __attribute__ ((visibility("hidden")))
#else
#define PTHREAD_HIDE
#endif
#define PTHREAD__UNPARK_MAX 128
struct pt_clean_t {
PTQ_ENTRY(pt_clean_t) ptc_next;
void (*ptc_cleanup)(void *);
void *ptc_arg;
};
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_lock_ops {
void (*plo_init)(__cpu_simple_lock_t *);
int (*plo_try)(__cpu_simple_lock_t *);
void (*plo_unlock)(__cpu_simple_lock_t *);
void (*plo_lock)(__cpu_simple_lock_t *);
};
struct __pthread_st {
pthread_t pt_self;
#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
struct tls_tcb *pt_tls;
#endif
unsigned int pt_magic;
int pt_state;
int pt_flags;
_Atomic unsigned int pt_cancel;
int pt_errno;
stack_t pt_stack;
bool pt_stack_allocated;
bool pt_is_main;
size_t pt_guardsize;
void *pt_exitval;
char *pt_name;
struct pthread_lock_ops pt_lockops;
void *(*pt_func)(void *);
void *pt_arg;
PTQ_HEAD(, pt_clean_t) pt_cleanup_stack;
lwpid_t pt_lid;
PTQ_ENTRY(__pthread_st) pt_deadq;
rb_node_t pt_alltree __aligned(COHERENCY_UNIT);
PTQ_ENTRY(__pthread_st) pt_allq;
pthread_mutex_t pt_lock __aligned(COHERENCY_UNIT);
int pt_dummy1 __aligned(COHERENCY_UNIT);
struct lwpctl *pt_lwpctl;
volatile int pt_rwlocked;
void * volatile pt_sleepobj;
PTQ_ENTRY(__pthread_st) pt_sleep;
int pt_havespecific __aligned(COHERENCY_UNIT);
struct pt_specific {
void *pts_value;
PTQ_ENTRY(pt_specific) pts_next;
} pt_specific[];
};
#define PT_STATE_RUNNING 1
#define PT_STATE_ZOMBIE 5
#define PT_STATE_DEAD 6
#define PT_FLAG_DETACHED 0x0001
#define PT_FLAG_SCOPE_SYSTEM 0x0040
#define PT_FLAG_EXPLICIT_SCHED 0x0080
#define PT_FLAG_SUSPENDED 0x0100
#define PT_CANCEL_DISABLED __BIT(0)
#define PT_CANCEL_ASYNC __BIT(1)
#define PT_CANCEL_PENDING __BIT(2)
#define PT_CANCEL_CANCELLED __BIT(3)
#define PT_MAGIC 0x11110001
#define PT_DEAD 0xDEAD0001
#define PT_ATTR_MAGIC 0x22220002
#define PT_ATTR_DEAD 0xDEAD0002
extern size_t pthread__stacksize;
extern size_t pthread__guardsize;
extern size_t pthread__pagesize;
extern int pthread__nspins;
extern int pthread__concurrency;
extern int pthread__osrev;
extern size_t pthread__unpark_max;
extern int pthread_keys_max;
extern int __uselibcstub;
struct pthread__waiter {
struct pthread__waiter *volatile next;
lwpid_t volatile lid;
};
#define _UC_USER_BIT 30
#define _UC_USER (1LU << _UC_USER_BIT)
void pthread__unpark_all(pthread_queue_t *, pthread_t, pthread_mutex_t *)
PTHREAD_HIDE;
void pthread__unpark(pthread_queue_t *, pthread_t, pthread_mutex_t *)
PTHREAD_HIDE;
int pthread__park(pthread_t, pthread_mutex_t *, pthread_queue_t *,
const struct timespec *, int) PTHREAD_HIDE;
pthread_mutex_t *pthread__hashlock(volatile const void *) PTHREAD_HIDE;
void pthread__lockprim_init(void) PTHREAD_HIDE;
void pthread_lockinit(pthread_spin_t *) PTHREAD_HIDE;
static inline void pthread__spinlock(pthread_t, pthread_spin_t *)
__attribute__((__always_inline__));
static inline void
pthread__spinlock(pthread_t self, pthread_spin_t *lock)
{
if (__predict_true((*self->pt_lockops.plo_try)(lock)))
return;
(*self->pt_lockops.plo_lock)(lock);
}
static inline int pthread__spintrylock(pthread_t, pthread_spin_t *)
__attribute__((__always_inline__));
static inline int
pthread__spintrylock(pthread_t self, pthread_spin_t *lock)
{
return (*self->pt_lockops.plo_try)(lock);
}
static inline void pthread__spinunlock(pthread_t, pthread_spin_t *)
__attribute__((__always_inline__));
static inline void
pthread__spinunlock(pthread_t self, pthread_spin_t *lock)
{
(*self->pt_lockops.plo_unlock)(lock);
}
extern const struct pthread_lock_ops *pthread__lock_ops;
int pthread__simple_locked_p(__cpu_simple_lock_t *) PTHREAD_HIDE;
#define pthread__simple_lock_init(alp) (*pthread__lock_ops->plo_init)(alp)
#define pthread__simple_lock_try(alp) (*pthread__lock_ops->plo_try)(alp)
#define pthread__simple_unlock(alp) (*pthread__lock_ops->plo_unlock)(alp)
void pthread__testcancel(pthread_t) PTHREAD_HIDE;
int pthread__find(pthread_t) PTHREAD_HIDE;
#ifndef PTHREAD_MD_INIT
#define PTHREAD_MD_INIT
#endif
#ifndef _INITCONTEXT_U_MD
#define _INITCONTEXT_U_MD(ucp)
#endif
#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
#ifdef _PTHREAD_GETTCB_EXT
struct tls_tcb *_PTHREAD_GETTCB_EXT(void);
#endif
static inline pthread_t __constfunc
pthread__self(void)
{
#if defined(_PTHREAD_GETTCB_EXT)
struct tls_tcb * const tcb = _PTHREAD_GETTCB_EXT();
#elif defined(__HAVE___LWP_GETTCB_FAST)
struct tls_tcb * const tcb = __lwp_gettcb_fast();
#else
struct tls_tcb * const tcb = __lwp_getprivate_fast();
#endif
return (pthread_t)tcb->tcb_pthread;
}
#define pthread__abort() \
pthread__assertfunc(__FILE__, __LINE__, __func__, "unreachable")
#define pthread__assert(e) do { \
if (__predict_false(!(e))) \
pthread__assertfunc(__FILE__, __LINE__, __func__, #e); \
} while (0)
#define pthread__error(err, msg, e) do { \
if (__predict_false(!(e))) { \
pthread__errorfunc(__FILE__, __LINE__, __func__, msg); \
return (err); \
} \
} while (0)
void *pthread_tsd_init(size_t *) PTHREAD_HIDE;
void pthread__destroy_tsd(pthread_t) PTHREAD_HIDE;
void pthread__copy_tsd(pthread_t) PTHREAD_HIDE;
__dead void pthread__assertfunc(const char *, int, const char *, const char *)
PTHREAD_HIDE;
void pthread__errorfunc(const char *, int, const char *, const char *, ...)
__printflike(4, 5) PTHREAD_HIDE;
char *pthread__getenv(const char *) PTHREAD_HIDE;
__dead void pthread__cancelled(void) PTHREAD_HIDE;
void pthread__mutex_deferwake(pthread_t, pthread_mutex_t *,
struct pthread__waiter *) PTHREAD_HIDE;
int pthread__checkpri(int) PTHREAD_HIDE;
int pthread__add_specific(pthread_t, pthread_key_t, const void *) PTHREAD_HIDE;
#ifndef pthread__smt_pause
#define pthread__smt_pause() __nothing
#endif
#ifndef pthread__smt_wait
#define pthread__smt_wait() __nothing
#endif
#ifndef pthread__smt_wake
#define pthread__smt_wake() __nothing
#endif
#define RW_HAS_WAITERS 0x01
#define RW_WRITE_WANTED 0x02
#define RW_WRITE_LOCKED 0x04
#define RW_UNUSED 0x08
#define RW_FLAGMASK 0x0f
#define RW_READ_COUNT_SHIFT 4
#define RW_READ_INCR (1 << RW_READ_COUNT_SHIFT)
#define RW_THREAD ((uintptr_t)-RW_READ_INCR)
#endif