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