Legofit
infers population history from nucleotide site patterns.
Data Structures | Macros | Functions
jobqueue.c File Reference

Multithreaded job queue. More...

#include "jobqueue.h"
#include "misc.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <time.h>
#include "dprintf.h"

Data Structures

struct  Job
 A single job in the queue. More...
 
struct  JobQueue
 All data used by job queue. More...
 

Macros

#define ERR(code, msg)
 
#define CHECKMEM(x)
 
#define JOBQUEUE_VALID   8131950
 

Functions

void * threadfun (void *arg)
 Waits until there is a job in the queue, pops it off and executes it, then waits for another. More...
 
void Job_free (Job *job)
 Destroy a Job.
 
JobQueueJobQueue_new (int maxThreads, void *threadData, void *(*ThreadState_new)(void *), void(*ThreadState_free)(void *))
 Construct a JobQueue.
 
void JobQueue_addJob (JobQueue *jq, int(*jobfun)(void *, void *), void *param)
 Add a job to the queue. More...
 
void JobQueue_noMoreJobs (JobQueue *jq)
 Stop accepting jobs.
 
void JobQueue_waitOnJobs (JobQueue *jq)
 Wait until all threads are idle.
 
void JobQueue_free (JobQueue *jq)
 Destroy a JobQueue.
 

Detailed Description

Multithreaded job queue.

Author
Alan R. Rogers

This file implements a multithreaded job queue. Jobs are pushed onto a queue by the main program. Each thread (or worker) removes a job from the queue, executes it, and then goes back for another. When all jobs are finished, control returns to the main function.

Macro Definition Documentation

◆ CHECKMEM

#define CHECKMEM (   x)
Value:
do { \
if(!(x)) { \
fprintf(stderr, "%s:%s:%d: allocation error\n", \
__FILE__,__func__,__LINE__); \
exit(EXIT_FAILURE); \
} \
} while(0);

◆ ERR

#define ERR (   code,
  msg 
)
Value:
do{ \
char err_buff[50]; \
strerror_r((code), err_buff, sizeof(err_buff)); \
fprintf(stderr,"%s:%s:%d: %s: %d (%s)\n", \
__FILE__,__func__,__LINE__, \
(msg), (code), err_buff); \
exit(1); \
}while(0)

Function Documentation

◆ JobQueue_addJob()

void JobQueue_addJob ( JobQueue jq,
int(*)(void *, void *)  jobfun,
void *  param 
)

Add a job to the queue.

Parameters
jqthe JobQueue
jobfunfunction to be executed. Takes two void* arguments. The first points to a structure containing data associated with this job. The second points to data associated with the thread that runs the job. For example, the 2nd argument can be used to maintain a separate random number generator within each thread.
parampointer to structure with parameters for this job

◆ threadfun()

void * threadfun ( void *  arg)

Waits until there is a job in the queue, pops it off and executes it, then waits for another.

Runs until jobs are completed and main thread sets acceptingJobs=0.