From 90b055f4be619e5a3c76bbaccfe856280a934c93 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Fri, 6 Feb 2015 17:36:26 +0100 Subject: [PATCH] posixcompat: properly handle PTHREAD_MUTEX_RECURSIVE and BF thread id assignment. Signed-off-by: Simon Gerber --- include/barrelfish/threads.h | 1 + lib/barrelfish/debug.c | 4 ++-- lib/barrelfish/threads.c | 11 +++++++++++ lib/posixcompat/pthreads.c | 17 +++++++++++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/barrelfish/threads.h b/include/barrelfish/threads.h index 5ccf47e..0e29720 100644 --- a/include/barrelfish/threads.h +++ b/include/barrelfish/threads.h @@ -71,6 +71,7 @@ void thread_set_tls_key(int, void *); void *thread_get_tls_key(int); uintptr_t thread_id(void); +uintptr_t thread_get_id(struct thread *t); void thread_set_id(uintptr_t id); __END_DECLS diff --git a/lib/barrelfish/debug.c b/lib/barrelfish/debug.c index 2e9f2f0..a4b12c0 100644 --- a/lib/barrelfish/debug.c +++ b/lib/barrelfish/debug.c @@ -110,8 +110,8 @@ void debug_printf(const char *fmt, ...) char str[256]; size_t len; - len = snprintf(str, sizeof(str), "\033[34m%.*s.\033[31m%u\033[0m: ", DISP_NAME_LEN, disp_name(), - disp_get_core_id()); + len = snprintf(str, sizeof(str), "\033[34m%.*s.\033[31m%u.%lu\033[0m: ", DISP_NAME_LEN, disp_name(), + disp_get_core_id(), thread_id()); if (len < sizeof(str)) { va_start(argptr, fmt); vsnprintf(str + len, sizeof(str) - len, fmt, argptr); diff --git a/lib/barrelfish/threads.c b/lib/barrelfish/threads.c index 0c0950b..316ade1 100644 --- a/lib/barrelfish/threads.c +++ b/lib/barrelfish/threads.c @@ -93,6 +93,9 @@ static void thread_entry(thread_func_t start_func, void *start_data) assert(!"thread_exit returned"); } +/// int counter for assigning initial thread ids +static uintptr_t threadid = 0; + #ifndef NDEBUG /// Debugging assertions on thread queues static void check_queue(struct thread *queue) @@ -445,6 +448,9 @@ struct thread *thread_create_unrunnable(thread_func_t start_func, void *arg, newthread->stack_top = (char *)newthread->stack_top - (lvaddr_t)newthread->stack_top % STACK_ALIGNMENT; + // set thread's ID + newthread->id = threadid++; + // init registers registers_set_initial(&newthread->regs, newthread, (lvaddr_t)thread_entry, (lvaddr_t)newthread->stack_top, @@ -589,6 +595,11 @@ uintptr_t thread_id(void) return thread_self()->id; } +uintptr_t thread_get_id(struct thread *t) +{ + return t->id; +} + void thread_set_id(uintptr_t id) { struct thread *me = thread_self(); diff --git a/lib/posixcompat/pthreads.c b/lib/posixcompat/pthreads.c index 8112f2e..c1df329 100644 --- a/lib/posixcompat/pthreads.c +++ b/lib/posixcompat/pthreads.c @@ -18,6 +18,7 @@ struct pthread_mutex_attr struct pthread_mutex { struct thread_mutex mutex; int locked; + struct pthread_mutex_attr attrs; }; @@ -148,7 +149,15 @@ int pthread_mutex_init(pthread_mutex_t *mutex, thread_mutex_init(&(*mutex)->mutex); (*mutex)->locked = 0; - return 0; + if (attr && *attr) { + debug_printf("kind = %u\n", (*attr)->kind); + memcpy(&(*mutex)->attrs, *attr, sizeof(struct pthread_mutex_attr)); + } else { + (*mutex)->attrs.kind = PTHREAD_MUTEX_NORMAL; + (*mutex)->attrs.robustness = 0; + (*mutex)->attrs.pshared = PTHREAD_PROCESS_PRIVATE; + } + return 0; } int pthread_mutex_destroy(pthread_mutex_t *mutex) @@ -170,7 +179,11 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) (*mutex)->locked++; thread_mutex_unlock(&mutex_mutex); - thread_mutex_lock_nested(&(*mutex)->mutex); + if ((*mutex)->attrs.kind == PTHREAD_MUTEX_RECURSIVE) { + thread_mutex_lock_nested(&(*mutex)->mutex); + } else { + thread_mutex_lock(&(*mutex)->mutex); + } return 0; } -- 1.7.2.5