pthread_wip

This commit is contained in:
2017-10-16 10:28:24 +00:00
parent 0686959e93
commit f64c80da9a
18 changed files with 96 additions and 29 deletions

View File

@@ -138,6 +138,7 @@ _MCOUNT_DECL(u_long frompc, u_long selfpc)
int s;
#endif
#if !defined(__minix)
#if defined(_REENTRANT) && !defined(_KERNEL)
if (__isthreaded) {
p = thr_getspecific(_gmonkey);
@@ -148,6 +149,7 @@ _MCOUNT_DECL(u_long frompc, u_long selfpc)
}
} else
#endif
#endif /* !defined(__minix) */
p = &_gmonparam;
/*
* check that we are profiling

View File

@@ -42,7 +42,7 @@ WARNS=5
CPPFLAGS+= -D_LIBC -DLIBC_SCCS -DSYSLIBC_SCCS -D_REENTRANT
CPPFLAGS+= -D_DIAGNOSTIC
.else
CPPFLAGS+= -D_LIBC
CPPFLAGS+= -D_LIBC -D_REENTRANT
.if defined(MLIBDIR)
CPPFLAGS+= -DMLIBDIR=\"${MLIBDIR}\"
.endif

View File

@@ -25,10 +25,8 @@ SRCS+= strtod.c
# private interfaces interfacing to libc
SRCS+= dtoa.c ldtoa.c hdtoa.c gdtoa.c
.if !defined(__MINIX)
# local locking implementation
SRCS+= gdtoa_locks.c
.endif # !defined(__MINIX)
# private interfaces
SRCS+= dmisc.c \

View File

@@ -20,7 +20,6 @@ SRCS+= _errno.c
.if defined(__MINIX)
# Unsupported by Minix.
# closefrom.c confstr.c extattr.c \
# pthread_atfork.c
#
# Not useful but portable
# disklabel.c
@@ -41,7 +40,7 @@ SRCS+= alarm.c alphasort.c arc4random.c assert.c asysctl.c \
isascii.c isatty.c isctype.c lockf.c nftw.c \
nice.c nlist.c nlist_aout.c nlist_coff.c nlist_ecoff.c nlist_elf32.c \
nlist_elf64.c opendir.c pause.c popen.c posix_spawn_sched.c \
posix_spawn_fileactions.c posix_spawnp.c psignal.c \
posix_spawn_fileactions.c posix_spawnp.c psignal.c pthread_atfork.c \
ptree.c pwcache.c pw_scan.c raise.c randomid.c rb.c readdir.c \
rewinddir.c scandir.c seekdir.c setdomainname.c \
sethostname.c setjmperr.c setmode.c setproctitle.c setprogname.c \

View File

@@ -467,8 +467,10 @@ arc4random_prng_create(void)
prng = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
if (prng == MAP_FAILED)
goto fail0;
#if !defined(__minix)
if (minherit(prng, size, MAP_INHERIT_ZERO) == -1)
goto fail1;
#endif /* !defined(__minix) */
return prng;

View File

@@ -215,10 +215,11 @@ static size_t malloc_pagemask;
#define ptr2idx(foo) \
(((size_t)(uintptr_t)(foo) >> malloc_pageshift)-malloc_origo)
#undef _MALLOC_LOCK /*LSC*/
#ifndef _MALLOC_LOCK
#define _MALLOC_LOCK()
#endif
#undef _MALLOC_UNLOCK /*LSC*/
#ifndef _MALLOC_UNLOCK
#define _MALLOC_UNLOCK()
#endif

View File

@@ -64,7 +64,9 @@ SRCS+= pthread_specific.c
SRCS+= pthread_spin.c
SRCS+= pthread_tsd.c
SRCS+= res_state.c
.if !defined(__MINIX)
SRCS+= sem.c
.endif # !defined(__MINIX)
# Architecture-dependent files
.if exists(${ARCHDIR}/pthread_md.S)
SRCS+= pthread_md.S
@@ -75,6 +77,11 @@ SRCS+= pthread_md.S
.if defined(__MINIX)
SRCS+= pthread_userspace.c
.PATH.c: ${.CURDIR}/../../common/lib/libc/arch/${ARCHSUBDIR}/atomic
.PATH.S: ${.CURDIR}/../../common/lib/libc/arch/${ARCHSUBDIR}/atomic
.PATH.c: ${.CURDIR}/../../common/lib/libc/atomic
.PATH.c: ${.CURDIR}/../libc/misc
.include "../../common/lib/libc/arch/${ARCHSUBDIR}/atomic/Makefile.inc"
.endif # defined(__MINIX)
# The PTHREAD__COMPAT flag builds a libpthread that can be dropped
# into a NetBSD 2/3/4 chroot with a NetBSD 5 or later kernel.
@@ -246,3 +253,4 @@ MLINKS+= pthread_testcancel.3 pthread_setcanceltype.3
# of the argument to setspecific() has to be discarded *somewhere*
# before returning it from getspecific().
CWARNFLAGS+= -Wno-cast-qual
NOCLANGERROR=

