From b054524aea23ccf7bcfae1d8544d514cd37b0d4a Mon Sep 17 00:00:00 2001 From: Kiran Pamnany Date: Tue, 3 Nov 2015 10:37:47 +0530 Subject: [PATCH] Threads: use new libuv thread calls Removed the pthread calls for thread creation and affinity setting and replaced them with libuv thread calls (from PR #31). --- src/threading.c | 51 ++++++++++++++----------------------------------- src/threading.h | 5 ----- 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/src/threading.c b/src/threading.c index 7017d3d18333a..6123f84333154 100644 --- a/src/threading.c +++ b/src/threading.c @@ -99,39 +99,6 @@ uint64_t *user_ticks; uint64_t *join_ticks; #endif -// create a thread and affinitize it if proc_num is specified -int ti_threadcreate(uv_thread_t *thread_id, int proc_num, - void (*thread_fun)(void *), void *thread_arg) -{ -#ifdef _OS_LINUX_ - pthread_attr_t attr; - pthread_attr_init(&attr); - - cpu_set_t cset; - if (proc_num >= 0) { - CPU_ZERO(&cset); - CPU_SET(proc_num, &cset); - pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cset); - } - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - return pthread_create(thread_id, &attr, thread_fun, thread_arg); -#else - return uv_thread_create(thread_id, thread_fun, thread_arg); -#endif -} - -// set thread affinity -void ti_threadsetaffinity(uint64_t thread_id, int proc_num) -{ -#ifdef _OS_LINUX_ - cpu_set_t cset; - - CPU_ZERO(&cset); - CPU_SET(proc_num, &cset); - pthread_setaffinity_np(thread_id, sizeof(cpu_set_t), &cset); -#endif -} - // thread function: used by all except the main thread void ti_threadfun(void *arg) { @@ -240,7 +207,7 @@ void jl_init_threading(void) void jl_start_threads(void) { - char *cp; + char *cp, mask[UV_CPU_SETSIZE]; int i, exclusive; uv_thread_t ptid; ti_threadarg_t **targs; @@ -254,8 +221,12 @@ void jl_start_threads(void) // exclusive use: affinitize threads, master thread on proc 0, rest // according to a 'compact' policy // non-exclusive: no affinity settings; let the kernel move threads about - if (exclusive) - ti_threadsetaffinity(uv_thread_self(), 0); + if (exclusive) { + memset(mask, 0, UV_CPU_SETSIZE); + mask[0] = 1; + ptid = uv_thread_self(); + uv_thread_setaffinity(&ptid, mask, NULL, UV_CPU_SETSIZE); + } // create threads targs = malloc((jl_n_threads - 1) * sizeof (ti_threadarg_t *)); @@ -263,7 +234,13 @@ void jl_start_threads(void) targs[i] = (ti_threadarg_t *)malloc(sizeof (ti_threadarg_t)); targs[i]->state = TI_THREAD_INIT; targs[i]->tid = i + 1; - ti_threadcreate(&ptid, exclusive ? i+1 : -1, ti_threadfun, targs[i]); + uv_thread_create(&ptid, ti_threadfun, targs[i]); + if (exclusive) { + memset(mask, 0, UV_CPU_SETSIZE); + mask[i+1] = 1; + uv_thread_setaffinity(&ptid, mask, NULL, UV_CPU_SETSIZE); + } + uv_thread_detach(&ptid); } // set up the world thread group diff --git a/src/threading.h b/src/threading.h index c719b0b11392c..8b1b803bab636 100644 --- a/src/threading.h +++ b/src/threading.h @@ -57,11 +57,6 @@ typedef struct { } ti_threadwork_t; -// basic functions for thread creation -int ti_threadcreate(uv_thread_t *thread_id, int proc_num, - void (*thread_fun)(void *), void *thread_arg); -void ti_threadsetaffinity(uint64_t thread_id, int proc_num); - // thread function void ti_threadfun(void *arg);