Skip to content

Commit

Permalink
Rename pthread_barrier to avoid collision with Linux version
Browse files Browse the repository at this point in the history
  • Loading branch information
phaag committed Mar 31, 2024
1 parent 9c9b71a commit d4420f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
18 changes: 9 additions & 9 deletions src/libnffile/barrier.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ uint32_t GetNumWorkers(uint32_t requested) {
} // End of GetNumWorkers

// initialize barrier for numWorkers + 1 controller
pthread_barrier_t *pthread_barrier_init(uint32_t numWorkers) {
pthread_barrier_t *barrier = calloc(1, sizeof(pthread_barrier_t));
pthread_control_barrier_t *pthread_control_barrier_init(uint32_t numWorkers) {
pthread_control_barrier_t *barrier = calloc(1, sizeof(pthread_control_barrier_t));
if (!barrier) return NULL;

if (numWorkers == 0) {
Expand All @@ -98,15 +98,15 @@ pthread_barrier_t *pthread_barrier_init(uint32_t numWorkers) {
} // End of pthread_barrier_init

// destroy mutex/cond variables
void pthread_barrier_destroy(pthread_barrier_t *barrier) {
void pthread_control_barrier_destroy(pthread_control_barrier_t *barrier) {
pthread_cond_destroy(&barrier->workerCond);
pthread_cond_destroy(&barrier->controllerCond);
pthread_mutex_destroy(&barrier->workerMutex);
} // End of pthread_barrier_destroy
} // End of pthread_control_barrier_destroy

// enter the barrier and block execution.
// If all workers are waiting, signal the controller
void pthread_barrier_wait(pthread_barrier_t *barrier) {
void pthread_control_barrier_wait(pthread_control_barrier_t *barrier) {
pthread_mutex_lock(&barrier->workerMutex);
barrier->workersWaiting++;
dbg_printf("Worker wait: %d\n", barrier->workersWaiting);
Expand All @@ -117,11 +117,11 @@ void pthread_barrier_wait(pthread_barrier_t *barrier) {
dbg_printf("Worker dbg_awake\n");
pthread_mutex_unlock(&barrier->workerMutex);

} // End of pthread_barrier_wait
} // End of pthread_control_barrier_wait

// wait for all workers to reach the barrier.
// if all workers wait, controller continues
void pthread_controller_wait(pthread_barrier_t *barrier) {
void pthread_controller_wait(pthread_control_barrier_t *barrier) {
dbg_printf("Controller wait\n");
pthread_mutex_lock(&barrier->workerMutex);
while (barrier->workersWaiting < barrier->numWorkers)
Expand All @@ -134,12 +134,12 @@ void pthread_controller_wait(pthread_barrier_t *barrier) {
} // End of pthread_controller_wait

// release barrier and let all workers continue
void pthread_barrier_release(pthread_barrier_t *barrier) {
void pthread_control_barrier_release(pthread_control_barrier_t *barrier) {
dbg_printf("Controller release\n");
pthread_mutex_lock(&barrier->workerMutex);
barrier->workersWaiting = 0;
pthread_cond_broadcast(&barrier->workerCond);
pthread_mutex_unlock(&barrier->workerMutex);
dbg_printf("Controller release done\n");

} // End of pthread_barrier_release
} // End of pthread_control_barrier_release
12 changes: 6 additions & 6 deletions src/libnffile/barrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ typedef struct {
pthread_cond_t controllerCond;
int workersWaiting;
int numWorkers;
} pthread_barrier_t;
} pthread_control_barrier_t;

/* function prototypes */

uint32_t GetNumWorkers(uint32_t requested);

pthread_barrier_t *pthread_barrier_init(uint32_t numWorkers);
pthread_control_barrier_t *pthread_control_barrier_init(uint32_t numWorkers);

void pthread_barrier_destroy(pthread_barrier_t *barrier);
void pthread_control_barrier_destroy(pthread_control_barrier_t *barrier);

void pthread_barrier_wait(pthread_barrier_t *barrier);
void pthread_control_barrier_wait(pthread_control_barrier_t *barrier);

void pthread_controller_wait(pthread_barrier_t *barrier);
void pthread_controller_wait(pthread_control_barrier_t *barrier);

void pthread_barrier_release(pthread_barrier_t *barrier);
void pthread_control_barrier_release(pthread_control_barrier_t *barrier);

#endif
20 changes: 10 additions & 10 deletions src/nfanon/nfanon.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ typedef struct worker_param_s {
dataBlock_t **dataBlock;

// sync barrier
pthread_barrier_t *barrier;
pthread_control_barrier_t *barrier;
} worker_param_t;

/* Function Prototypes */
static void usage(char *name);

static inline void AnonRecord(recordHeaderV3_t *v3Record);

static void process_data(char *wfile, int verbose, worker_param_t **workerList, int numWorkers, pthread_barrier_t *barrier);
static void process_data(char *wfile, int verbose, worker_param_t **workerList, int numWorkers, pthread_control_barrier_t *barrier);

/* Functions */

Expand Down Expand Up @@ -215,7 +215,7 @@ static inline void AnonRecord(recordHeaderV3_t *v3Record) {

} // End of AnonRecord

static void process_data(char *wfile, int verbose, worker_param_t **workerList, int numWorkers, pthread_barrier_t *barrier) {
static void process_data(char *wfile, int verbose, worker_param_t **workerList, int numWorkers, pthread_control_barrier_t *barrier) {
const char spinner[4] = {'|', '/', '-', '\\'};
char pathBuff[MAXPATHLEN];

Expand Down Expand Up @@ -330,7 +330,7 @@ static void process_data(char *wfile, int verbose, worker_param_t **workerList,

dbg_printf("Next block: %d, Records: %u\n", blk_count, dataBlock->NumRecords);
// release workers from barrier
pthread_barrier_release(barrier);
pthread_control_barrier_release(barrier);

// wait for all workers, work done on this block
pthread_controller_wait(barrier);
Expand All @@ -342,7 +342,7 @@ static void process_data(char *wfile, int verbose, worker_param_t **workerList,

// done! - signal all workers to terminate
dataBlock = NULL;
pthread_barrier_release(barrier);
pthread_control_barrier_release(barrier);

FreeDataBlock(dataBlock);
DisposeFile(nffile_w);
Expand All @@ -359,7 +359,7 @@ __attribute__((noreturn)) static void *worker(void *arg) {
uint32_t numWorkers = worker_param->numWorkers;

// wait in barrier after launch
pthread_barrier_wait(worker_param->barrier);
pthread_control_barrier_wait(worker_param->barrier);

while (*(worker_param->dataBlock)) {
dataBlock_t *dataBlock = *(worker_param->dataBlock);
Expand Down Expand Up @@ -408,14 +408,14 @@ __attribute__((noreturn)) static void *worker(void *arg) {
SKIP:
// Done
// wait in barrier for next data record
pthread_barrier_wait(worker_param->barrier);
pthread_control_barrier_wait(worker_param->barrier);
}

dbg_printf("Worker %d done.\n", worker_param->self);
pthread_exit(NULL);
} // End of worker

static worker_param_t **LauchWorkers(pthread_t *tid, int numWorkers, pthread_barrier_t *barrier) {
static worker_param_t **LauchWorkers(pthread_t *tid, int numWorkers, pthread_control_barrier_t *barrier) {
if (numWorkers > MAXWORKERS) {
LogError("LaunchWorkers: number of worker: %u > max workers: %u", numWorkers, MAXWORKERS);
return NULL;
Expand Down Expand Up @@ -523,7 +523,7 @@ int main(int argc, char **argv) {
// check numWorkers depending on cores online
numWorkers = GetNumWorkers(numWorkers);

pthread_barrier_t *barrier = pthread_barrier_init(numWorkers);
pthread_control_barrier_t *barrier = pthread_control_barrier_init(numWorkers);
if (!barrier) exit(255);

pthread_t tid[MAXWORKERS];
Expand All @@ -539,7 +539,7 @@ int main(int argc, char **argv) {
process_data(wfile, verbose, workerList, numWorkers, barrier);

WaitWorkersDone(tid, numWorkers);
pthread_barrier_destroy(barrier);
pthread_control_barrier_destroy(barrier);

return 0;
}

0 comments on commit d4420f7

Please sign in to comment.