#include <sys/types.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "libc_private.h"
#include "thr_private.h"
#ifdef DEBUG_THREAD_LIST
#define DBG_MSG stdout_debug
#else
#define DBG_MSG(x...)
#endif
struct thread_head _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
struct thread_head _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
int _thread_active_threads = 1;
int _thr_gc_count;
umtx_t _thr_list_lock;
#define MAX_CACHED_THREADS 100
static TAILQ_HEAD(, __pthread_s) free_threadq;
static umtx_t free_thread_lock;
static umtx_t tcb_lock;
static int free_thread_count = 0;
static int inited = 0;
static u_int64_t next_uniqueid = 1;
LIST_HEAD(thread_hash_head, __pthread_s);
#define HASH_QUEUES 128
static struct thread_hash_head thr_hashtable[HASH_QUEUES];
#define THREAD_HASH(thrd) (((unsigned long)thrd >> 12) % HASH_QUEUES)
static void thr_destroy(pthread_t curthread, pthread_t thread);
void
_thr_list_init(void)
{
int i;
_thr_gc_count = 0;
_thr_umtx_init(&_thr_list_lock);
TAILQ_INIT(&_thread_list);
TAILQ_INIT(&free_threadq);
_thr_umtx_init(&free_thread_lock);
_thr_umtx_init(&tcb_lock);
if (inited) {
for (i = 0; i < HASH_QUEUES; ++i)
LIST_INIT(&thr_hashtable[i]);
}
inited = 1;
}
void
_thr_gc(pthread_t curthread)
{
pthread_t td, td_next;
TAILQ_HEAD(, __pthread_s) worklist;
TAILQ_INIT(&worklist);
THREAD_LIST_LOCK(curthread);
for (td = TAILQ_FIRST(&_thread_gc_list); td != NULL; td = td_next) {
td_next = TAILQ_NEXT(td, gcle);
if (td->terminated == 0) {
continue;
}
_thr_stack_free(&td->attr);
if (((td->tlflags & TLFLAGS_DETACHED) != 0) &&
(td->refcount == 0)) {
THR_GCLIST_REMOVE(td);
THR_LIST_REMOVE(td);
TAILQ_INSERT_HEAD(&worklist, td, gcle);
}
}
THREAD_LIST_UNLOCK(curthread);
while ((td = TAILQ_FIRST(&worklist)) != NULL) {
TAILQ_REMOVE(&worklist, td, gcle);
if (td == _thr_initial) {
DBG_MSG("Initial thread won't be freed\n");
continue;
}
_thr_free(curthread, td);
}
}
pthread_t
_thr_alloc(pthread_t curthread)
{
pthread_t thread = NULL;
struct tls_tcb *tcb;
if (curthread != NULL) {
if (GC_NEEDED())
_thr_gc(curthread);
if (free_thread_count > 0) {
THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
if ((thread = TAILQ_FIRST(&free_threadq)) != NULL) {
TAILQ_REMOVE(&free_threadq, thread, tle);
free_thread_count--;
}
THR_LOCK_RELEASE(curthread, &free_thread_lock);
}
}
if (thread == NULL) {
thread = __malloc(sizeof(struct __pthread_s));
if (thread == NULL)
return (NULL);
}
if (curthread != NULL) {
THR_LOCK_ACQUIRE(curthread, &tcb_lock);
tcb = _tcb_ctor(thread, 0 );
THR_LOCK_RELEASE(curthread, &tcb_lock);
} else {
tcb = _tcb_ctor(thread, 1 );
}
if (tcb != NULL) {
memset(thread, 0, sizeof(*thread));
thread->tcb = tcb;
} else {
thr_destroy(curthread, thread);
thread = NULL;
}
return (thread);
}
void
_thr_free(pthread_t curthread, pthread_t thread)
{
DBG_MSG("Freeing thread %p\n", thread);
if (thread->name) {
__free(thread->name);
thread->name = NULL;
}
if (curthread != NULL) {
THR_LOCK_ACQUIRE(curthread, &tcb_lock);
_tcb_dtor(thread->tcb);
THR_LOCK_RELEASE(curthread, &tcb_lock);
} else {
_tcb_dtor(thread->tcb);
}
thread->tcb = NULL;
if ((curthread == NULL) || (free_thread_count >= MAX_CACHED_THREADS)) {
thr_destroy(curthread, thread);
} else {
THR_LOCK_ACQUIRE(curthread, &free_thread_lock);
TAILQ_INSERT_TAIL(&free_threadq, thread, tle);
free_thread_count++;
THR_LOCK_RELEASE(curthread, &free_thread_lock);
}
}
static void
thr_destroy(pthread_t curthread __unused, pthread_t thread)
{
__free(thread);
}
void
_thr_link(pthread_t curthread, pthread_t thread)
{
THREAD_LIST_LOCK(curthread);
thread->uniqueid = next_uniqueid++;
THR_LIST_ADD(thread);
_thread_active_threads++;
THREAD_LIST_UNLOCK(curthread);
}
void
_thr_unlink(pthread_t curthread, pthread_t thread)
{
THREAD_LIST_LOCK(curthread);
THR_LIST_REMOVE(thread);
_thread_active_threads--;
THREAD_LIST_UNLOCK(curthread);
}
void
_thr_hash_add(pthread_t thread)
{
struct thread_hash_head *head;
head = &thr_hashtable[THREAD_HASH(thread)];
LIST_INSERT_HEAD(head, thread, hle);
}
void
_thr_hash_remove(pthread_t thread)
{
LIST_REMOVE(thread, hle);
}
pthread_t
_thr_hash_find(pthread_t thread)
{
pthread_t td;
struct thread_hash_head *head;
head = &thr_hashtable[THREAD_HASH(thread)];
LIST_FOREACH(td, head, hle) {
if (td == thread)
return (thread);
}
return (NULL);
}
int
_thr_ref_add(pthread_t curthread, pthread_t thread,
int include_dead)
{
int ret;
if (thread == NULL)
return (EINVAL);
THREAD_LIST_LOCK(curthread);
if ((ret = _thr_find_thread(curthread, thread, include_dead)) == 0) {
thread->refcount++;
}
THREAD_LIST_UNLOCK(curthread);
return (ret);
}
void
_thr_ref_delete(pthread_t curthread, pthread_t thread)
{
THREAD_LIST_LOCK(curthread);
_thr_ref_delete_unlocked(curthread, thread);
THREAD_LIST_UNLOCK(curthread);
}
void
_thr_ref_delete_unlocked(pthread_t curthread __unused, pthread_t thread)
{
if (thread != NULL) {
thread->refcount--;
if ((thread->refcount == 0) && thread->state == PS_DEAD &&
(thread->tlflags & TLFLAGS_DETACHED) != 0)
THR_GCLIST_ADD(thread);
}
}
int
_thr_find_thread(pthread_t curthread __unused, pthread_t thread,
int include_dead)
{
pthread_t pthread;
if (thread == NULL)
return (EINVAL);
pthread = _thr_hash_find(thread);
if (pthread) {
if (include_dead == 0 && pthread->state == PS_DEAD) {
pthread = NULL;
}
}
return ((pthread != NULL) ? 0 : ESRCH);
}