1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 07:03:16 +00:00

Create pthread threads with detached attribute.

Rather than calling pthread_detach on a default thread.
This commit is contained in:
Calvin Rose 2022-11-11 11:01:59 -06:00
parent 76be8006a4
commit a3a42eebea
2 changed files with 11 additions and 2 deletions

View File

@ -557,6 +557,10 @@ void janet_ev_init_common(void) {
janet_vm.tq_capacity = 0;
janet_table_init_raw(&janet_vm.threaded_abstracts, 0);
janet_rng_seed(&janet_vm.ev_rng, 0);
#ifndef JANET_WINDOWS
pthread_attr_init(&janet_vm.new_thread_attr);
pthread_attr_setdetachstate(&janet_vm.new_thread_attr, PTHREAD_CREATE_DETACHED);
#endif
}
/* Common deinit code */
@ -566,6 +570,9 @@ void janet_ev_deinit_common(void) {
janet_free(janet_vm.listeners);
janet_vm.listeners = NULL;
janet_table_deinit(&janet_vm.threaded_abstracts);
#ifndef JANET_WINDOWS
pthread_attr_destroy(&janet_vm.new_thread_attr);
#endif
}
/* Short hand to yield to event loop */
@ -2072,12 +2079,11 @@ void janet_ev_threaded_call(JanetThreadedSubroutine fp, JanetEVGenericMessage ar
#else
init->write_pipe = janet_vm.selfpipe[1];
pthread_t waiter_thread;
int err = pthread_create(&waiter_thread, NULL, janet_thread_body, init);
int err = pthread_create(&waiter_thread, &janet_vm.new_thread_attr, janet_thread_body, init);
if (err) {
janet_free(init);
janet_panicf("%s", strerror(err));
}
pthread_detach(waiter_thread);
#endif
/* Increment ev refcount so we don't quit while waiting for a subprocess */

View File

@ -152,16 +152,19 @@ struct JanetVM {
#ifdef JANET_WINDOWS
void **iocp;
#elif defined(JANET_EV_EPOLL)
pthread_attr_t new_thread_attr;
JanetHandle selfpipe[2];
int epoll;
int timerfd;
int timer_enabled;
#elif defined(JANET_EV_KQUEUE)
pthread_attr_t new_thread_attr;
JanetHandle selfpipe[2];
int kq;
int timer;
int timer_enabled;
#else
pthread_attr_t new_thread_attr;
JanetHandle selfpipe[2];
struct pollfd *fds;
#endif