mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Remove need for semaphore in Thread
This commit is contained in:
+1
-6
@@ -45,12 +45,7 @@ gb_internal void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize
|
|||||||
slice_init(&pool->threads, a, thread_count);
|
slice_init(&pool->threads, a, thread_count);
|
||||||
for_array(i, pool->threads) {
|
for_array(i, pool->threads) {
|
||||||
Thread *t = &pool->threads[i];
|
Thread *t = &pool->threads[i];
|
||||||
thread_init(t);
|
thread_init_and_start(t, thread_pool_thread_proc, pool);
|
||||||
}
|
|
||||||
|
|
||||||
for_array(i, pool->threads) {
|
|
||||||
Thread *t = &pool->threads[i];
|
|
||||||
thread_start(t, thread_pool_thread_proc, pool);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-27
@@ -27,7 +27,6 @@ struct Thread {
|
|||||||
isize user_index;
|
isize user_index;
|
||||||
isize volatile return_value;
|
isize volatile return_value;
|
||||||
|
|
||||||
Semaphore * semaphore;
|
|
||||||
isize stack_size;
|
isize stack_size;
|
||||||
std::atomic<bool> is_running;
|
std::atomic<bool> is_running;
|
||||||
};
|
};
|
||||||
@@ -60,10 +59,9 @@ gb_internal void condition_wait_with_timeout(Condition *c, BlockingMutex *m, u32
|
|||||||
|
|
||||||
gb_internal u32 thread_current_id(void);
|
gb_internal u32 thread_current_id(void);
|
||||||
|
|
||||||
gb_internal void thread_init (Thread *t);
|
gb_internal void thread_init_and_start (Thread *t, ThreadProc *proc, void *data);
|
||||||
|
gb_internal void thread_init_and_start_with_stack(Thread *t, ThreadProc *proc, void *data, isize stack_size);
|
||||||
gb_internal void thread_destroy (Thread *t);
|
gb_internal void thread_destroy (Thread *t);
|
||||||
gb_internal void thread_start (Thread *t, ThreadProc *proc, void *data);
|
|
||||||
gb_internal void thread_start_with_stack(Thread *t, ThreadProc *proc, void *data, isize stack_size);
|
|
||||||
gb_internal void thread_join (Thread *t);
|
gb_internal void thread_join (Thread *t);
|
||||||
gb_internal bool thread_is_running (Thread const *t);
|
gb_internal bool thread_is_running (Thread const *t);
|
||||||
gb_internal void thread_set_name (Thread *t, char const *name);
|
gb_internal void thread_set_name (Thread *t, char const *name);
|
||||||
@@ -328,27 +326,12 @@ gb_internal gb_inline void yield(void) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
gb_internal void thread_init(Thread *t) {
|
|
||||||
gb_zero_item(t);
|
|
||||||
#if defined(GB_SYSTEM_WINDOWS)
|
|
||||||
t->win32_handle = INVALID_HANDLE_VALUE;
|
|
||||||
#else
|
|
||||||
t->posix_handle = 0;
|
|
||||||
#endif
|
|
||||||
t->semaphore = gb_alloc_item(heap_allocator(), Semaphore);
|
|
||||||
semaphore_init(t->semaphore);
|
|
||||||
}
|
|
||||||
|
|
||||||
gb_internal void thread_destroy(Thread *t) {
|
gb_internal void thread_destroy(Thread *t) {
|
||||||
thread_join(t);
|
thread_join(t);
|
||||||
semaphore_destroy(t->semaphore);
|
|
||||||
gb_free(heap_allocator(), t->semaphore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
gb_internal void gb__thread_run(Thread *t) {
|
gb_internal void private__thread_run(Thread *t) {
|
||||||
semaphore_release(t->semaphore);
|
|
||||||
t->return_value = t->proc(t);
|
t->return_value = t->proc(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,7 +339,7 @@ gb_internal void gb__thread_run(Thread *t) {
|
|||||||
gb_internal DWORD __stdcall internal_thread_proc(void *arg) {
|
gb_internal DWORD __stdcall internal_thread_proc(void *arg) {
|
||||||
Thread *t = cast(Thread *)arg;
|
Thread *t = cast(Thread *)arg;
|
||||||
t->is_running.store(true);
|
t->is_running.store(true);
|
||||||
gb__thread_run(t);
|
private__thread_run(t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -370,14 +353,20 @@ gb_internal void gb__thread_run(Thread *t) {
|
|||||||
|
|
||||||
Thread *t = cast(Thread *)arg;
|
Thread *t = cast(Thread *)arg;
|
||||||
t->is_running.store(true);
|
t->is_running.store(true);
|
||||||
gb__thread_run(t);
|
private__thread_run(t);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
gb_internal void thread_start(Thread *t, ThreadProc *proc, void *user_data) { thread_start_with_stack(t, proc, user_data, 0); }
|
gb_internal void thread_init_and_start(Thread *t, ThreadProc *proc, void *user_data) { thread_init_and_start_with_stack(t, proc, user_data, 0); }
|
||||||
|
|
||||||
gb_internal void thread_start_with_stack(Thread *t, ThreadProc *proc, void *user_data, isize stack_size) {
|
gb_internal void thread_init_and_start_with_stack(Thread *t, ThreadProc *proc, void *user_data, isize stack_size) {
|
||||||
|
gb_zero_item(t);
|
||||||
|
#if defined(GB_SYSTEM_WINDOWS)
|
||||||
|
t->win32_handle = INVALID_HANDLE_VALUE;
|
||||||
|
#else
|
||||||
|
t->posix_handle = 0;
|
||||||
|
#endif
|
||||||
GB_ASSERT(!t->is_running.load());
|
GB_ASSERT(!t->is_running.load());
|
||||||
GB_ASSERT(proc != NULL);
|
GB_ASSERT(proc != NULL);
|
||||||
t->proc = proc;
|
t->proc = proc;
|
||||||
@@ -391,16 +380,14 @@ gb_internal void thread_start_with_stack(Thread *t, ThreadProc *proc, void *user
|
|||||||
{
|
{
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
|
defer (pthread_attr_destroy(&attr));
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||||
if (stack_size != 0) {
|
if (stack_size != 0) {
|
||||||
pthread_attr_setstacksize(&attr, stack_size);
|
pthread_attr_setstacksize(&attr, stack_size);
|
||||||
}
|
}
|
||||||
pthread_create(&t->posix_handle, &attr, internal_thread_proc, t);
|
pthread_create(&t->posix_handle, &attr, internal_thread_proc, t);
|
||||||
pthread_attr_destroy(&attr);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
semaphore_wait(t->semaphore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_internal void thread_join(Thread *t) {
|
gb_internal void thread_join(Thread *t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user