View File

@@ -41,6 +41,9 @@ __RCSID("$NetBSD: pthread.c,v 1.147 2015/05/29 16:05:13 christos Exp $");
#include <sys/lwpctl.h>
#include <sys/resource.h>
#include <sys/tls.h>
#if defined(__minix)
#include <minix/sys_config.h>
#endif /* defined(__minix) */
#include <assert.h>
#include <dlfcn.h>
@@ -272,6 +275,7 @@ pthread__child_callback(void)
* much. Anything that permits some pthread_* calls to work is
* merely being polite.
*/
printf("%s:%d\n", __func__, __LINE__);
pthread__started = 0;
}
@@ -379,11 +383,13 @@ pthread__getstack(pthread_t newthread, const pthread_attr_t *attr)
redzone = (char *)stackbase;
stackbase2 = (char *)stackbase + guardsize;
#endif
#if !defined(__minix) /* No support for mprotect yet */
if (allocated && guardsize &&
mprotect(redzone, guardsize, PROT_NONE) == -1) {
munmap(stackbase, stacksize + guardsize);
return EPERM;
}
#endif /* !defined(__minix) */
newthread->pt_stack.ss_size = stacksize;
newthread->pt_stack.ss_sp = stackbase2;
newthread->pt_guardsize = guardsize;
@@ -1075,7 +1081,11 @@ pthread__assertfunc(const char *file, int line, const char *function,
function ? function : "",
function ? "\"" : "");
#if !defined(__minix)
_sys_write(STDERR_FILENO, buf, (size_t)len);
#else
write(STDERR_FILENO, buf, (size_t)len);
#endif /* !defined(__minix) */
(void)kill(getpid(), SIGABRT);
_exit(1);
@@ -1106,7 +1116,11 @@ pthread__errorfunc(const char *file, int line, const char *function,
function ? "\"" : "");
if (pthread__diagassert & DIAGASSERT_STDERR)
#if !defined(__minix)
_sys_write(STDERR_FILENO, buf, len);
#else
write(STDERR_FILENO, buf, len);
#endif /* !defined(__minix) */
if (pthread__diagassert & DIAGASSERT_SYSLOG)
syslog(LOG_DEBUG | LOG_USER, "%s", buf);
@@ -1278,16 +1292,22 @@ pthread__unpark_all(pthread_queue_t *queue, pthread_t self,
static void
pthread__initmainstack(void)
{
#if !defined(__minix)
struct rlimit slimit;
#endif /* defined(__minix) */
const AuxInfo *aux;
size_t size;
_DIAGASSERT(_dlauxinfo() != NULL);
#if !defined(__minix)
if (getrlimit(RLIMIT_STACK, &slimit) == -1)
err(EXIT_FAILURE,
"Couldn't get stack resource consumption limits");
size = slimit.rlim_cur;
#else
size = DEFAULT_STACK_LIMIT;
#endif /* !defined(__minix) */
pthread__main->pt_stack.ss_size = size;
for (aux = _dlauxinfo(); aux->a_type != AT_NULL; ++aux) {

View File

@@ -224,7 +224,6 @@ fcntl(int fd, int cmd, ...)
return retval;
}
#if !defined(__minix)
int
fdatasync(int d)
{
@@ -238,7 +237,6 @@ fdatasync(int d)
return retval;
}
#endif /* !defined(__minix) */
int
fsync(int d)

View File

@@ -405,7 +405,11 @@ pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
do {
pthread__testcancel(self);
pthread_mutex_unlock(mutex);
#if !defined(__minix)
retval = _sys___nanosleep50(&diff, NULL);
#else
retval = nanosleep(&diff, NULL);
#endif /* ! defined(__minix) */
pthread_mutex_lock(mutex);
} while (abstime == NULL && retval == 0);
pthread__testcancel(self);

View File

@@ -54,7 +54,9 @@ int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
int _sys_sched_yield(void);
__strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
#if !defined(__minix)
__strong_alias(__sigprocmask14,pthread_sigmask)
#endif /* !defined(__minix) */
__strong_alias(__libc_thr_yield,pthread__sched_yield)
int
@@ -140,6 +142,7 @@ pthread_kill(pthread_t thread, int sig)
return 0;
}
#if !defined(__minix)
int
pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
@@ -147,6 +150,7 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
return errno;
return 0;
}
#endif /* !defined(__minix) */
int
pthread__sched_yield(void)

