Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
add thread exit callback for cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
jgli committed Nov 26, 2015
1 parent bd70b5e commit 0c7d9c4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(libevhtp)

set(PROJECT_MAJOR_VERSION 1)
set(PROJECT_MINOR_VERSION 2)
set(PROJECT_PATCH_VERSION 10)
set(PROJECT_PATCH_VERSION 11)
set(PROJECT_VERSION ${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_PATCH_VERSION})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPROJECT_VERSION=\"${PROJECT_VERSION}\" -Wall")
Expand Down
22 changes: 16 additions & 6 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3599,20 +3599,30 @@ _evhtp_thread_init(evthr_t * thr, void * arg) {
evhtp_t * htp = (evhtp_t *)arg;

if (htp->thread_init_cb) {
htp->thread_init_cb(htp, thr, htp->thread_init_cbarg);
htp->thread_init_cb(htp, thr, htp->thread_cbarg);
}
}

static void
_evhtp_thread_exit(evthr_t * thr, void * arg) {
evhtp_t * htp = (evhtp_t *)arg;

if (htp->thread_exit_cb) {
htp->thread_exit_cb(htp, thr, htp->thread_cbarg);
}
}

int
evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, int nthreads, void * arg) {
htp->thread_init_cb = init_cb;
htp->thread_init_cbarg = arg;
evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, evhtp_thread_exit_cb exit_cb, int nthreads, void * arg) {
htp->thread_cbarg = arg;
htp->thread_init_cb = init_cb;
htp->thread_exit_cb = exit_cb;

#ifndef EVHTP_DISABLE_SSL
evhtp_ssl_use_threads();
#endif

if (!(htp->thr_pool = evthr_pool_new(nthreads, _evhtp_thread_init, htp))) {
if (!(htp->thr_pool = evthr_pool_new(nthreads, _evhtp_thread_init, _evhtp_thread_exit, htp))) {
return -1;
}

Expand Down Expand Up @@ -4378,4 +4388,4 @@ evhtp_make_request(evhtp_connection_t * c, evhtp_request_t * r,
unsigned int
evhtp_request_status(evhtp_request_t * r) {
return htparser_get_status(r->conn->parser);
}
}
7 changes: 5 additions & 2 deletions evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ typedef enum evhtp_ssl_scache_type evhtp_ssl_scache_type;
typedef enum evhtp_type evhtp_type;

typedef void (* evhtp_thread_init_cb)(evhtp_t * htp, evthr_t * thr, void * arg);
typedef void (* evhtp_thread_exit_cb)(evhtp_t * htp, evthr_t * thr, void * arg);
typedef void (* evhtp_callback_cb)(evhtp_request_t * req, void * arg);
typedef void (* evhtp_hook_err_cb)(evhtp_request_t * req, evhtp_error_flags errtype, void * arg);
typedef void (* evhtp_hook_event_cb)(evhtp_connection_t * conn, short events, void * arg);
Expand Down Expand Up @@ -315,8 +316,9 @@ struct evhtp_s {

#ifndef EVHTP_DISABLE_EVTHR
pthread_mutex_t * lock; /**< parent lock for add/del cbs in threads */
void * thread_cbarg;
evhtp_thread_init_cb thread_init_cb;
void * thread_init_cbarg;
evhtp_thread_init_cb thread_exit_cb;
#endif
evhtp_callbacks_t * callbacks;
evhtp_defaults_t defaults;
Expand Down Expand Up @@ -840,12 +842,13 @@ EVHTP_EXPORT int evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr *,
*
* @param htp
* @param init_cb
* @param exit_cb
* @param nthreads
* @param arg
*
* @return
*/
EVHTP_EXPORT int evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, int nthreads, void * arg);
EVHTP_EXPORT int evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, evhtp_thread_exit_cb exit_cb, int nthreads, void * arg);


/**
Expand Down
16 changes: 11 additions & 5 deletions evthr.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct evthr {
pthread_mutex_t lock;
pthread_t * thr;
evthr_init_cb init_cb;
evthr_init_cb exit_cb;
void * arg;
void * aux;

Expand Down Expand Up @@ -118,15 +119,19 @@ _evthr_loop(void * args) {
#endif

pthread_mutex_lock(&thread->lock);

if (thread->init_cb != NULL) {
thread->init_cb(thread, thread->arg);
}

pthread_mutex_unlock(&thread->lock);

event_base_loop(thread->evbase, 0);

pthread_mutex_lock(&thread->lock);
if (thread->exit_cb != NULL) {
thread->exit_cb(thread, thread->arg);
}
pthread_mutex_unlock(&thread->lock);

if (thread->err == 1) {
fprintf(stderr, "FATAL ERROR!\n");
}
Expand Down Expand Up @@ -181,7 +186,7 @@ evthr_get_aux(evthr_t * thr) {
}

evthr_t *
evthr_new(evthr_init_cb init_cb, void * args) {
evthr_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * args) {
evthr_t * thread;
int fds[2];

Expand All @@ -198,6 +203,7 @@ evthr_new(evthr_init_cb init_cb, void * args) {

thread->thr = malloc(sizeof(pthread_t));
thread->init_cb = init_cb;
thread->exit_cb = exit_cb;
thread->arg = args;
thread->rdr = fds[0];
thread->wdr = fds[1];
Expand Down Expand Up @@ -322,7 +328,7 @@ evthr_pool_defer(evthr_pool_t * pool, evthr_cb cb, void * arg) {
} /* evthr_pool_defer */

evthr_pool_t *
evthr_pool_new(int nthreads, evthr_init_cb init_cb, void * shared) {
evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * shared) {
evthr_pool_t * pool;
int i;

Expand Down Expand Up @@ -356,7 +362,7 @@ evthr_pool_new(int nthreads, evthr_init_cb init_cb, void * shared) {
for (i = 0; i < nthreads; i++) {
evthr_t * thread;

if (!(thread = evthr_new(init_cb, shared))) {
if (!(thread = evthr_new(init_cb, exit_cb, shared))) {
evthr_pool_free(pool);
return NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions evthr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ typedef enum evthr_res evthr_res;

typedef void (* evthr_cb)(evthr_t * thr, void * cmd_arg, void * shared);
typedef void (* evthr_init_cb)(evthr_t * thr, void * shared);
typedef void (* evthr_exit_cb)(evthr_t * thr, void * shared);

EVHTP_EXPORT evthr_t * evthr_new(evthr_init_cb init_cb, void * arg);
EVHTP_EXPORT evthr_t * evthr_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * arg);
EVHTP_EXPORT evbase_t * evthr_get_base(evthr_t * thr);
EVHTP_EXPORT void evthr_set_aux(evthr_t * thr, void * aux);
EVHTP_EXPORT void * evthr_get_aux(evthr_t * thr);
Expand All @@ -42,7 +43,7 @@ EVHTP_EXPORT evthr_res evthr_stop(evthr_t * evthr);
EVHTP_EXPORT evthr_res evthr_defer(evthr_t * evthr, evthr_cb cb, void * arg);
EVHTP_EXPORT void evthr_free(evthr_t * evthr);

EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb init_cb, void * shared);
EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * shared);
EVHTP_EXPORT int evthr_pool_start(evthr_pool_t * pool);
EVHTP_EXPORT evthr_res evthr_pool_stop(evthr_pool_t * pool);
EVHTP_EXPORT evthr_res evthr_pool_defer(evthr_pool_t * pool, evthr_cb cb, void * arg);
Expand Down

0 comments on commit 0c7d9c4

Please sign in to comment.