Skip to content

Commit b9ee37a

Browse files
Add thread-local governing hardware thread ownership.
1 parent 1a74e0d commit b9ee37a

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

include/qt_threadpool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ typedef enum {
1818

1919
hw_pool_init_status hw_pool_init(uint32_t num_threads);
2020
void hw_pool_destroy();
21-
void hw_pool_run_on_all(qt_threadpool_func_type func, void *arg);
21+
void run_on_current_pool(qt_threadpool_func_type func, void *arg);
2222

2323
#endif

src/threadpool.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ typedef struct {
7575
#endif
7676
} pool_header;
7777

78+
_Thread_local pool_header *owned_pool;
79+
7880
typedef struct {
7981
// 16 byte aligned to allow loading it in one atomic instruction
8082
// on architectures where that makes sense (most of them).
@@ -306,6 +308,7 @@ API_FUNC hw_pool_init_status hw_pool_init(uint32_t num_threads) {
306308
#ifdef QPOOL_USE_PTHREADS
307309
pthread_attr_destroy(&attr);
308310
#endif
311+
owned_pool = &hw_pool;
309312
return POOL_INIT_SUCCESS;
310313
cleanup_threads:
311314
if (i) {
@@ -348,6 +351,7 @@ API_FUNC hw_pool_init_status hw_pool_init(uint32_t num_threads) {
348351
}
349352

350353
API_FUNC __attribute__((no_sanitize("memory"))) void hw_pool_destroy() {
354+
owned_pool = NULL;
351355
uint32_t num_threads =
352356
atomic_load_explicit(&hw_pool.num_threads, memory_order_relaxed);
353357
char *buffer = atomic_load_explicit(&hw_pool.threads, memory_order_relaxed);
@@ -400,6 +404,10 @@ pool_run_on_all(pool_header *pool, qt_threadpool_func_type func, void *arg) {
400404
suspend_main_while_working(pool);
401405
}
402406

407+
API_FUNC void run_on_current_pool(qt_threadpool_func_type func, void *arg) {
408+
pool_run_on_all(owned_pool, func, arg);
409+
}
410+
403411
API_FUNC void hw_pool_run_on_all(qt_threadpool_func_type func, void *arg) {
404412
pool_run_on_all(&hw_pool, func, arg);
405413
}

test/internal/threadpool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main() {
1010
hw_pool_init(2);
1111
hw_pool_destroy();
1212
hw_pool_init(2);
13-
hw_pool_run_on_all(&on_thread_test, NULL);
13+
run_on_current_pool(&on_thread_test, NULL);
1414
hw_pool_destroy();
1515
printf("exited successfully\n");
1616
fflush(stdout);

0 commit comments

Comments
 (0)