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