added thread initialization/deinitialization hooks
moved empty_enumerator to a public implementation
This commit is contained in:
@@ -88,6 +88,26 @@ struct private_dispatcher_t {
|
||||
* user param to context constructor
|
||||
*/
|
||||
void *param;
|
||||
|
||||
/**
|
||||
* thread specific initialization handler
|
||||
*/
|
||||
void (*init)(void *param);
|
||||
|
||||
/**
|
||||
* argument to pass to thread intiializer
|
||||
*/
|
||||
void *init_param;
|
||||
|
||||
/**
|
||||
* thread specific deinitialization handler
|
||||
*/
|
||||
void (*deinit)(void *param);
|
||||
|
||||
/**
|
||||
* param tho thread specific deinitialization handler
|
||||
*/
|
||||
void *deinit_param;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
@@ -172,13 +192,12 @@ static void add_controller(private_dispatcher_t *this,
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch
|
||||
* Actual dispatching code
|
||||
*/
|
||||
static void dispatch(private_dispatcher_t *this)
|
||||
{
|
||||
FCGX_Request fcgi_req;
|
||||
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
if (FCGX_InitRequest(&fcgi_req, this->fd, 0) == 0)
|
||||
{
|
||||
while (TRUE)
|
||||
@@ -273,17 +292,45 @@ static void dispatch(private_dispatcher_t *this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup thread and start dispatching
|
||||
*/
|
||||
static void start_dispatching(private_dispatcher_t *this)
|
||||
{
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
||||
if (this->init)
|
||||
{
|
||||
this->init(this->init_param);
|
||||
}
|
||||
if (this->deinit)
|
||||
{
|
||||
pthread_cleanup_push(this->deinit, this->deinit_param);
|
||||
dispatch(this);
|
||||
pthread_cleanup_pop(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatch(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of dispatcher_t.run.
|
||||
*/
|
||||
static void run(private_dispatcher_t *this, int threads)
|
||||
static void run(private_dispatcher_t *this, int threads,
|
||||
void(*init)(void *param), void *init_param,
|
||||
void(*deinit)(void *param), void *deinit_param)
|
||||
{
|
||||
this->init = init;
|
||||
this->init_param = init_param;
|
||||
this->deinit = deinit;
|
||||
this->deinit_param = deinit_param;
|
||||
this->thread_count = threads;
|
||||
this->threads = malloc(sizeof(pthread_t) * threads);
|
||||
while (threads)
|
||||
{
|
||||
if (pthread_create(&this->threads[threads - 1],
|
||||
NULL, (void*)dispatch, this) == 0)
|
||||
NULL, (void*)start_dispatching, this) == 0)
|
||||
{
|
||||
threads--;
|
||||
}
|
||||
@@ -331,7 +378,7 @@ dispatcher_t *dispatcher_create(char *socket, int timeout,
|
||||
private_dispatcher_t *this = malloc_thing(private_dispatcher_t);
|
||||
|
||||
this->public.add_controller = (void(*)(dispatcher_t*, controller_constructor_t, void*))add_controller;
|
||||
this->public.run = (void(*)(dispatcher_t*, int threads))run;
|
||||
this->public.run = (void(*)(dispatcher_t*, int threads,void(*)(void *),void *,void(*)(void *),void *))run;
|
||||
this->public.waitsignal = (void(*)(dispatcher_t*))waitsignal;
|
||||
this->public.destroy = (void(*)(dispatcher_t*))destroy;
|
||||
|
||||
|
||||
@@ -52,9 +52,20 @@ struct dispatcher_t {
|
||||
/**
|
||||
* @brief Start with dispatching.
|
||||
*
|
||||
* It may be necessary to call per-thread initialization functions.
|
||||
* If init is not NULL, the handler is called right after thread
|
||||
* creation (by the created thread) and the deinit function is called
|
||||
* before the thread gets destroyed (again by the thread itself).
|
||||
*
|
||||
* @param thread number of dispatching threads
|
||||
* @param init thread specific initialization function, or NULL
|
||||
* @param init_param param to pass to init function
|
||||
* @param deinit thread dpecific deinitialization function, or NULL
|
||||
* @param deinit_param param to pass to deinit function
|
||||
*/
|
||||
void (*run)(dispatcher_t *this, int threads);
|
||||
void (*run)(dispatcher_t *this, int threads,
|
||||
void(*init)(void *param), void *init_param,
|
||||
void(*deinit)(void *param), void *deinit_param);
|
||||
|
||||
/**
|
||||
* @brief Wait for a relevant signal action.
|
||||
|
||||
@@ -35,11 +35,13 @@ struct enumerator_t {
|
||||
/**
|
||||
* @brief Enumerate collection.
|
||||
*
|
||||
* @param item first enumerated item
|
||||
* @param ... additional items enumerated, depending in implementation
|
||||
* @return TRUE if pointers returned
|
||||
* The enumerate function takes a variable argument list containing
|
||||
* pointers where the enumerated values get written.
|
||||
*
|
||||
* @param ... variable list of enumerated items, implementation dependant
|
||||
* @return TRUE if pointers returned
|
||||
*/
|
||||
bool (*enumerate)(enumerator_t *this, void *item, ...);
|
||||
bool (*enumerate)(enumerator_t *this, ...);
|
||||
|
||||
/**
|
||||
* @brief Destroy a enumerator instance.
|
||||
@@ -47,4 +49,9 @@ struct enumerator_t {
|
||||
void (*destroy)(enumerator_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Create an enumerator which enumerates over nothing
|
||||
*/
|
||||
enumerator_t* enumerator_create_empty();
|
||||
|
||||
#endif /* ENUMERATOR_H_ */
|
||||
|
||||
Reference in New Issue
Block a user