2008-12-10 21:31:36 +00:00
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bdberl: Thread Pool
|
|
|
|
* Copyright (c) 2008 The Hive. All rights reserved.
|
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------- */
|
|
|
|
#ifndef _BDBERL_TPOOL_DRV
|
|
|
|
#define _BDBERL_TPOOL_DRV
|
|
|
|
|
|
|
|
#include "erl_driver.h"
|
|
|
|
|
|
|
|
typedef void (*TPoolJobFunc)(void* arg);
|
|
|
|
|
|
|
|
typedef struct _TPoolJob
|
|
|
|
{
|
2008-12-11 01:17:44 +00:00
|
|
|
TPoolJobFunc main_fn; /* Function to invoke for this job */
|
2008-12-10 21:31:36 +00:00
|
|
|
|
2008-12-11 01:17:44 +00:00
|
|
|
TPoolJobFunc cancel_fn; /* Function that gets invoked if job is canceled before it can run */
|
2008-12-10 21:31:36 +00:00
|
|
|
|
2008-12-11 01:17:44 +00:00
|
|
|
void* arg; /* Input data for the function */
|
2008-12-10 21:31:36 +00:00
|
|
|
|
|
|
|
unsigned int running; /* Flag indicating if the job is currently running */
|
|
|
|
|
|
|
|
unsigned int canceled; /* Flag indicating if the job was canceled */
|
|
|
|
|
|
|
|
struct _TPoolJob* next; /* Next job in the queue */
|
|
|
|
|
|
|
|
} TPoolJob;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ErlDrvMutex* lock;
|
|
|
|
|
|
|
|
ErlDrvCond* work_cv;
|
|
|
|
|
|
|
|
ErlDrvCond* cancel_cv;
|
|
|
|
|
|
|
|
TPoolJob* pending_jobs;
|
|
|
|
|
|
|
|
TPoolJob* last_pending_job;
|
|
|
|
|
|
|
|
TPoolJob* active_jobs;
|
|
|
|
|
|
|
|
unsigned int pending_job_count;
|
|
|
|
|
|
|
|
unsigned int active_job_count;
|
|
|
|
|
|
|
|
ErlDrvTid* threads;
|
|
|
|
|
|
|
|
unsigned int thread_count;
|
|
|
|
|
|
|
|
unsigned int active_threads;
|
|
|
|
|
|
|
|
unsigned int shutdown;
|
|
|
|
|
|
|
|
} TPool;
|
|
|
|
|
|
|
|
TPool* bdberl_tpool_start(unsigned int thread_count);
|
|
|
|
|
|
|
|
void bdberl_tpool_stop(TPool* tpool);
|
|
|
|
|
2008-12-11 01:17:44 +00:00
|
|
|
TPoolJob* bdberl_tpool_run(TPool* tpool, TPoolJobFunc main_fn, void* arg, TPoolJobFunc cancel_fn);
|
2008-12-10 21:31:36 +00:00
|
|
|
|
|
|
|
void bdberl_tpool_cancel(TPool* tpool, TPoolJob* job);
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|