#include <sys/param.h>
#include <sys/time.h>
#include <sys/lock.h>
#include <sys/resource.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <errno.h>
#include <pthread.h>
#include <pthread_np.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <tib.h>
#include <unistd.h>
#include "rthread.h"
REDIRECT_SYSCALL(sysctl);
REDIRECT_SYSCALL(getthrname);
REDIRECT_SYSCALL(setthrname);
void
pthread_set_name_np(pthread_t thread, const char *name)
{
pid_t tid = 0;
if (thread != pthread_self())
tid = thread->tib->tib_tid;
setthrname(tid, name);
}
void
pthread_get_name_np(pthread_t thread, char *name, size_t len)
{
pid_t tid = 0;
int ret;
if (thread != pthread_self())
tid = thread->tib->tib_tid;
ret = getthrname(tid, name, len);
if (ret == ERANGE && len > 0)
name[len-1] = '\0';
}
int
pthread_main_np(void)
{
return (!_threads_ready ||
(TIB_GET()->tib_thread_flags & TIB_THREAD_INITIAL_STACK) ? 1 : 0);
}
int
pthread_stackseg_np(pthread_t thread, stack_t *sinfo)
{
if (thread->stack) {
#ifdef MACHINE_STACK_GROWS_UP
sinfo->ss_sp = thread->stack->base;
#else
sinfo->ss_sp = (char *)thread->stack->base +
thread->stack->len;
#endif
sinfo->ss_size = thread->stack->len;
if (thread->stack->guardsize != 1)
sinfo->ss_size -= thread->stack->guardsize;
sinfo->ss_flags = 0;
return (0);
} else if (thread->tib->tib_thread_flags & TIB_THREAD_INITIAL_STACK) {
static struct _ps_strings _ps;
static struct rlimit rl;
static int gotself;
if (!_threads_ready)
_rthread_init();
if (gotself == 0) {
const int mib[2] = { CTL_VM, VM_PSSTRINGS };
size_t len;
if (getrlimit(RLIMIT_STACK, &rl) != 0)
return (EAGAIN);
len = sizeof(_ps);
if (sysctl(mib, 2, &_ps, &len, NULL, 0) != 0)
return (EAGAIN);
gotself = 1;
}
#ifdef MACHINE_STACK_GROWS_UP
sinfo->ss_sp = _ps.val;
#else
sinfo->ss_sp = (void *)ROUND_TO_PAGE((uintptr_t)_ps.val);
#endif
sinfo->ss_size = (size_t)rl.rlim_cur;
sinfo->ss_flags = 0;
return (0);
}
return (EAGAIN);
}