Skip to content

Commit

Permalink
Add NOTHREADS macro for vacuous pthread symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-F5 committed Oct 2, 2024
1 parent 8bd4f76 commit 5d2dea8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions common/pthreads_cross.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unistd.h>
unsigned int pcthread_get_num_procs()
{
return (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
}
#endif

#endif
22 changes: 22 additions & 0 deletions common/pthreads_cross.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ SOFTWARE.
#ifndef __CPTHREAD_H__
#define __CPTHREAD_H__

#ifndef NOTHREADS
#ifdef _WIN32
#include <stdbool.h>
#include <windows.h>
#else
#include <pthread.h>
#include <sched.h>
#endif
#endif

#include <time.h>

#ifdef _WIN32
Expand Down Expand Up @@ -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" {
Expand Down
3 changes: 3 additions & 0 deletions common/workerpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5d2dea8

Please sign in to comment.