Skip to content

Commit

Permalink
Add NOTHREADS compile option
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-F5 committed Oct 1, 2024
1 parent 8bd4f76 commit ffd3e02
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ endif()
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_EXAMPLES "Build example executables" ON)
option(ASAN "Use AddressSanitizer for debug builds to detect memory issues" OFF)
option(NOTHREADS "Dissable multithreading" OFF)

if (ASAN)
set(ASAN_FLAGS "\
Expand All @@ -29,6 +30,10 @@ if (ASAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ASAN_FLAGS}")
endif()

if (NOTHREADS)
add_definitions(-D NOTHREADS)
endif()

# Set a default build type if none was specified
set(default_build_type "Release")

Expand Down
6 changes: 6 additions & 0 deletions apriltag.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ apriltag_detector_t *apriltag_detector_create()

td->tag_families = zarray_create(sizeof(apriltag_family_t*));

#ifndef NOTHREADS
pthread_mutex_init(&td->mutex, NULL);
#endif

td->tp = timeprofile_create();

Expand Down Expand Up @@ -967,9 +969,13 @@ static void quad_decode_task(void *_u)
det->p[i][1] = p[1];
}

#ifndef NOTHREADS
pthread_mutex_lock(&td->mutex);
#endif
zarray_add(task->detections, &det);
#ifndef NOTHREADS
pthread_mutex_unlock(&td->mutex);
#endif
}

quad_destroy(quad);
Expand Down
2 changes: 2 additions & 0 deletions apriltag.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ struct apriltag_detector
// Used to manage multi-threading.
workerpool_t *wp;

#ifndef NOTHREADS
// Used for thread safety.
pthread_mutex_t mutex;
#endif
};

// Represents the detection of a tag. These are returned to the user
Expand Down
4 changes: 4 additions & 0 deletions apriltag_quad_thresh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,13 @@ static void do_quad_task(void *p)
memset(&quad, 0, sizeof(struct quad));

if (fit_quad(td, task->im, *cluster, &quad, task->tag_width, task->normal_border, task->reversed_border)) {
#ifndef NOTHREADS
pthread_mutex_lock(&td->mutex);
#endif
zarray_add(quads, &quad);
#ifndef NOTHREADS
pthread_mutex_unlock(&td->mutex);
#endif
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions common/pthreads_cross.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SOFTWARE.
#include "common/pthreads_cross.h"

#ifdef _WIN32
#ifndef NOTHREADS

typedef struct {
SRWLOCK lock;
Expand Down Expand Up @@ -222,6 +223,8 @@ int sched_yield() {
return (int)SwitchToThread();
}

#endif /* NOTHREADS */

void ms_to_timespec(struct timespec *ts, unsigned int ms)
{
if (ts == NULL)
Expand All @@ -246,11 +249,21 @@ unsigned int pcthread_get_num_procs()
return sysinfo.dwNumberOfProcessors;
}

#else
#else /* _WIN32 */

#ifndef NOTHREADS
#include <unistd.h>
unsigned int pcthread_get_num_procs()
unsigned int pcthread_get_num_procs(void)
{
return (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
}
#endif

#endif /* _WIN32 */

#ifdef NOTHREADS
unsigned int pcthread_get_num_procs(void)
{
return 1;
}
#endif
10 changes: 7 additions & 3 deletions common/pthreads_cross.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ 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 /* NOTHREADS */

#include <time.h>

#ifndef NOTHREADS
#ifdef _WIN32

typedef CRITICAL_SECTION pthread_mutex_t;
Expand Down Expand Up @@ -65,13 +69,13 @@ int sched_yield(void);
#ifdef __cplusplus
}
#endif
#endif

#endif /* _WIN32 */
#endif /* NOTHREADS */

#ifdef __cplusplus
extern "C" {
#endif
unsigned int pcthread_get_num_procs();
unsigned int pcthread_get_num_procs(void);

void ms_to_timespec(struct timespec *ts, unsigned int ms);
unsigned int timespec_to_ms(const struct timespec *abstime);
Expand Down
89 changes: 62 additions & 27 deletions common/workerpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ either expressed or implied, of the Regents of The University of Michigan.
#include "workerpool.h"
#include "debug_print.h"

struct task
{
void (*f)(void *p);
void *p;
};

int workerpool_get_nprocs(void)
{
return pcthread_get_num_procs();
}

struct workerpool {
int nthreads;
zarray_t *tasks;
int taskspos;
int end_count; // how many threads are done?

#ifndef NOTHREADS
int nthreads;

pthread_t *threads;
int *status;
Expand All @@ -54,17 +68,23 @@ struct workerpool {
pthread_cond_t startcond; // used to signal the availability of work
bool start_predicate; // predicate that prevents spurious wakeups on startcond
pthread_cond_t endcond; // used to signal completion of all work

int end_count; // how many threads are done?
#endif
};

struct task
void workerpool_run_single(workerpool_t *wp)
{
void (*f)(void *p);
void *p;
};
for (int i = 0; i < zarray_size(wp->tasks); i++) {
struct task *task;
zarray_get_volatile(wp->tasks, i, &task);
task->f(task->p);
}

zarray_clear(wp->tasks);
}

#ifndef NOTHREADS

void *worker_thread(void *p)
static void *worker_thread(void *p)
{
workerpool_t *wp = (workerpool_t*) p;

Expand Down Expand Up @@ -177,17 +197,6 @@ void workerpool_add_task(workerpool_t *wp, void (*f)(void *p), void *p)
}
}

void workerpool_run_single(workerpool_t *wp)
{
for (int i = 0; i < zarray_size(wp->tasks); i++) {
struct task *task;
zarray_get_volatile(wp->tasks, i, &task);
task->f(task->p);
}

zarray_clear(wp->tasks);
}

// runs all added tasks, waits for them to complete.
void workerpool_run(workerpool_t *wp)
{
Expand All @@ -213,13 +222,39 @@ void workerpool_run(workerpool_t *wp)
}
}

int workerpool_get_nprocs()
#else /* NOTHREADS */

workerpool_t *workerpool_create(__attribute__((unused)) int nthreads)
{
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#else
return sysconf (_SC_NPROCESSORS_ONLN);
#endif
workerpool_t *wp = calloc(1, sizeof(workerpool_t));
wp->tasks = zarray_create(sizeof(struct task));
return wp;
}

void workerpool_destroy(workerpool_t *wp)
{
if (wp == NULL)
return;
zarray_destroy(wp->tasks);
free(wp);
}

int workerpool_get_nthreads(__attribute__((unused)) workerpool_t *wp)
{
return 1;
}

void workerpool_add_task(workerpool_t *wp, void (*f)(void *p), void *p)
{
struct task t;
t.f = f;
t.p = p;
zarray_add(wp->tasks, &t);
}

void workerpool_run(workerpool_t *wp)
{
workerpool_run_single(wp);
}

#endif
2 changes: 1 addition & 1 deletion common/workerpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ void workerpool_run_single(workerpool_t *wp);

int workerpool_get_nthreads(workerpool_t *wp);

int workerpool_get_nprocs();
int workerpool_get_nprocs(void);

0 comments on commit ffd3e02

Please sign in to comment.