Use a uniform strategy when a function is not working: do not expose

it.

Rather than failing at runtime, it is better to fail at compile or
link time.

svn:r1363
This commit is contained in:
Nick Mathewson 2009-07-17 21:47:45 +00:00
parent 9cf4ee7e17
commit 1fb2e818a6
3 changed files with 20 additions and 21 deletions

View File

@ -420,7 +420,7 @@ ACX_PTHREAD([
AC_DEFINE(HAVE_PTHREADS, 1, AC_DEFINE(HAVE_PTHREADS, 1,
[Define if we have pthreads on this system]) [Define if we have pthreads on this system])
have_pthreads=yes]) have_pthreads=yes])
AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no"]) AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"])
# check if we should compile locking into the library # check if we should compile locking into the library
if test x$enable_thread_support = xno; then if test x$enable_thread_support = xno; then

18
event.c
View File

@ -1678,6 +1678,7 @@ event_set_mem_functions(void *(*malloc_fn)(size_t sz),
} }
#endif #endif
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
/* support for threading */ /* support for threading */
void (*_evthread_locking_fn)(int mode, void *lock) = NULL; void (*_evthread_locking_fn)(int mode, void *lock) = NULL;
unsigned long (*_evthread_id_fn)(void) = NULL; unsigned long (*_evthread_id_fn)(void) = NULL;
@ -1687,12 +1688,9 @@ void (*_evthread_lock_free_fn)(void *) = NULL;
void void
evthread_set_locking_callback(void (*locking_fn)(int mode, void *lock)) evthread_set_locking_callback(void (*locking_fn)(int mode, void *lock))
{ {
#ifdef _EVENT_DISABLE_THREAD_SUPPORT
event_errx(1, "%s: not compiled with thread support", __func__);
#else
_evthread_locking_fn = locking_fn; _evthread_locking_fn = locking_fn;
#endif
} }
#endif
#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H) #if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
static void static void
@ -1717,15 +1715,13 @@ evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
#endif #endif
} }
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
void void
evthread_set_id_callback(unsigned long (*id_fn)(void)) evthread_set_id_callback(unsigned long (*id_fn)(void))
{ {
#ifdef _EVENT_DISABLE_THREAD_SUPPORT
event_errx(1, "%s: not compiled with thread support", __func__);
#else
_evthread_id_fn = id_fn; _evthread_id_fn = id_fn;
#endif
} }
#endif
int int
evthread_make_base_notifiable(struct event_base *base) evthread_make_base_notifiable(struct event_base *base)
@ -1789,17 +1785,15 @@ evthread_make_base_notifiable(struct event_base *base)
return event_add(&base->th_notify, NULL); return event_add(&base->th_notify, NULL);
} }
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
void void
evthread_set_lock_create_callbacks(void *(*alloc_fn)(void), evthread_set_lock_create_callbacks(void *(*alloc_fn)(void),
void (*free_fn)(void *)) void (*free_fn)(void *))
{ {
#ifdef _EVENT_DISABLE_THREAD_SUPPORT
event_errx(1, "%s: not compiled with thread support", __func__);
#else
_evthread_lock_alloc_fn = alloc_fn; _evthread_lock_alloc_fn = alloc_fn;
_evthread_lock_free_fn = free_fn; _evthread_lock_free_fn = free_fn;
#endif
} }
#endif
void void
event_base_dump_events(struct event_base *base, FILE *output) event_base_dump_events(struct event_base *base, FILE *output)

View File

@ -65,6 +65,8 @@ extern "C" {
#define EVTHREAD_WRITE 0x04 #define EVTHREAD_WRITE 0x04
#define EVTHREAD_READ 0x08 #define EVTHREAD_READ 0x08
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
/** /**
Sets the functions Libevent should use for allocating and freeing Sets the functions Libevent should use for allocating and freeing
locks. This needs to be called in addition to locks. This needs to be called in addition to
@ -100,13 +102,7 @@ void evthread_set_locking_callback(
void evthread_set_id_callback( void evthread_set_id_callback(
unsigned long (*id_fn)(void)); unsigned long (*id_fn)(void));
/** Make sure it's safe to tell an event base to wake up from another thread. #if defined(WIN32) && !defined(_EVENT_DISABLE_THREAD_SUPPORT)
@return 0 on success, -1 on failure.
*/
int evthread_make_base_notifiable(struct event_base *base);
#ifdef WIN32
/** Sets up Libevent for use with Windows builtin locking and thread ID /** Sets up Libevent for use with Windows builtin locking and thread ID
functions. Unavailable if Libevent is not built for Windows. functions. Unavailable if Libevent is not built for Windows.
@ -115,7 +111,7 @@ int evthread_use_windows_threads(void);
#define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1 #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1
#endif #endif
#ifdef _EVENT_HAVE_PTHREADS #if defined(_EVENT_HAVE_PTHREADS)
/** Sets up Libevent for use with Pthreads locking and thread ID functions. /** Sets up Libevent for use with Pthreads locking and thread ID functions.
Unavailable if Libevent is not build for use with pthreads. Requires Unavailable if Libevent is not build for use with pthreads. Requires
libraries to link against Libevent_pthreads as well as Libevent. libraries to link against Libevent_pthreads as well as Libevent.
@ -125,6 +121,15 @@ int evthread_use_pthreads(void);
#define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1 #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
#endif #endif
#endif /* _EVENT_DISABLE_THREAD_SUPPORT */
/** Make sure it's safe to tell an event base to wake up from another thread.
or a signal handler.
@return 0 on success, -1 on failure.
*/
int evthread_make_base_notifiable(struct event_base *base);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif