2008-12-10 21:31:36 +00:00
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bdberl: Thread Pool
|
2009-06-23 19:22:11 +00:00
|
|
|
* Copyright (c) 2008-9 The Hive http://www.thehive.com/
|
|
|
|
* Authors: Dave "dizzyd" Smith <dizzyd@dizzyd.com>
|
|
|
|
* Phil Toland <phil.toland@gmail.com>
|
2009-06-23 20:27:37 +00:00
|
|
|
* Jon Meredith <jon@jonmeredith.com>
|
2009-06-23 19:22:11 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2008-12-10 21:31:36 +00:00
|
|
|
*
|
|
|
|
* ------------------------------------------------------------------- */
|
|
|
|
#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);
|
|
|
|
|
2009-02-09 22:01:25 +00:00
|
|
|
void bdberl_tpool_run(TPool* tpool, TPoolJobFunc main_fn, void* arg, TPoolJobFunc cancel_fn,
|
|
|
|
TPoolJob** job_ptr);
|
2008-12-10 21:31:36 +00:00
|
|
|
|
|
|
|
void bdberl_tpool_cancel(TPool* tpool, TPoolJob* job);
|
|
|
|
|
2009-06-10 20:18:26 +00:00
|
|
|
void bdberl_tpool_job_count(TPool* tpool, unsigned int *pending_count_ptr,
|
|
|
|
unsigned int *active_count_ptr);
|
2008-12-10 21:31:36 +00:00
|
|
|
|
|
|
|
#endif
|