View File

@@ -49,7 +49,9 @@ __strong_alias(__libc_thr_setspecific,pthread_setspecific)
__strong_alias(__libc_thr_getspecific,pthread_getspecific)
__strong_alias(__libc_thr_curcpu,pthread_curcpu_np)
#if !defined(__minix)
__strong_alias(setcontext,pthread_setcontext)
#endif /* !defined(__minix) */
int
pthread_setspecific(pthread_key_t key, const void *value)
@@ -94,6 +96,7 @@ pthread_curcpu_np(void)
}
}
#if !defined(__minix)
/*
* Override setcontext so that pthread private pointer is preserved
*/
@@ -113,3 +116,4 @@ pthread_setcontext(const ucontext_t *ucp)
#endif /* _UC_TLSBASE */
return _sys_setcontext(ucp);
}
#endif /* !defined(__minix) */

View File

@@ -4,6 +4,7 @@
#include <sys/lwp.h>
#include <sys/lwpctl.h>
#include <sys/param.h>
#include <sys/ras.h>
#include <sys/syscall.h>
#include <assert.h>
@@ -31,6 +32,9 @@ int _sys_sched_yield(void);
int _sys_mq_send(mqd_t, const char *, size_t, unsigned);
ssize_t _sys_mq_receive(mqd_t, char *, size_t, unsigned *);
/* Work around kludge for pthread_cancelstub */
int pthread__cancel_stub_binder;
struct lwp {
int return_value;
int flags;
@@ -39,7 +43,7 @@ struct lwp {
};
static struct lwp lwp_threads[MAX_THREAD_POOL];
static lwpid_t current_thread = -1;
static volatile lwpid_t current_thread = -1;
static int
__minix_runnable(struct lwp* thread)
@@ -60,11 +64,13 @@ __minix_schedule(int signal __unused)
static int last_pos = 0;
int pos;
// printf("%s:%d\n", __func__, __LINE__);
if (-1 == current_thread) {
/* No pthread scheduled yet, so nothing to do. */
return;
}
// printf("%s:%d\n", __func__, __LINE__);
/* Select Next thread to run.
* Simply scan the array looking for a schedulable thread, and
* loopback to the start if we reach the end. */
@@ -75,6 +81,7 @@ __minix_schedule(int signal __unused)
}
}
// printf("%s:%d\n", __func__, __LINE__);
if (pos == last_pos) {
/* No other thread found to run, is the current one
* still runnable? */
@@ -83,36 +90,33 @@ __minix_schedule(int signal __unused)
}
}
// printf("%s:%d\n", __func__, __LINE__);
/* Restore the next context of the thread picked. */
last_pos = pos;
/* FIXME DO STUFF */
// printf("%s:%d\n", __func__, __LINE__);
(void)setcontext(&(lwp_threads[pos].context));
}
static void __attribute__((__constructor__, __used__))
__pthread_init(void)
void __pthread_init_minix(void) __attribute__((__constructor__, __used__));
void
__pthread_init_minix(void)
{
int r;
struct sigaction old_action;
struct sigaction new_action;
static struct sigaction old_action;
static struct sigaction new_action;
struct itimerval nit;
struct itimerval oit;
__libc_atomic_init();
__libc_thr_init();
printf("Start: Setting signal handler\n");
new_action.sa_handler = __minix_schedule;
new_action.sa_flags = 0;
r = sigaction(SIGVTALRM, &new_action, &old_action);
if (0 != r) {
int e = errno;
printf("Failure to install signal handler: %s\n", strerror(e));
}
printf("Start: Setting timer\n");
nit.it_value.tv_sec = 0;
nit.it_value.tv_usec = 1;
nit.it_interval.tv_sec = 0;
@@ -120,7 +124,6 @@ __pthread_init(void)
r = setitimer(ITIMER_VIRTUAL, &nit, &oit);
if (0 != r) {
int e = errno;
printf("Failure to set the virtual timer: %s\n", strerror(e));
}
}
@@ -284,13 +287,12 @@ _lwp_unpark(lwpid_t lwp, const void * hint)
ssize_t
_lwp_unpark_all(const lwpid_t * targets, size_t ntargets, const void * hint)
{
if (MAX_THREAD_POOL <= ntargets) {
errno = EINVAL;
return -1;
if (NULL == targets) {
return MAX_THREAD_POOL;
}
if (NULL == targets) {
errno = EFAULT;
if (MAX_THREAD_POOL <= ntargets) {
errno = EINVAL;
return -1;
}
@@ -298,6 +300,7 @@ _lwp_unpark_all(const lwpid_t * targets, size_t ntargets, const void * hint)
lwp_threads[targets[i]].flags |= LW_UNPARKED;
}
printf("%s:%d\n", __func__, __LINE__);
return 0;
}
@@ -359,7 +362,7 @@ sched_yield(void)
return _sys_sched_yield();
}
#if 0
#if 1 /* FIXME */
int
_sched_setaffinity(pid_t a, lwpid_t b, size_t c, const cpuset_t *d)
{
@@ -384,6 +387,14 @@ _sched_getparam(pid_t a, lwpid_t b, int *c, struct sched_param *d)
return -1;
}
int
rasctl(void *addr, size_t len, int op)
{
errno = EOPNOTSUPP;
return -1;
}
#if 0
int
_sys_mq_send(mqd_t a, const char *b, size_t c, unsigned d)
{
@@ -396,3 +407,4 @@ _sys_mq_receive(mqd_t a, char *b, size_t c, unsigned *d)
return -1;
}
#endif /* 0 */
#endif /* 0 */

