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