Skip to content

Commit e8c7ec2

Browse files
qukhanxnnpack-bot
authored andcommitted
Update pthreadpool_thread_(create|join) function signatures.
- `thrd_create` expects a thread function that returns an `int`. - `pthread_create` expects a thread function that returns a `void*`. Introduce the `pthreadpool_thread_return_t` type definition that maps to the correct type depending on the implementation that is used. PiperOrigin-RevId: 817810757
1 parent f73bcd7 commit e8c7ec2

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

src/pthreads.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static uint32_t thread_wrap_up(struct pthreadpool* threadpool,
432432
return curr_active_threads;
433433
}
434434

435-
static void* thread_main(void* arg);
435+
static pthreadpool_thread_return_t thread_main(void* arg);
436436

437437
static void ensure_num_threads(struct pthreadpool* threadpool,
438438
uint32_t thread_id) {
@@ -476,7 +476,7 @@ static void ensure_num_threads(struct pthreadpool* threadpool,
476476
}
477477
}
478478

479-
static void* thread_main(void* arg) {
479+
static pthreadpool_thread_return_t thread_main(void* arg) {
480480
// Unpack the argument, i.e. extract the pointer to the `pthreadpool` from the
481481
// provided pointer to this thread's `thread_info`.
482482
struct thread_info* thread = (struct thread_info*)arg;
@@ -558,7 +558,7 @@ static void* thread_main(void* arg) {
558558
// Release our hold on the threadpool.
559559
pthreadpool_release(threadpool);
560560

561-
return NULL;
561+
return 0;
562562
}
563563

564564
static size_t get_num_cpus() {

src/threadpool-atomics.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
typedef pthread_t pthreadpool_thread_t;
5757
typedef pthread_mutex_t pthreadpool_mutex_t;
5858
typedef pthread_cond_t pthreadpool_cond_t;
59+
typedef void* pthreadpool_thread_return_t;
5960
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
6061
pthread_mutex_init(mutex, NULL);
6162
}
@@ -84,18 +85,20 @@ static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
8485
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
8586
pthread_cond_broadcast(cond);
8687
}
87-
static inline void pthreadpool_thread_create(pthreadpool_thread_t* thread,
88-
void*(fun)(void*), void* arg) {
88+
static inline void pthreadpool_thread_create(
89+
pthreadpool_thread_t* thread, pthreadpool_thread_return_t(fun)(void*),
90+
void* arg) {
8991
pthread_create(thread, NULL, fun, arg);
9092
}
91-
static inline void pthreadpool_thread_join(pthreadpool_thread_t thread,
92-
void* return_value) {
93-
pthread_join(thread, (void**)return_value);
93+
static inline void pthreadpool_thread_join(
94+
pthreadpool_thread_t thread, pthreadpool_thread_return_t* return_value) {
95+
pthread_join(thread, return_value);
9496
}
9597
#else
9698
typedef thrd_t pthreadpool_thread_t;
9799
typedef mtx_t pthreadpool_mutex_t;
98100
typedef cnd_t pthreadpool_cond_t;
101+
typedef int pthreadpool_thread_return_t;
99102
static inline void pthreadpool_mutex_init(pthreadpool_mutex_t* mutex) {
100103
mtx_init(mutex, mtx_plain);
101104
}
@@ -124,12 +127,13 @@ static inline void pthreadpool_cond_signal(pthreadpool_cond_t* cond) {
124127
static inline void pthreadpool_cond_broadcast(pthreadpool_cond_t* cond) {
125128
cnd_broadcast(cond);
126129
}
127-
static inline void pthreadpool_thread_create(pthreadpool_thread_t* thread,
128-
void*(fun)(void*), void* arg) {
130+
static inline void pthreadpool_thread_create(
131+
pthreadpool_thread_t* thread, pthreadpool_thread_return_t(fun)(void*),
132+
void* arg) {
129133
thrd_create(thread, fun, arg);
130134
}
131-
static inline void pthreadpool_thread_join(pthreadpool_thread_t thread,
132-
void* return_value) {
135+
static inline void pthreadpool_thread_join(
136+
pthreadpool_thread_t thread, pthreadpool_thread_return_t* return_value) {
133137
thrd_join(thread, return_value);
134138
}
135139
#endif // PTHREADPOOL_USE_PTHREADS

0 commit comments

Comments
 (0)