From c9c44242611b4a93f0a4125cbeb8ba6e1a63a390 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 7 Dec 2019 17:54:08 -0600 Subject: [PATCH] Add thread/self. --- src/core/thread.c | 49 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/core/thread.c b/src/core/thread.c index 5f2cd058..a3da7e21 100644 --- a/src/core/thread.c +++ b/src/core/thread.c @@ -45,6 +45,7 @@ struct JanetMailbox { }; static JANET_THREAD_LOCAL JanetMailbox *janet_vm_mailbox = NULL; +static JANET_THREAD_LOCAL JanetThread *janet_vm_thread_self = NULL; static JanetMailbox *janet_mailbox_create(JanetMailbox *parent, int refCount) { JanetMailbox *mailbox = malloc(sizeof(JanetMailbox)); @@ -82,20 +83,6 @@ static void janet_mailbox_ref(JanetMailbox *mailbox, int delta) { janet_mailbox_ref_with_lock(mailbox, delta); } -void janet_threads_init(void) { - if (NULL != janet_vm_mailbox) { - return; - } - janet_vm_mailbox = janet_mailbox_create(NULL, 1); -} - -void janet_threads_deinit(void) { - pthread_mutex_lock(&janet_vm_mailbox->lock); - janet_vm_mailbox->closed = 1; - janet_mailbox_ref_with_lock(janet_vm_mailbox, -1); - janet_vm_mailbox = NULL; -} - static void janet_close_thread(JanetThread *thread) { if (thread->mailbox) { janet_mailbox_ref(thread->mailbox, -1); @@ -348,10 +335,39 @@ static int janet_thread_start_child(JanetThread *thread) { } } +/* + * Setup/Teardown + */ + +void janet_threads_init(void) { + if (NULL == janet_vm_mailbox) { + janet_vm_mailbox = janet_mailbox_create(NULL, 1); + } +} + +void janet_threads_deinit(void) { + pthread_mutex_lock(&janet_vm_mailbox->lock); + janet_vm_mailbox->closed = 1; + janet_mailbox_ref_with_lock(janet_vm_mailbox, -1); + janet_vm_mailbox = NULL; + janet_vm_thread_self = NULL; +} + /* * Cfuns */ +static Janet cfun_thread_self(int32_t argc, Janet *argv) { + (void) argv; + janet_fixarity(argc, 0); + if (NULL == janet_vm_thread_self) { + janet_vm_thread_self = janet_make_thread(janet_vm_mailbox, janet_get_core_table("make-image-dict")); + janet_mailbox_ref(janet_vm_mailbox, 1); + janet_gcroot(janet_wrap_abstract(janet_vm_thread_self)); + } + return janet_wrap_abstract(janet_vm_thread_self); +} + static Janet cfun_thread_new(int32_t argc, Janet *argv) { janet_fixarity(argc, 0); (void) argv; @@ -419,6 +435,11 @@ static Janet janet_thread_getter(void *p, Janet key) { } static const JanetReg threadlib_cfuns[] = { + { + "thread/self", cfun_thread_self, + JDOC("(thread/self)\n\n" + "Get the current running thread.") + }, { "thread/new", cfun_thread_new, JDOC("(thread/new)\n\n"