Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:12

0001 /*
0002  * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  * 3. The name of the author may not be used to endorse or promote products
0013  *    derived from this software without specific prior written permission.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 #ifndef EVENT2_THREAD_H_INCLUDED_
0027 #define EVENT2_THREAD_H_INCLUDED_
0028 
0029 /** @file event2/thread.h
0030 
0031   Functions for multi-threaded applications using Libevent.
0032 
0033   When using a multi-threaded application in which multiple threads
0034   add and delete events from a single event base, Libevent needs to
0035   lock its data structures.
0036 
0037   Like the memory-management function hooks, all of the threading functions
0038   _must_ be set up before an event_base is created if you want the base to
0039   use them.
0040 
0041   Most programs will either be using Windows threads or Posix threads.  You
0042   can configure Libevent to use one of these event_use_windows_threads() or
0043   event_use_pthreads() respectively.  If you're using another threading
0044   library, you'll need to configure threading functions manually using
0045   evthread_set_lock_callbacks() and evthread_set_condition_callbacks().
0046 
0047  */
0048 
0049 #include <event2/visibility.h>
0050 
0051 #ifdef __cplusplus
0052 extern "C" {
0053 #endif
0054 
0055 #include <event2/event-config.h>
0056 
0057 /**
0058    @name Flags passed to lock functions
0059 
0060    @{
0061 */
0062 /** A flag passed to a locking callback when the lock was allocated as a
0063  * read-write lock, and we want to acquire or release the lock for writing. */
0064 #define EVTHREAD_WRITE  0x04
0065 /** A flag passed to a locking callback when the lock was allocated as a
0066  * read-write lock, and we want to acquire or release the lock for reading. */
0067 #define EVTHREAD_READ   0x08
0068 /** A flag passed to a locking callback when we don't want to block waiting
0069  * for the lock; if we can't get the lock immediately, we will instead
0070  * return nonzero from the locking callback. */
0071 #define EVTHREAD_TRY    0x10
0072 /**@}*/
0073 
0074 #if !defined(EVENT__DISABLE_THREAD_SUPPORT) || defined(EVENT_IN_DOXYGEN_)
0075 
0076 #define EVTHREAD_LOCK_API_VERSION 1
0077 
0078 /**
0079    @name Types of locks
0080 
0081    @{*/
0082 /** A recursive lock is one that can be acquired multiple times at once by the
0083  * same thread.  No other process can allocate the lock until the thread that
0084  * has been holding it has unlocked it as many times as it locked it. */
0085 #define EVTHREAD_LOCKTYPE_RECURSIVE 1
0086 /* A read-write lock is one that allows multiple simultaneous readers, but
0087  * where any one writer excludes all other writers and readers. */
0088 #define EVTHREAD_LOCKTYPE_READWRITE 2
0089 /**@}*/
0090 
0091 /** This structure describes the interface a threading library uses for
0092  * locking.   It's used to tell evthread_set_lock_callbacks() how to use
0093  * locking on this platform.
0094  */
0095 struct evthread_lock_callbacks {
0096     /** The current version of the locking API.  Set this to
0097      * EVTHREAD_LOCK_API_VERSION */
0098     int lock_api_version;
0099     /** Which kinds of locks does this version of the locking API
0100      * support?  A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and
0101      * EVTHREAD_LOCKTYPE_READWRITE.
0102      *
0103      * (Note that RECURSIVE locks are currently mandatory, and
0104      * READWRITE locks are not currently used.)
0105      **/
0106     unsigned supported_locktypes;
0107     /** Function to allocate and initialize new lock of type 'locktype'.
0108      * Returns NULL on failure. */
0109     void *(*alloc)(unsigned locktype);
0110     /** Funtion to release all storage held in 'lock', which was created
0111      * with type 'locktype'. */
0112     void (*free)(void *lock, unsigned locktype);
0113     /** Acquire an already-allocated lock at 'lock' with mode 'mode'.
0114      * Returns 0 on success, and nonzero on failure. */
0115     int (*lock)(unsigned mode, void *lock);
0116     /** Release a lock at 'lock' using mode 'mode'.  Returns 0 on success,
0117      * and nonzero on failure. */
0118     int (*unlock)(unsigned mode, void *lock);
0119 };
0120 
0121 /** Sets a group of functions that Libevent should use for locking.
0122  * For full information on the required callback API, see the
0123  * documentation for the individual members of evthread_lock_callbacks.
0124  *
0125  * Note that if you're using Windows or the Pthreads threading library, you
0126  * probably shouldn't call this function; instead, use
0127  * evthread_use_windows_threads() or evthread_use_posix_threads() if you can.
0128  */
0129 EVENT2_EXPORT_SYMBOL
0130 int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
0131 
0132 #define EVTHREAD_CONDITION_API_VERSION 1
0133 
0134 struct timeval;
0135 
0136 /** This structure describes the interface a threading library uses for
0137  * condition variables.  It's used to tell evthread_set_condition_callbacks
0138  * how to use locking on this platform.
0139  */
0140 struct evthread_condition_callbacks {
0141     /** The current version of the conditions API.  Set this to
0142      * EVTHREAD_CONDITION_API_VERSION */
0143     int condition_api_version;
0144     /** Function to allocate and initialize a new condition variable.
0145      * Returns the condition variable on success, and NULL on failure.
0146      * The 'condtype' argument will be 0 with this API version.
0147      */
0148     void *(*alloc_condition)(unsigned condtype);
0149     /** Function to free a condition variable. */
0150     void (*free_condition)(void *cond);
0151     /** Function to signal a condition variable.  If 'broadcast' is 1, all
0152      * threads waiting on 'cond' should be woken; otherwise, only on one
0153      * thread is worken.  Should return 0 on success, -1 on failure.
0154      * This function will only be called while holding the associated
0155      * lock for the condition.
0156      */
0157     int (*signal_condition)(void *cond, int broadcast);
0158     /** Function to wait for a condition variable.  The lock 'lock'
0159      * will be held when this function is called; should be released
0160      * while waiting for the condition to be come signalled, and
0161      * should be held again when this function returns.
0162      * If timeout is provided, it is interval of seconds to wait for
0163      * the event to become signalled; if it is NULL, the function
0164      * should wait indefinitely.
0165      *
0166      * The function should return -1 on error; 0 if the condition
0167      * was signalled, or 1 on a timeout. */
0168     int (*wait_condition)(void *cond, void *lock,
0169         const struct timeval *timeout);
0170 };
0171 
0172 /** Sets a group of functions that Libevent should use for condition variables.
0173  * For full information on the required callback API, see the
0174  * documentation for the individual members of evthread_condition_callbacks.
0175  *
0176  * Note that if you're using Windows or the Pthreads threading library, you
0177  * probably shouldn't call this function; instead, use
0178  * evthread_use_windows_threads() or evthread_use_pthreads() if you can.
0179  */
0180 EVENT2_EXPORT_SYMBOL
0181 int evthread_set_condition_callbacks(
0182     const struct evthread_condition_callbacks *);
0183 
0184 /**
0185    Sets the function for determining the thread id.
0186 
0187    @param base the event base for which to set the id function
0188    @param id_fn the identify function Libevent should invoke to
0189      determine the identity of a thread.
0190 */
0191 EVENT2_EXPORT_SYMBOL
0192 void evthread_set_id_callback(
0193     unsigned long (*id_fn)(void));
0194 
0195 #if (defined(_WIN32) && !defined(EVENT__DISABLE_THREAD_SUPPORT)) || defined(EVENT_IN_DOXYGEN_)
0196 /** Sets up Libevent for use with Windows builtin locking and thread ID
0197     functions.  Unavailable if Libevent is not built for Windows.
0198 
0199     @return 0 on success, -1 on failure. */
0200 EVENT2_EXPORT_SYMBOL
0201 int evthread_use_windows_threads(void);
0202 /**
0203    Defined if Libevent was built with support for evthread_use_windows_threads()
0204 */
0205 #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1
0206 #endif
0207 
0208 #if defined(EVENT__HAVE_PTHREADS) || defined(EVENT_IN_DOXYGEN_)
0209 /** Sets up Libevent for use with Pthreads locking and thread ID functions.
0210     Unavailable if Libevent is not build for use with pthreads.  Requires
0211     libraries to link against Libevent_pthreads as well as Libevent.
0212 
0213     @return 0 on success, -1 on failure. */
0214 EVENT2_EXPORT_SYMBOL
0215 int evthread_use_pthreads(void);
0216 /** Defined if Libevent was built with support for evthread_use_pthreads() */
0217 #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
0218 
0219 #endif
0220 
0221 /** Enable debugging wrappers around the current lock callbacks.  If Libevent
0222  * makes one of several common locking errors, exit with an assertion failure.
0223  *
0224  * If you're going to call this function, you must do so before any locks are
0225  * allocated.
0226  **/
0227 EVENT2_EXPORT_SYMBOL
0228 void evthread_enable_lock_debugging(void);
0229 
0230 /* Old (misspelled) version: This is deprecated; use
0231  * evthread_enable_log_debugging instead. */
0232 EVENT2_EXPORT_SYMBOL
0233 void evthread_enable_lock_debuging(void);
0234 
0235 #endif /* EVENT__DISABLE_THREAD_SUPPORT */
0236 
0237 struct event_base;
0238 /** Make sure it's safe to tell an event base to wake up from another thread
0239     or a signal handler.
0240 
0241     You shouldn't need to call this by hand; configuring the base with thread
0242     support should be necessary and sufficient.
0243 
0244     @return 0 on success, -1 on failure.
0245  */
0246 EVENT2_EXPORT_SYMBOL
0247 int evthread_make_base_notifiable(struct event_base *base);
0248 
0249 #ifdef __cplusplus
0250 }
0251 #endif
0252 
0253 #endif /* EVENT2_THREAD_H_INCLUDED_ */