View File

@@ -6,10 +6,10 @@
#include <unistd.h>
#ifdef __weak_alias
__weak_alias(fork, _fork)
__weak_alias(__fork, _fork)
#endif
pid_t fork(void)
pid_t __fork(void)
{
message m;

View File

@@ -14,6 +14,7 @@ _BSD_OWN_MK_=1
LDSTATIC?= -static
MKDYNAMICROOT?= no
NO_LIBGOMP?= yes
PTHREAD_CANCELSTUB?= no
BINMODE?= 755
NONBINMODE?= 644

View File

@@ -32,14 +32,18 @@ TESTS_C+= t_once
TESTS_C+= t_preempt
TESTS_SH+= t_resolv
TESTS_C+= t_rwlock
.if !defined(__MINIX)
TESTS_C+= t_sem
.endif # !defined(__MINIX)
TESTS_C+= t_sigmask
TESTS_C+= t_sigsuspend
TESTS_C+= t_siglongjmp
TESTS_C+= t_sleep
TESTS_C+= t_swapcontext
.if !defined(__MINIX)
LDADD.t_sem+= -lrt
.endif # !defined(__MINIX)
BINDIR= ${TESTSDIR}
PROGS= h_atexit

View File

@@ -163,7 +163,9 @@ ATF_TC_BODY(once3, tc)
act.sa_sigaction = handler;
sigemptyset(&act.sa_mask);
#if !defined(__minix)
act.sa_flags = SA_SIGINFO;
#endif /* !defined(__minix) */
sigaction(SIGALRM, &act, NULL);
timerclear(&it.it_value);

View File

@@ -111,13 +111,17 @@ ATF_TC_BODY(before_threads, tc)
act.sa_sigaction = upcalls_not_started_handler1;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGUSR2);
#if !defined(__minix)
act.sa_flags = SA_SIGINFO;
#endif /* !defined(__minix) */
ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
act.sa_sigaction = upcalls_not_started_handler2;
sigemptyset(&act.sa_mask);
#if !defined(__minix)
act.sa_flags = SA_SIGINFO;
#endif /* !defined(__minix) */
(void)sigaction(SIGUSR2, &act, NULL);
kill(getpid(), SIGUSR1);
@@ -182,13 +186,17 @@ ATF_TC_BODY(respected_while_running, tc)
act.sa_sigaction = respected_while_running_handler1;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGUSR2);
#if !defined(__minix)
act.sa_flags = SA_SIGINFO;
#endif /* !defined(__minix) */
ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0);
act.sa_sigaction = respected_while_running_handler2;
sigemptyset(&act.sa_mask);
#if !defined(__minix)
act.sa_flags = SA_SIGINFO;
#endif /* !defined(__minix) */
(void)sigaction(SIGUSR2, &act, NULL);
PTHREAD_REQUIRE(pthread_create(&thread, NULL,