1 /* $NetBSD: thread.h,v 1.1.1.3 2017/01/31 21:14:53 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_INCLUDED_ 28 #define EVENT2_THREAD_H_INCLUDED_ 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 #include <event2/visibility.h> 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 #include <event2/event-config.h> 57 58 /** 59 @name Flags passed to lock functions 60 61 @{ 62 */ 63 /** A flag passed to a locking callback when the lock was allocated as a 64 * read-write lock, and we want to acquire or release the lock for writing. */ 65 #define EVTHREAD_WRITE 0x04 66 /** A flag passed to a locking callback when the lock was allocated as a 67 * read-write lock, and we want to acquire or release the lock for reading. */ 68 #define EVTHREAD_READ 0x08 69 /** A flag passed to a locking callback when we don't want to block waiting 70 * for the lock; if we can't get the lock immediately, we will instead 71 * return nonzero from the locking callback. */ 72 #define EVTHREAD_TRY 0x10 73 /**@}*/ 74 75 #if !defined(EVENT__DISABLE_THREAD_SUPPORT) || defined(EVENT_IN_DOXYGEN_) 76 77 #define EVTHREAD_LOCK_API_VERSION 1 78 79 /** 80 @name Types of locks 81 82 @{*/ 83 /** A recursive lock is one that can be acquired multiple times at once by the 84 * same thread. No other process can allocate the lock until the thread that 85 * has been holding it has unlocked it as many times as it locked it. */ 86 #define EVTHREAD_LOCKTYPE_RECURSIVE 1 87 /* A read-write lock is one that allows multiple simultaneous readers, but 88 * where any one writer excludes all other writers and readers. */ 89 #define EVTHREAD_LOCKTYPE_READWRITE 2 90 /**@}*/ 91 92 /** This structure describes the interface a threading library uses for 93 * locking. It's used to tell evthread_set_lock_callbacks() how to use 94 * locking on this platform. 95 */ 96 struct evthread_lock_callbacks { 97 /** The current version of the locking API. Set this to 98 * EVTHREAD_LOCK_API_VERSION */ 99 int lock_api_version; 100 /** Which kinds of locks does this version of the locking API 101 * support? A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and 102 * EVTHREAD_LOCKTYPE_READWRITE. 103 * 104 * (Note that RECURSIVE locks are currently mandatory, and 105 * READWRITE locks are not currently used.) 106 **/ 107 unsigned supported_locktypes; 108 /** Function to allocate and initialize new lock of type 'locktype'. 109 * Returns NULL on failure. */ 110 void *(*alloc)(unsigned locktype); 111 /** Funtion to release all storage held in 'lock', which was created 112 * with type 'locktype'. */ 113 void (*free)(void *lock, unsigned locktype); 114 /** Acquire an already-allocated lock at 'lock' with mode 'mode'. 115 * Returns 0 on success, and nonzero on failure. */ 116 int (*lock)(unsigned mode, void *lock); 117 /** Release a lock at 'lock' using mode 'mode'. Returns 0 on success, 118 * and nonzero on failure. */ 119 int (*unlock)(unsigned mode, void *lock); 120 }; 121 122 /** Sets a group of functions that Libevent should use for locking. 123 * For full information on the required callback API, see the 124 * documentation for the individual members of evthread_lock_callbacks. 125 * 126 * Note that if you're using Windows or the Pthreads threading library, you 127 * probably shouldn't call this function; instead, use 128 * evthread_use_windows_threads() or evthread_use_posix_threads() if you can. 129 */ 130 EVENT2_EXPORT_SYMBOL 131 int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *); 132 133 #define EVTHREAD_CONDITION_API_VERSION 1 134 135 struct timeval; 136 137 /** This structure describes the interface a threading library uses for 138 * condition variables. It's used to tell evthread_set_condition_callbacks 139 * how to use locking on this platform. 140 */ 141 struct evthread_condition_callbacks { 142 /** The current version of the conditions API. Set this to 143 * EVTHREAD_CONDITION_API_VERSION */ 144 int condition_api_version; 145 /** Function to allocate and initialize a new condition variable. 146 * Returns the condition variable on success, and NULL on failure. 147 * The 'condtype' argument will be 0 with this API version. 148 */ 149 void *(*alloc_condition)(unsigned condtype); 150 /** Function to free a condition variable. */ 151 void (*free_condition)(void *cond); 152 /** Function to signal a condition variable. If 'broadcast' is 1, all 153 * threads waiting on 'cond' should be woken; otherwise, only on one 154 * thread is worken. Should return 0 on success, -1 on failure. 155 * This function will only be called while holding the associated 156 * lock for the condition. 157 */ 158 int (*signal_condition)(void *cond, int broadcast); 159 /** Function to wait for a condition variable. The lock 'lock' 160 * will be held when this function is called; should be released 161 * while waiting for the condition to be come signalled, and 162 * should be held again when this function returns. 163 * If timeout is provided, it is interval of seconds to wait for 164 * the event to become signalled; if it is NULL, the function 165 * should wait indefinitely. 166 * 167 * The function should return -1 on error; 0 if the condition 168 * was signalled, or 1 on a timeout. */ 169 int (*wait_condition)(void *cond, void *lock, 170 const struct timeval *timeout); 171 }; 172 173 /** Sets a group of functions that Libevent should use for condition variables. 174 * For full information on the required callback API, see the 175 * documentation for the individual members of evthread_condition_callbacks. 176 * 177 * Note that if you're using Windows or the Pthreads threading library, you 178 * probably shouldn't call this function; instead, use 179 * evthread_use_windows_threads() or evthread_use_pthreads() if you can. 180 */ 181 EVENT2_EXPORT_SYMBOL 182 int evthread_set_condition_callbacks( 183 const struct evthread_condition_callbacks *); 184 185 /** 186 Sets the function for determining the thread id. 187 188 @param base the event base for which to set the id function 189 @param id_fn the identify function Libevent should invoke to 190 determine the identity of a thread. 191 */ 192 EVENT2_EXPORT_SYMBOL 193 void evthread_set_id_callback( 194 unsigned long (*id_fn)(void)); 195 196 #if (defined(_WIN32) && !defined(EVENT__DISABLE_THREAD_SUPPORT)) || defined(EVENT_IN_DOXYGEN_) 197 /** Sets up Libevent for use with Windows builtin locking and thread ID 198 functions. Unavailable if Libevent is not built for Windows. 199 200 @return 0 on success, -1 on failure. */ 201 EVENT2_EXPORT_SYMBOL 202 int evthread_use_windows_threads(void); 203 /** 204 Defined if Libevent was built with support for evthread_use_windows_threads() 205 */ 206 #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1 207 #endif 208 209 #if defined(EVENT__HAVE_PTHREADS) || defined(EVENT_IN_DOXYGEN_) 210 /** Sets up Libevent for use with Pthreads locking and thread ID functions. 211 Unavailable if Libevent is not build for use with pthreads. Requires 212 libraries to link against Libevent_pthreads as well as Libevent. 213 214 @return 0 on success, -1 on failure. */ 215 EVENT2_EXPORT_SYMBOL 216 int evthread_use_pthreads(void); 217 /** Defined if Libevent was built with support for evthread_use_pthreads() */ 218 #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1 219 220 #endif 221 222 /** Enable debugging wrappers around the current lock callbacks. If Libevent 223 * makes one of several common locking errors, exit with an assertion failure. 224 * 225 * If you're going to call this function, you must do so before any locks are 226 * allocated. 227 **/ 228 EVENT2_EXPORT_SYMBOL 229 void evthread_enable_lock_debugging(void); 230 231 /* Old (misspelled) version: This is deprecated; use 232 * evthread_enable_log_debugging instead. */ 233 EVENT2_EXPORT_SYMBOL 234 void evthread_enable_lock_debuging(void); 235 236 #endif /* EVENT__DISABLE_THREAD_SUPPORT */ 237 238 struct event_base; 239 /** Make sure it's safe to tell an event base to wake up from another thread 240 or a signal handler. 241 242 You shouldn't need to call this by hand; configuring the base with thread 243 support should be necessary and sufficient. 244 245 @return 0 on success, -1 on failure. 246 */ 247 EVENT2_EXPORT_SYMBOL 248 int evthread_make_base_notifiable(struct event_base *base); 249 250 #ifdef __cplusplus 251 } 252 #endif 253 254 #endif /* EVENT2_THREAD_H_INCLUDED_ */ 255