From 5d2dea82b6af09843c8fb6db696ad18586006a5a Mon Sep 17 00:00:00 2001 From: Christopher Lang Date: Sun, 29 Sep 2024 23:02:15 +0100 Subject: [PATCH] Add NOTHREADS macro for vacuous pthread symbols --- common/pthreads_cross.c | 22 ++++++++++++++++++++++ common/pthreads_cross.h | 22 ++++++++++++++++++++++ common/workerpool.c | 3 +++ 3 files changed, 47 insertions(+) diff --git a/common/pthreads_cross.c b/common/pthreads_cross.c index 04e556cf..a5eaed25 100644 --- a/common/pthreads_cross.c +++ b/common/pthreads_cross.c @@ -248,9 +248,31 @@ unsigned int pcthread_get_num_procs() #else +#ifdef NOTHREADS +int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { return 0; } +int pthread_join(pthread_t thread, void **value_ptr) { return 0; } +int pthread_detach(pthread_t thread) { return 0; } + +int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) { return 0; } +int pthread_mutex_destroy(pthread_mutex_t *mutex) { return 0; } +int pthread_mutex_lock(pthread_mutex_t *mutex) { return 0; } +int pthread_mutex_unlock(pthread_mutex_t *mutex) { return 0; } + +int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr) { return 0; } +int pthread_cond_destroy(pthread_cond_t *cond) { return 0; } +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { return 0; } +int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) { return 0; } +int pthread_cond_signal(pthread_cond_t *cond) { return 0; } +int pthread_cond_broadcast(pthread_cond_t *cond) { return 0; } + +int sched_yield(void) { return 0; } +unsigned int pcthread_get_num_procs() { return 1; } +#else #include unsigned int pcthread_get_num_procs() { return (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); } #endif + +#endif diff --git a/common/pthreads_cross.h b/common/pthreads_cross.h index fc40c9ba..d0b77baf 100644 --- a/common/pthreads_cross.h +++ b/common/pthreads_cross.h @@ -23,6 +23,7 @@ SOFTWARE. #ifndef __CPTHREAD_H__ #define __CPTHREAD_H__ +#ifndef NOTHREADS #ifdef _WIN32 #include #include @@ -30,6 +31,8 @@ SOFTWARE. #include #include #endif +#endif + #include #ifdef _WIN32 @@ -67,6 +70,25 @@ int sched_yield(void); #endif #endif +#ifdef NOTHREADS +int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); +int pthread_join(pthread_t thread, void **value_ptr); +int pthread_detach(pthread_t thread); + +int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr); +int pthread_mutex_destroy(pthread_mutex_t *mutex); +int pthread_mutex_lock(pthread_mutex_t *mutex); +int pthread_mutex_unlock(pthread_mutex_t *mutex); + +int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr); +int pthread_cond_destroy(pthread_cond_t *cond); +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); +int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); +int pthread_cond_signal(pthread_cond_t *cond); +int pthread_cond_broadcast(pthread_cond_t *cond); + +int sched_yield(void); +#endif #ifdef __cplusplus extern "C" { diff --git a/common/workerpool.c b/common/workerpool.c index 6b73541c..c5721d8f 100644 --- a/common/workerpool.c +++ b/common/workerpool.c @@ -96,6 +96,9 @@ void *worker_thread(void *p) workerpool_t *workerpool_create(int nthreads) { assert(nthreads > 0); +#ifdef NOTHREADS + nthreads = 1; +#endif workerpool_t *wp = calloc(1, sizeof(workerpool_t)); wp->nthreads = nthreads;