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