Skip to content

Commit 1c7ac88

Browse files
Add interface for querying numbers of delegated threads.
1 parent bdf2e8d commit 1c7ac88

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

include/qt_threadpool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ typedef enum {
1919
hw_pool_init_status hw_pool_init(uint32_t num_threads);
2020
void hw_pool_destroy();
2121
void run_on_current_pool(qt_threadpool_func_type func, void *arg);
22+
uint32_t get_num_delegated_threads();
2223

2324
#endif

src/threadpool.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,11 @@ API_FUNC __attribute__((no_sanitize("memory"))) void hw_pool_destroy() {
382382
atomic_store_explicit(&hw_pool.num_threads, 0, memory_order_release);
383383
}
384384

385-
// TODO: interface for querying about the owned thread pool.
386-
// At least let the user get the number of threads available.
387-
// If no thread pool is owned, just report a single thread.
385+
API_FUNC uint32_t get_num_delegated_threads() {
386+
if (owned_pool) return owned_pool->num_threads;
387+
// Every thread at least has itself available for work.
388+
return 1;
389+
}
388390

389391
// TODO: have the main thread fill the role of thread 0.
390392
// Instead of having the main thread wait/resume, swap in its thread-locals

test/internal/threadpool.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#include "qt_threadpool.h"
33

44
static int on_thread_test(void *arg) {
5+
test_check(get_num_delegated_threads() == 1);
56
printf("hello from thread\n");
67
return 0;
78
}
89

910
int main() {
11+
test_check(get_num_delegated_threads() == 1);
1012
hw_pool_init(2);
13+
test_check(get_num_delegated_threads() == 2);
1114
hw_pool_destroy();
15+
test_check(get_num_delegated_threads() == 1);
1216
hw_pool_init(2);
1317
run_on_current_pool(&on_thread_test, NULL);
1418
hw_pool_destroy();

0 commit comments

Comments
 (0)