diff --git a/src/backend/threadpool.c b/src/backend/threadpool.c index 7c7d48fd8..66ecd4e33 100644 --- a/src/backend/threadpool.c +++ b/src/backend/threadpool.c @@ -61,6 +61,12 @@ lumiera_threadpool_init(unsigned limit) threadpool.pool[i].max_threads = limit; threadpool.pool[i].working_thread_count = 0; threadpool.pool[i].idle_thread_count = 0; + + //TODO: configure each pools' pthread_attrs appropriately + pthread_attr_init (&threadpool.pool[i].pthread_attrs); + pthread_attr_setdetachstate (&threadpool.pool[i].pthread_attrs, PTHREAD_CREATE_DETACHED); + //cancel... + lumiera_mutex_init(&threadpool.pool[i].lock,"pool of threads", &NOBUG_FLAG(threadpool)); } } @@ -79,6 +85,7 @@ lumiera_threadpool_destroy(void) ECHO ("destroying the pool mutex"); lumiera_mutex_destroy (&threadpool.pool[i].lock, &NOBUG_FLAG (threadpool)); ECHO ("pool mutex destroyed"); + pthread_attr_destroy (&threadpool.pool[i].pthread_attrs); } } @@ -97,7 +104,8 @@ lumiera_threadpool_acquire_thread(enum lumiera_thread_class kind, if (threadpool.pool[kind].working_thread_count + threadpool.pool[kind].idle_thread_count < threadpool.pool[kind].max_threads) { - ret = lumiera_thread_new (kind, NULL, purpose, flag); + ret = lumiera_thread_new (kind, NULL, purpose, flag, + &threadpool.pool[kind].pthread_attrs); threadpool.pool[kind].working_thread_count++; ENSURE (ret, "did not create a valid thread"); } diff --git a/src/backend/threadpool.h b/src/backend/threadpool.h index b978e9e57..03b56a4fe 100644 --- a/src/backend/threadpool.h +++ b/src/backend/threadpool.h @@ -74,6 +74,7 @@ struct lumiera_threadpool_struct unsigned max_threads; unsigned working_thread_count; unsigned idle_thread_count; + pthread_attr_t pthread_attrs; } pool[LUMIERA_THREADCLASS_COUNT]; }; diff --git a/src/backend/threads.c b/src/backend/threads.c index b943d55fc..62f0d14a6 100644 --- a/src/backend/threads.c +++ b/src/backend/threads.c @@ -93,16 +93,6 @@ static void* pthread_runner (void* thread) } #endif -static pthread_once_t attr_once = PTHREAD_ONCE_INIT; -static pthread_attr_t attrs; - -static void thread_attr_init (void) -{ - pthread_attr_init (&attrs); - pthread_attr_setdetachstate (&attrs, PTHREAD_CREATE_DETACHED); - //cancel ... -} - #if 0 // TODO: rewrite this using lumiera_threadpool_acquire() LumieraThread @@ -171,27 +161,26 @@ LumieraThread lumiera_thread_new (enum lumiera_thread_class kind, LumieraReccondition finished, const char* purpose, - struct nobug_flag* flag) + struct nobug_flag* flag, + pthread_attr_t* attrs) { // TODO: do something with these: (void) purpose; (void) flag; REQUIRE (kind < LUMIERA_THREADCLASS_COUNT, "invalid thread kind specified: %d", kind); - - if (attr_once == PTHREAD_ONCE_INIT) - pthread_once (&attr_once, thread_attr_init); + REQUIRE (attrs, "invalid pthread attributes structure passed"); //REQUIRE (finished, "invalid finished flag passed"); + LumieraThread self = lumiera_malloc (sizeof (*self)); llist_init(&self->node); self->finished = finished; self->kind = kind; self->state = LUMIERA_THREADSTATE_IDLE; - //REQUIRE (&attrs); - //REQUIRE (&thread_loop); - int error = pthread_create (&self->id, &attrs, &thread_loop, self); + //REQUIRE (thread_loop); + int error = pthread_create (&self->id, attrs, &thread_loop, self); ENSURE(error == 0 || EAGAIN == error, "pthread returned %d:%s", error, strerror(error)); if (error) { diff --git a/src/backend/threads.h b/src/backend/threads.h index 416e160a0..e8b774299 100644 --- a/src/backend/threads.h +++ b/src/backend/threads.h @@ -139,7 +139,8 @@ LumieraThread lumiera_thread_new (enum lumiera_thread_class kind, LumieraReccondition finished, const char* purpose, - struct nobug_flag* flag); + struct nobug_flag* flag, + pthread_attr_t* attrs); LumieraThread lumiera_thread_destroy (LumieraThread self);