Skip to content

Commit

Permalink
Threads: use new libuv thread calls
Browse files Browse the repository at this point in the history
Removed the pthread calls for thread creation and affinity setting
and replaced them with libuv thread calls (from PR #31).
  • Loading branch information
kpamnany committed Nov 3, 2015
1 parent af3e15d commit b054524
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 42 deletions.
51 changes: 14 additions & 37 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -254,16 +221,26 @@ 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 *));
for (i = 0; i < jl_n_threads - 1; ++i) {
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
Expand Down
5 changes: 0 additions & 5 deletions src/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b054524

Please sign in to comment.