Skip to content

Commit 084a512

Browse files
committed
Replace explicit tls with thread_local
A straightforward replacement from the previous PR. Doing it first is for rebasing convenience given the multiplication of PRs.
1 parent 1ef52ed commit 084a512

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

otherlibs/systhreads/st_pthreads.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,6 @@ static void st_thread_join(st_thread_id thr)
7171
/* best effort: ignore errors */
7272
}
7373

74-
/* Thread-specific state */
75-
76-
typedef pthread_key_t st_tlskey;
77-
78-
static int st_tls_newkey(st_tlskey * res)
79-
{
80-
return pthread_key_create(res, NULL);
81-
}
82-
83-
Caml_inline void * st_tls_get(st_tlskey k)
84-
{
85-
return pthread_getspecific(k);
86-
}
87-
88-
Caml_inline void st_tls_set(st_tlskey k, void * v)
89-
{
90-
pthread_setspecific(k, v);
91-
}
92-
9374
/* The master lock. This is a mutex that is held most of the time,
9475
so we implement it in a slightly convoluted way to avoid
9576
all risks of busy-waiting. Also, we count the number of waiting

otherlibs/systhreads/st_stubs.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ struct caml_thread_struct {
8888

8989
typedef struct caml_thread_struct* caml_thread_t;
9090

91-
/* Thread-local key for accessing the current thread's [caml_thread_t] */
92-
st_tlskey caml_thread_key;
91+
/* The current thread's [caml_thread_t] */
92+
static _Thread_local caml_thread_t this_thread;
9393

9494
/* overall table for threads across domains */
9595
struct caml_thread_table {
@@ -210,7 +210,7 @@ static void caml_thread_leave_blocking_section(void)
210210
st_masterlock_acquire(&Thread_main_lock);
211211
/* Update Active_thread to point to the thread descriptor corresponding to
212212
the thread currently executing */
213-
Active_thread = st_tls_get(caml_thread_key);
213+
Active_thread = this_thread;
214214
/* Restore the runtime state from the curr_thread descriptor */
215215
caml_thread_restore_runtime_state();
216216
}
@@ -359,7 +359,7 @@ CAMLprim value caml_thread_initialize_domain(value v)
359359
new_thread->prev = new_thread;
360360
new_thread->backtrace_last_exn = Val_unit;
361361

362-
st_tls_set(caml_thread_key, new_thread);
362+
this_thread = new_thread;
363363

364364
Active_thread = new_thread;
365365
Tick_thread_running = 0;
@@ -397,9 +397,6 @@ CAMLprim value caml_thread_initialize(value unit)
397397
caml_failwith("caml_thread_initialize: cannot initialize Thread "
398398
"while several domains are running.");
399399

400-
/* Initialize the key to the [caml_thread_t] structure */
401-
st_tls_newkey(&caml_thread_key);
402-
403400
/* First initialise the systhread chain on this domain */
404401
caml_thread_initialize_domain(Val_unit);
405402

@@ -464,10 +461,10 @@ static void * caml_thread_start(void * v)
464461

465462
caml_init_domain_self(th->domain_id);
466463

467-
st_tls_set(caml_thread_key, th);
464+
this_thread = th;
468465

469466
st_masterlock_acquire(&Thread_main_lock);
470-
Active_thread = st_tls_get(caml_thread_key);
467+
Active_thread = this_thread;
471468
caml_thread_restore_runtime_state();
472469

473470
#ifdef POSIX_SIGNALS
@@ -572,7 +569,7 @@ CAMLexport int caml_c_thread_register(void)
572569
if (Caml_state == NULL) {
573570
caml_init_domain_self(0);
574571
};
575-
if (st_tls_get(caml_thread_key) != NULL) return 0;
572+
if (this_thread != NULL) return 0;
576573
/* Take master lock to protect access to the runtime */
577574
st_masterlock_acquire(&Thread_main_lock);
578575
/* Create a thread info block */
@@ -594,7 +591,7 @@ CAMLexport int caml_c_thread_register(void)
594591
Active_thread->next = th;
595592
}
596593
/* Associate the thread descriptor with the thread */
597-
st_tls_set(caml_thread_key, (void *) th);
594+
this_thread = th;
598595
/* Allocate the thread descriptor on the heap */
599596
th->descr = caml_thread_new_descriptor(Val_unit); /* no closure */
600597

@@ -619,13 +616,13 @@ CAMLexport int caml_c_thread_unregister(void)
619616
/* If Caml_state is not set, this thread was likely not registered */
620617
if (Caml_state == NULL) return 0;
621618

622-
th = st_tls_get(caml_thread_key);
619+
th = this_thread;
623620
/* Not registered? */
624621
if (th == NULL) return 0;
625622
/* Wait until the runtime is available */
626623
st_masterlock_acquire(&Thread_main_lock);
627624
/* Forget the thread descriptor */
628-
st_tls_set(caml_thread_key, NULL);
625+
this_thread = NULL;
629626
/* Remove thread info block from list of threads, and free it */
630627
caml_thread_remove_info(th);
631628

@@ -683,7 +680,7 @@ CAMLprim value caml_thread_yield(value unit)
683680
caml_raise_if_exception(caml_process_pending_signals_exn());
684681
caml_thread_save_runtime_state();
685682
st_thread_yield(&Thread_main_lock);
686-
Active_thread = st_tls_get(caml_thread_key);
683+
Active_thread = this_thread;
687684
caml_thread_restore_runtime_state();
688685
caml_raise_if_exception(caml_process_pending_signals_exn());
689686

0 commit comments

Comments
 (0)