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

Commit

Permalink
Updates for threading functionality.
Browse files Browse the repository at this point in the history
[general]

All of the thread-new-type functions will be deprecated in lieu of @jgli's
additions, which allow for entry and exit callbacks. But not just yet.

Reviewing the pull requst for the above, the function declarations had been
changed without a warning to the user. So this commit attempts to keep backwards
compatibility, but will also warn the user of the upcoming changes.

Once we feel as if these minor updates have been ported by the majority of
users, we rename (or deprecate) the 'wexit' functions to their respective
names.

[evhtp-config]
- Added cross-compile macros for marking a function declaration as deprecated.
  It's been cut and pasted to so many projects, hell if I know who wrote it or
  under what license.

[evthr]

- Moved `evthr_[pool_]new` to `static _evthr_[pool_]new`; which takes both the entry and exit
  callbacks.

- The exported function `evthr_[pool_]new` now calls `_evthr_[pool_]new` but with a NULL exit
  callback.

- The exported function `evthr_[pool_]wexit_new` calls `_evthr_[pool_]new with the new exit
  callback.

- These two functions are to be removed once merged with their parents which are
  now marked as deprecated

[evhtp]

- The same logic applies to the local wrapper functions in evhtp.c
    `evhtp_use_threads` is now `static _evhtp_use_threads`, taking both the
    and exit callbacks. (which in turn; uses `evthr_pool_wexit_new`)

    `evhtp_use_threads` now calls `_evhtp_use_threads` with the exit callback
     set to NULL, and vice-versa for 	`evhtp_us_threads_wexit`

- While these changes can go mostly unnoticed, the public function
  `evhtp_use_threads` has been marked as deprecated, and will take
   on the syntax of `evhtp_use_threads_wexit`
  • Loading branch information
Mark Ellzey committed Nov 26, 2015
1 parent c8978b6 commit b634002
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 33 deletions.
14 changes: 14 additions & 0 deletions evhtp-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ extern "C" {
#endif


#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008
# define DEPRECATED(message) __declspec(deprecated(message))
#elif defined(__clang__) && defined(__has_feature)
# if __has_feature(attribute_deprecated_with_message)
# define DEPRECATED(message) __attribute__ ((deprecated(message)))
# endif
# elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
# define DEPRECATED(message) __attribute__ ((deprecated(message)))
# elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
# define DEPRECATED(message) __attribute__((__deprecated__))
# else
# define DEPRECATED(message)
#endif

#undef EVHTP_DISABLE_EVTHR
#undef EVHTP_DISABLE_REGEX
#undef EVHTP_DISABLE_SSL
Expand Down
31 changes: 27 additions & 4 deletions evhtp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
Expand Down Expand Up @@ -3614,8 +3613,16 @@ _evhtp_thread_exit(evthr_t * thr, void * arg) {
}
}

int
evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, evhtp_thread_exit_cb exit_cb, int nthreads, void * arg) {
static int
_evhtp_use_threads(evhtp_t * htp,
evhtp_thread_init_cb init_cb,
evhtp_thread_exit_cb exit_cb,
int nthreads, void * arg) {

if (htp == NULL) {
return -1;
}

htp->thread_cbarg = arg;
htp->thread_init_cb = init_cb;
htp->thread_exit_cb = exit_cb;
Expand All @@ -3624,7 +3631,9 @@ evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, evhtp_thread_exit
evhtp_ssl_use_threads();
#endif

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

Expand All @@ -3633,6 +3642,19 @@ evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb, evhtp_thread_exit
return 0;
}

int
evhtp_use_threads(evhtp_t * htp, evhtp_thread_init_cb init_cb,
int nthreads, void * arg) {
return _evhtp_use_threads(htp, init_cb, NULL, nthreads, arg);
}

int
evhtp_use_threads_wexit(evhtp_t * htp,
evhtp_thread_init_cb init_cb,
evhtp_thread_exit_cb exit_cb,
int nthreads, void * arg) {
return _evhtp_use_threads(htp, init_cb, exit_cb, nthreads, arg);
}
#endif

#ifndef EVHTP_DISABLE_EVTHR
Expand Down Expand Up @@ -4391,3 +4413,4 @@ unsigned int
evhtp_request_status(evhtp_request_t * r) {
return htparser_get_status(r->conn->parser);
}

24 changes: 18 additions & 6 deletions evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,17 @@ struct evhtp_s {
#endif

#ifndef EVHTP_DISABLE_EVTHR
evthr_pool_t * thr_pool; /**< connection threadpool */
#endif
evthr_pool_t * thr_pool; /**< connection threadpool */
pthread_mutex_t * lock; /**< parent lock for add/del cbs in threads */

#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;
evhtp_thread_exit_cb thread_exit_cb;

/* keep backwards compat because I'm dumb and didn't
* make these structs private
*/
#define thread_init_cbarg thread_cbarg
void * thread_cbarg;
#endif
evhtp_callbacks_t * callbacks;
evhtp_defaults_t defaults;
Expand Down Expand Up @@ -848,8 +851,17 @@ EVHTP_EXPORT int evhtp_bind_sockaddr(evhtp_t * htp, struct sockaddr *,
*
* @return
*/
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);
EVHTP_EXPORT int evhtp_use_threads(evhtp_t *, evhtp_thread_init_cb, int nthreads, void *)
DEPRECATED("will take on the syntax of evhtp_use_threads_wexit");

/**
* @brief Temporary function which will be renamed evhtp_use_threads in the
* future. evhtp_use_threads() has been noted as deprecated for now
*/
EVHTP_EXPORT int evhtp_use_threads_wexit(evhtp_t *,
evhtp_thread_init_cb,
evhtp_thread_exit_cb,
int nthreads, void * arg);

/**
* @brief generates all the right information for a reply to be sent to the client
Expand Down
84 changes: 67 additions & 17 deletions evthr.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ _evthr_loop(void * args) {

pthread_mutex_lock(&thread->lock);
if (thread->init_cb != NULL) {
thread->init_cb(thread, thread->arg);
(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);
(thread->exit_cb)(thread, thread->arg);
}
pthread_mutex_unlock(&thread->lock);

Expand Down Expand Up @@ -172,21 +172,45 @@ evthr_stop(evthr_t * thread) {

evbase_t *
evthr_get_base(evthr_t * thr) {
return thr->evbase;
return thr ? thr->evbase : NULL;
}

void
evthr_set_aux(evthr_t * thr, void * aux) {
thr->aux = aux;
if (thr) {
thr->aux = aux;
}
}

void *
evthr_get_aux(evthr_t * thr) {
return thr->aux;
return thr ? thr->aux : NULL;
}

evthr_t *
evthr_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * args) {
int
evthr_set_initcb(evthr_t * thr, evthr_init_cb cb) {
if (thr == NULL) {
return -1;
}

thr->init_cb = cb;

return 01;
}

int
evthr_set_exitcb(evthr_t * thr, evthr_exit_cb cb) {
if (thr == NULL) {
return -1;
}

thr->exit_cb = cb;

return 0;
}

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

Expand All @@ -201,12 +225,13 @@ evthr_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * args) {
return NULL;
}

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];
thread->thr = malloc(sizeof(pthread_t));
thread->arg = args;
thread->rdr = fds[0];
thread->wdr = fds[1];

evthr_set_initcb(thread, init_cb);
evthr_set_exitcb(thread, exit_cb);

if (pthread_mutex_init(&thread->lock, NULL)) {
evthr_free(thread);
Expand All @@ -216,6 +241,16 @@ evthr_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * args) {
return thread;
} /* evthr_new */

evthr_t *
evhtr_new(evthr_init_cb init_cb, void * args) {
return _evthr_new(init_cb, NULL, args);
}

evthr_t *
evthr_wexit_new(evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * args) {
return _evthr_new(init_cb, exit_cb, args);
}

int
evthr_start(evthr_t * thread) {
if (thread == NULL || thread->thr == NULL) {
Expand Down Expand Up @@ -327,8 +362,11 @@ evthr_pool_defer(evthr_pool_t * pool, evthr_cb cb, void * arg) {
#endif
} /* evthr_pool_defer */

evthr_pool_t *
evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * shared) {
static evthr_pool_t *
_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 @@ -362,7 +400,7 @@ evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void
for (i = 0; i < nthreads; i++) {
evthr_t * thread;

if (!(thread = evthr_new(init_cb, exit_cb, shared))) {
if (!(thread = evthr_wexit_new(init_cb, exit_cb, shared))) {
evthr_pool_free(pool);
return NULL;
}
Expand All @@ -375,7 +413,19 @@ evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void
}

return pool;
} /* evthr_pool_new */
} /* _evthr_pool_new */

evthr_pool_t *
evthr_pool_new(int nthreads, evthr_init_cb init_cb, void * shared) {
return _evthr_pool_new(nthreads, init_cb, NULL, shared);
}

evthr_pool_t *
evthr_pool_wexit_new(int nthreads,
evthr_init_cb init_cb,
evthr_exit_cb exit_cb, void * shared) {
return _evthr_pool_new(nthreads, init_cb, exit_cb, shared);
}

int
evthr_pool_start(evthr_pool_t * pool) {
Expand Down
16 changes: 10 additions & 6 deletions evthr.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#ifndef __EVTHR_H__
#define __EVTHR_H__

Expand Down Expand Up @@ -34,21 +31,28 @@ 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, evthr_exit_cb exit_cb, void * arg);
EVHTP_EXPORT evthr_t * evthr_new(evthr_init_cb, void *)
DEPRECATED("will take on the syntax of evthr_wexit_new");

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);
EVHTP_EXPORT int evthr_start(evthr_t * evthr);
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 evthr_res evthr_defer(evthr_t * evthr, evthr_cb cb, void *);
EVHTP_EXPORT void evthr_free(evthr_t * evthr);

EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb init_cb, evthr_exit_cb exit_cb, void * shared);
EVHTP_EXPORT evthr_pool_t * evthr_pool_new(int nthreads, evthr_init_cb, void *)
DEPRECATED("will take on the syntax of evthr_pool_wexit_new");

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);
EVHTP_EXPORT void evthr_pool_free(evthr_pool_t * pool);

EVHTP_EXPORT evthr_t * evthr_wexit_new(evthr_init_cb, evthr_exit_cb, void * shared);
EVHTP_EXPORT evthr_pool_t * evthr_pool_wexit_new(int nthreads, evthr_init_cb, evthr_exit_cb, void *);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit b634002

Please sign in to comment.