From 7aa4cea6f77a436f9028472677395881bed92138 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Thu, 23 Nov 2017 07:13:00 +0000 Subject: [PATCH] h_exit now passes --- lib/libpthread/pthread.c | 16 ++++------- lib/libpthread/pthread_int.h | 16 +++++++---- lib/libpthread/pthread_userspace.c | 46 +++++++++++++----------------- 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/lib/libpthread/pthread.c b/lib/libpthread/pthread.c index 66b850cff..e6af05685 100644 --- a/lib/libpthread/pthread.c +++ b/lib/libpthread/pthread.c @@ -275,7 +275,6 @@ 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; } @@ -1062,6 +1061,9 @@ pthread__errno(void) } ssize_t _sys_write(int, const void *, size_t); +#if defined(__minix) +#define _sys_write(a,b,c) write(a,b,c) +#endif /* defined(__minix) */ void pthread__assertfunc(const char *file, int line, const char *function, @@ -1081,11 +1083,7 @@ 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); @@ -1116,11 +1114,7 @@ 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); @@ -1350,7 +1344,7 @@ pthread__initmain(pthread_t *newt) 4 * pthread__pagesize / 1024); *newt = pthread__main; -#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) || !defined(__minix) +#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) || defined(__minix) #if defined(_PTHREAD_GETTCB_EXT) pthread__main->pt_tls = _PTHREAD_GETTCB_EXT(); #elif defined(__HAVE___LWP_GETTCB_FAST) @@ -1359,7 +1353,7 @@ pthread__initmain(pthread_t *newt) pthread__main->pt_tls = _lwp_getprivate(); #endif pthread__main->pt_tls->tcb_pthread = pthread__main; -#endif /* defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) || !defined(__minix) */ +#endif /* defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) || defined(__minix) */ } static signed int diff --git a/lib/libpthread/pthread_int.h b/lib/libpthread/pthread_int.h index f2cab6e7f..5fedfd8c0 100644 --- a/lib/libpthread/pthread_int.h +++ b/lib/libpthread/pthread_int.h @@ -95,7 +95,7 @@ struct pthread_lock_ops { struct __pthread_st { pthread_t pt_self; /* Must be first. */ -#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) +#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II) || defined(__minix) struct tls_tcb *pt_tls; /* Thread Local Storage area */ #endif unsigned int pt_magic; /* Magic number */ @@ -265,12 +265,16 @@ int pthread__find(pthread_t) PTHREAD_HIDE; struct tls_tcb *_PTHREAD_GETTCB_EXT(void); #endif +#if defined(__minix) +struct tls_tcb { + void *tcb_pthread; +}; +#endif /* !defined(__minix) */ + static inline pthread_t __constfunc pthread__self(void) { -#if defined(__minix) - return NULL; -#else +#if !defined(__minix) #if defined(_PTHREAD_GETTCB_EXT) struct tls_tcb * const tcb = _PTHREAD_GETTCB_EXT(); #elif defined(__HAVE___LWP_GETTCB_FAST) @@ -278,8 +282,10 @@ pthread__self(void) #else struct tls_tcb * const tcb = __lwp_getprivate_fast(); #endif - return (pthread_t)tcb->tcb_pthread; +#else + struct tls_tcb * const tcb = _lwp_getprivate(); #endif /* defined(__minix) */ + return (pthread_t)tcb->tcb_pthread; } #define pthread__abort() \ diff --git a/lib/libpthread/pthread_userspace.c b/lib/libpthread/pthread_userspace.c index 142f1076d..d70a3dc82 100644 --- a/lib/libpthread/pthread_userspace.c +++ b/lib/libpthread/pthread_userspace.c @@ -40,10 +40,11 @@ struct lwp { int flags; const char * name; ucontext_t context; + struct tls_tcb tls; }; static struct lwp lwp_threads[MAX_THREAD_POOL]; -static volatile lwpid_t current_thread = -1; +static volatile lwpid_t current_thread = 0; static int __minix_runnable(struct lwp* thread) @@ -62,15 +63,9 @@ static void __minix_schedule(int signal __unused) { static int last_pos = 0; + struct lwp* old = &lwp_threads[current_thread]; 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. */ @@ -81,21 +76,21 @@ __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? */ if (!__minix_runnable(&lwp_threads[pos])) { - assert(!"No runnable threads to schedule!"); + return; /* "No runnable threads to schedule. */ } } -// printf("%s:%d\n", __func__, __LINE__); + /* Point the current thread to the thread picked. */ + current_thread = pos; + /* Restore the next context of the thread picked. */ last_pos = pos; -// printf("%s:%d\n", __func__, __LINE__); - (void)setcontext(&(lwp_threads[pos].context)); + (void)swapcontext(&(old->context), &(lwp_threads[pos].context)); } void __pthread_init_minix(void) __attribute__((__constructor__, __used__)); @@ -109,23 +104,19 @@ __pthread_init_minix(void) struct itimerval nit; struct itimerval oit; + memset(&old_action, 0, sizeof(old_action)); + memset(&new_action, 0, sizeof(new_action)); + new_action.sa_handler = __minix_schedule; new_action.sa_flags = 0; r = sigaction(SIGVTALRM, &new_action, &old_action); - if (0 != r) { - int e = errno; - } - nit.it_value.tv_sec = 0; nit.it_value.tv_usec = 1; nit.it_interval.tv_sec = 0; nit.it_interval.tv_usec = 10; r = setitimer(ITIMER_VIRTUAL, &nit, &oit); - if (0 != r) { - int e = errno; - } -} +} lwpid_t _lwp_self(void) @@ -263,12 +254,13 @@ _lwp_getname(lwpid_t target, char * name, size_t len) void * _lwp_getprivate(void) { - return NULL; + return &lwp_threads[current_thread].tls; } void _lwp_setprivate(void *cookie) { + /* Not supported */ } int @@ -300,7 +292,6 @@ _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; } @@ -330,8 +321,11 @@ _lwp_kill(lwpid_t thread, int signal) int _lwp_exit(void) { - // FIXME - return -1; + lwp_threads[current_thread].flags &= ~SLOT_IN_USE; + __minix_schedule(SIGVTALRM); + + /* We reach this only if there is nothing left to schedule. */ + exit(0); } int @@ -352,7 +346,7 @@ _lwp_ctl(int features, struct lwpctl **address) int _sys_sched_yield(void) { - // FIXME + __minix_schedule(SIGVTALRM); return -1; }