1*2b15cb3dSCy Schubert /* 2*2b15cb3dSCy Schubert * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson 3*2b15cb3dSCy Schubert * 4*2b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without 5*2b15cb3dSCy Schubert * modification, are permitted provided that the following conditions 6*2b15cb3dSCy Schubert * are met: 7*2b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright 8*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer. 9*2b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 10*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the 11*2b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution. 12*2b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products 13*2b15cb3dSCy Schubert * derived from this software without specific prior written permission. 14*2b15cb3dSCy Schubert * 15*2b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16*2b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*2b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*2b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19*2b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*2b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*2b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*2b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*2b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*2b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*2b15cb3dSCy Schubert */ 26*2b15cb3dSCy Schubert #ifndef EVENT2_THREAD_H_INCLUDED_ 27*2b15cb3dSCy Schubert #define EVENT2_THREAD_H_INCLUDED_ 28*2b15cb3dSCy Schubert 29*2b15cb3dSCy Schubert /** @file event2/thread.h 30*2b15cb3dSCy Schubert 31*2b15cb3dSCy Schubert Functions for multi-threaded applications using Libevent. 32*2b15cb3dSCy Schubert 33*2b15cb3dSCy Schubert When using a multi-threaded application in which multiple threads 34*2b15cb3dSCy Schubert add and delete events from a single event base, Libevent needs to 35*2b15cb3dSCy Schubert lock its data structures. 36*2b15cb3dSCy Schubert 37*2b15cb3dSCy Schubert Like the memory-management function hooks, all of the threading functions 38*2b15cb3dSCy Schubert _must_ be set up before an event_base is created if you want the base to 39*2b15cb3dSCy Schubert use them. 40*2b15cb3dSCy Schubert 41*2b15cb3dSCy Schubert Most programs will either be using Windows threads or Posix threads. You 42*2b15cb3dSCy Schubert can configure Libevent to use one of these event_use_windows_threads() or 43*2b15cb3dSCy Schubert event_use_pthreads() respectively. If you're using another threading 44*2b15cb3dSCy Schubert library, you'll need to configure threading functions manually using 45*2b15cb3dSCy Schubert evthread_set_lock_callbacks() and evthread_set_condition_callbacks(). 46*2b15cb3dSCy Schubert 47*2b15cb3dSCy Schubert */ 48*2b15cb3dSCy Schubert 49*2b15cb3dSCy Schubert #include <event2/visibility.h> 50*2b15cb3dSCy Schubert 51*2b15cb3dSCy Schubert #ifdef __cplusplus 52*2b15cb3dSCy Schubert extern "C" { 53*2b15cb3dSCy Schubert #endif 54*2b15cb3dSCy Schubert 55*2b15cb3dSCy Schubert #include <event2/event-config.h> 56*2b15cb3dSCy Schubert 57*2b15cb3dSCy Schubert /** 58*2b15cb3dSCy Schubert @name Flags passed to lock functions 59*2b15cb3dSCy Schubert 60*2b15cb3dSCy Schubert @{ 61*2b15cb3dSCy Schubert */ 62*2b15cb3dSCy Schubert /** A flag passed to a locking callback when the lock was allocated as a 63*2b15cb3dSCy Schubert * read-write lock, and we want to acquire or release the lock for writing. */ 64*2b15cb3dSCy Schubert #define EVTHREAD_WRITE 0x04 65*2b15cb3dSCy Schubert /** A flag passed to a locking callback when the lock was allocated as a 66*2b15cb3dSCy Schubert * read-write lock, and we want to acquire or release the lock for reading. */ 67*2b15cb3dSCy Schubert #define EVTHREAD_READ 0x08 68*2b15cb3dSCy Schubert /** A flag passed to a locking callback when we don't want to block waiting 69*2b15cb3dSCy Schubert * for the lock; if we can't get the lock immediately, we will instead 70*2b15cb3dSCy Schubert * return nonzero from the locking callback. */ 71*2b15cb3dSCy Schubert #define EVTHREAD_TRY 0x10 72*2b15cb3dSCy Schubert /**@}*/ 73*2b15cb3dSCy Schubert 74*2b15cb3dSCy Schubert #if !defined(EVENT__DISABLE_THREAD_SUPPORT) || defined(EVENT_IN_DOXYGEN_) 75*2b15cb3dSCy Schubert 76*2b15cb3dSCy Schubert #define EVTHREAD_LOCK_API_VERSION 1 77*2b15cb3dSCy Schubert 78*2b15cb3dSCy Schubert /** 79*2b15cb3dSCy Schubert @name Types of locks 80*2b15cb3dSCy Schubert 81*2b15cb3dSCy Schubert @{*/ 82*2b15cb3dSCy Schubert /** A recursive lock is one that can be acquired multiple times at once by the 83*2b15cb3dSCy Schubert * same thread. No other process can allocate the lock until the thread that 84*2b15cb3dSCy Schubert * has been holding it has unlocked it as many times as it locked it. */ 85*2b15cb3dSCy Schubert #define EVTHREAD_LOCKTYPE_RECURSIVE 1 86*2b15cb3dSCy Schubert /* A read-write lock is one that allows multiple simultaneous readers, but 87*2b15cb3dSCy Schubert * where any one writer excludes all other writers and readers. */ 88*2b15cb3dSCy Schubert #define EVTHREAD_LOCKTYPE_READWRITE 2 89*2b15cb3dSCy Schubert /**@}*/ 90*2b15cb3dSCy Schubert 91*2b15cb3dSCy Schubert /** This structure describes the interface a threading library uses for 92*2b15cb3dSCy Schubert * locking. It's used to tell evthread_set_lock_callbacks() how to use 93*2b15cb3dSCy Schubert * locking on this platform. 94*2b15cb3dSCy Schubert */ 95*2b15cb3dSCy Schubert struct evthread_lock_callbacks { 96*2b15cb3dSCy Schubert /** The current version of the locking API. Set this to 97*2b15cb3dSCy Schubert * EVTHREAD_LOCK_API_VERSION */ 98*2b15cb3dSCy Schubert int lock_api_version; 99*2b15cb3dSCy Schubert /** Which kinds of locks does this version of the locking API 100*2b15cb3dSCy Schubert * support? A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and 101*2b15cb3dSCy Schubert * EVTHREAD_LOCKTYPE_READWRITE. 102*2b15cb3dSCy Schubert * 103*2b15cb3dSCy Schubert * (Note that RECURSIVE locks are currently mandatory, and 104*2b15cb3dSCy Schubert * READWRITE locks are not currently used.) 105*2b15cb3dSCy Schubert **/ 106*2b15cb3dSCy Schubert unsigned supported_locktypes; 107*2b15cb3dSCy Schubert /** Function to allocate and initialize new lock of type 'locktype'. 108*2b15cb3dSCy Schubert * Returns NULL on failure. */ 109*2b15cb3dSCy Schubert void *(*alloc)(unsigned locktype); 110*2b15cb3dSCy Schubert /** Funtion to release all storage held in 'lock', which was created 111*2b15cb3dSCy Schubert * with type 'locktype'. */ 112*2b15cb3dSCy Schubert void (*free)(void *lock, unsigned locktype); 113*2b15cb3dSCy Schubert /** Acquire an already-allocated lock at 'lock' with mode 'mode'. 114*2b15cb3dSCy Schubert * Returns 0 on success, and nonzero on failure. */ 115*2b15cb3dSCy Schubert int (*lock)(unsigned mode, void *lock); 116*2b15cb3dSCy Schubert /** Release a lock at 'lock' using mode 'mode'. Returns 0 on success, 117*2b15cb3dSCy Schubert * and nonzero on failure. */ 118*2b15cb3dSCy Schubert int (*unlock)(unsigned mode, void *lock); 119*2b15cb3dSCy Schubert }; 120*2b15cb3dSCy Schubert 121*2b15cb3dSCy Schubert /** Sets a group of functions that Libevent should use for locking. 122*2b15cb3dSCy Schubert * For full information on the required callback API, see the 123*2b15cb3dSCy Schubert * documentation for the individual members of evthread_lock_callbacks. 124*2b15cb3dSCy Schubert * 125*2b15cb3dSCy Schubert * Note that if you're using Windows or the Pthreads threading library, you 126*2b15cb3dSCy Schubert * probably shouldn't call this function; instead, use 127*2b15cb3dSCy Schubert * evthread_use_windows_threads() or evthread_use_posix_threads() if you can. 128*2b15cb3dSCy Schubert */ 129*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 130*2b15cb3dSCy Schubert int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *); 131*2b15cb3dSCy Schubert 132*2b15cb3dSCy Schubert #define EVTHREAD_CONDITION_API_VERSION 1 133*2b15cb3dSCy Schubert 134*2b15cb3dSCy Schubert struct timeval; 135*2b15cb3dSCy Schubert 136*2b15cb3dSCy Schubert /** This structure describes the interface a threading library uses for 137*2b15cb3dSCy Schubert * condition variables. It's used to tell evthread_set_condition_callbacks 138*2b15cb3dSCy Schubert * how to use locking on this platform. 139*2b15cb3dSCy Schubert */ 140*2b15cb3dSCy Schubert struct evthread_condition_callbacks { 141*2b15cb3dSCy Schubert /** The current version of the conditions API. Set this to 142*2b15cb3dSCy Schubert * EVTHREAD_CONDITION_API_VERSION */ 143*2b15cb3dSCy Schubert int condition_api_version; 144*2b15cb3dSCy Schubert /** Function to allocate and initialize a new condition variable. 145*2b15cb3dSCy Schubert * Returns the condition variable on success, and NULL on failure. 146*2b15cb3dSCy Schubert * The 'condtype' argument will be 0 with this API version. 147*2b15cb3dSCy Schubert */ 148*2b15cb3dSCy Schubert void *(*alloc_condition)(unsigned condtype); 149*2b15cb3dSCy Schubert /** Function to free a condition variable. */ 150*2b15cb3dSCy Schubert void (*free_condition)(void *cond); 151*2b15cb3dSCy Schubert /** Function to signal a condition variable. If 'broadcast' is 1, all 152*2b15cb3dSCy Schubert * threads waiting on 'cond' should be woken; otherwise, only on one 153*2b15cb3dSCy Schubert * thread is worken. Should return 0 on success, -1 on failure. 154*2b15cb3dSCy Schubert * This function will only be called while holding the associated 155*2b15cb3dSCy Schubert * lock for the condition. 156*2b15cb3dSCy Schubert */ 157*2b15cb3dSCy Schubert int (*signal_condition)(void *cond, int broadcast); 158*2b15cb3dSCy Schubert /** Function to wait for a condition variable. The lock 'lock' 159*2b15cb3dSCy Schubert * will be held when this function is called; should be released 160*2b15cb3dSCy Schubert * while waiting for the condition to be come signalled, and 161*2b15cb3dSCy Schubert * should be held again when this function returns. 162*2b15cb3dSCy Schubert * If timeout is provided, it is interval of seconds to wait for 163*2b15cb3dSCy Schubert * the event to become signalled; if it is NULL, the function 164*2b15cb3dSCy Schubert * should wait indefinitely. 165*2b15cb3dSCy Schubert * 166*2b15cb3dSCy Schubert * The function should return -1 on error; 0 if the condition 167*2b15cb3dSCy Schubert * was signalled, or 1 on a timeout. */ 168*2b15cb3dSCy Schubert int (*wait_condition)(void *cond, void *lock, 169*2b15cb3dSCy Schubert const struct timeval *timeout); 170*2b15cb3dSCy Schubert }; 171*2b15cb3dSCy Schubert 172*2b15cb3dSCy Schubert /** Sets a group of functions that Libevent should use for condition variables. 173*2b15cb3dSCy Schubert * For full information on the required callback API, see the 174*2b15cb3dSCy Schubert * documentation for the individual members of evthread_condition_callbacks. 175*2b15cb3dSCy Schubert * 176*2b15cb3dSCy Schubert * Note that if you're using Windows or the Pthreads threading library, you 177*2b15cb3dSCy Schubert * probably shouldn't call this function; instead, use 178*2b15cb3dSCy Schubert * evthread_use_windows_threads() or evthread_use_pthreads() if you can. 179*2b15cb3dSCy Schubert */ 180*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 181*2b15cb3dSCy Schubert int evthread_set_condition_callbacks( 182*2b15cb3dSCy Schubert const struct evthread_condition_callbacks *); 183*2b15cb3dSCy Schubert 184*2b15cb3dSCy Schubert /** 185*2b15cb3dSCy Schubert Sets the function for determining the thread id. 186*2b15cb3dSCy Schubert 187*2b15cb3dSCy Schubert @param base the event base for which to set the id function 188*2b15cb3dSCy Schubert @param id_fn the identify function Libevent should invoke to 189*2b15cb3dSCy Schubert determine the identity of a thread. 190*2b15cb3dSCy Schubert */ 191*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 192*2b15cb3dSCy Schubert void evthread_set_id_callback( 193*2b15cb3dSCy Schubert unsigned long (*id_fn)(void)); 194*2b15cb3dSCy Schubert 195*2b15cb3dSCy Schubert #if (defined(_WIN32) && !defined(EVENT__DISABLE_THREAD_SUPPORT)) || defined(EVENT_IN_DOXYGEN_) 196*2b15cb3dSCy Schubert /** Sets up Libevent for use with Windows builtin locking and thread ID 197*2b15cb3dSCy Schubert functions. Unavailable if Libevent is not built for Windows. 198*2b15cb3dSCy Schubert 199*2b15cb3dSCy Schubert @return 0 on success, -1 on failure. */ 200*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 201*2b15cb3dSCy Schubert int evthread_use_windows_threads(void); 202*2b15cb3dSCy Schubert /** 203*2b15cb3dSCy Schubert Defined if Libevent was built with support for evthread_use_windows_threads() 204*2b15cb3dSCy Schubert */ 205*2b15cb3dSCy Schubert #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1 206*2b15cb3dSCy Schubert #endif 207*2b15cb3dSCy Schubert 208*2b15cb3dSCy Schubert #if defined(EVENT__HAVE_PTHREADS) || defined(EVENT_IN_DOXYGEN_) 209*2b15cb3dSCy Schubert /** Sets up Libevent for use with Pthreads locking and thread ID functions. 210*2b15cb3dSCy Schubert Unavailable if Libevent is not build for use with pthreads. Requires 211*2b15cb3dSCy Schubert libraries to link against Libevent_pthreads as well as Libevent. 212*2b15cb3dSCy Schubert 213*2b15cb3dSCy Schubert @return 0 on success, -1 on failure. */ 214*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 215*2b15cb3dSCy Schubert int evthread_use_pthreads(void); 216*2b15cb3dSCy Schubert /** Defined if Libevent was built with support for evthread_use_pthreads() */ 217*2b15cb3dSCy Schubert #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1 218*2b15cb3dSCy Schubert 219*2b15cb3dSCy Schubert #endif 220*2b15cb3dSCy Schubert 221*2b15cb3dSCy Schubert /** Enable debugging wrappers around the current lock callbacks. If Libevent 222*2b15cb3dSCy Schubert * makes one of several common locking errors, exit with an assertion failure. 223*2b15cb3dSCy Schubert * 224*2b15cb3dSCy Schubert * If you're going to call this function, you must do so before any locks are 225*2b15cb3dSCy Schubert * allocated. 226*2b15cb3dSCy Schubert **/ 227*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 228*2b15cb3dSCy Schubert void evthread_enable_lock_debugging(void); 229*2b15cb3dSCy Schubert 230*2b15cb3dSCy Schubert /* Old (misspelled) version: This is deprecated; use 231*2b15cb3dSCy Schubert * evthread_enable_log_debugging instead. */ 232*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 233*2b15cb3dSCy Schubert void evthread_enable_lock_debuging(void); 234*2b15cb3dSCy Schubert 235*2b15cb3dSCy Schubert #endif /* EVENT__DISABLE_THREAD_SUPPORT */ 236*2b15cb3dSCy Schubert 237*2b15cb3dSCy Schubert struct event_base; 238*2b15cb3dSCy Schubert /** Make sure it's safe to tell an event base to wake up from another thread 239*2b15cb3dSCy Schubert or a signal handler. 240*2b15cb3dSCy Schubert 241*2b15cb3dSCy Schubert You shouldn't need to call this by hand; configuring the base with thread 242*2b15cb3dSCy Schubert support should be necessary and sufficient. 243*2b15cb3dSCy Schubert 244*2b15cb3dSCy Schubert @return 0 on success, -1 on failure. 245*2b15cb3dSCy Schubert */ 246*2b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 247*2b15cb3dSCy Schubert int evthread_make_base_notifiable(struct event_base *base); 248*2b15cb3dSCy Schubert 249*2b15cb3dSCy Schubert #ifdef __cplusplus 250*2b15cb3dSCy Schubert } 251*2b15cb3dSCy Schubert #endif 252*2b15cb3dSCy Schubert 253*2b15cb3dSCy Schubert #endif /* EVENT2_THREAD_H_INCLUDED_ */ 254