1 | /** @file
|
---|
2 | * IPRT - Critical Sections.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_critsect_h
|
---|
27 | #define ___iprt_critsect_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #ifdef IN_RING3
|
---|
33 | # include <iprt/thread.h>
|
---|
34 | #endif
|
---|
35 | #ifdef RT_LOCK_STRICT_ORDER
|
---|
36 | # include <iprt/lockvalidator.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | RT_C_DECLS_BEGIN
|
---|
40 |
|
---|
41 | /** @defgroup grp_rt_critsect RTCritSect - Critical Sections
|
---|
42 | *
|
---|
43 | * "Critical section" synchronization primitives can be used to
|
---|
44 | * protect a section of code or data to which access must be exclusive;
|
---|
45 | * only one thread can hold access to a critical section at one time.
|
---|
46 | *
|
---|
47 | * A critical section is a fast recursive write lock; if the critical
|
---|
48 | * section is not acquired, then entering it is fast (requires no system
|
---|
49 | * call). IPRT uses the Windows terminology here; on other platform, this
|
---|
50 | * might be called a "futex" or a "fast mutex". As opposed to IPRT
|
---|
51 | * "fast mutexes" (see @ref grp_rt_sems_fast_mutex ), critical sections
|
---|
52 | * are recursive.
|
---|
53 | *
|
---|
54 | * Use RTCritSectInit to initialize a critical section; use RTCritSectEnter
|
---|
55 | * and RTCritSectLeave to acquire and release access.
|
---|
56 | *
|
---|
57 | * For an overview of all types of synchronization primitives provided
|
---|
58 | * by IPRT (event, mutex/fast mutex/read-write mutex semaphores), see
|
---|
59 | * @ref grp_rt_sems .
|
---|
60 | *
|
---|
61 | * @ingroup grp_rt
|
---|
62 | * @{
|
---|
63 | */
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Critical section.
|
---|
67 | */
|
---|
68 | typedef struct RTCRITSECT
|
---|
69 | {
|
---|
70 | /** Magic used to validate the section state.
|
---|
71 | * RTCRITSECT_MAGIC is the value of an initialized & operational section. */
|
---|
72 | volatile uint32_t u32Magic;
|
---|
73 | /** Number of lockers.
|
---|
74 | * -1 if the section is free. */
|
---|
75 | volatile int32_t cLockers;
|
---|
76 | /** The owner thread. */
|
---|
77 | volatile RTNATIVETHREAD NativeThreadOwner;
|
---|
78 | /** Number of nested enter operations performed.
|
---|
79 | * Greater or equal to 1 if owned, 0 when free.
|
---|
80 | */
|
---|
81 | volatile int32_t cNestings;
|
---|
82 | /** Section flags - the RTCRITSECT_FLAGS_* \#defines. */
|
---|
83 | uint32_t fFlags;
|
---|
84 | /** The semaphore to block on. */
|
---|
85 | RTSEMEVENT EventSem;
|
---|
86 | /** Lock validator record. Only used in strict builds. */
|
---|
87 | R3R0PTRTYPE(PRTLOCKVALRECEXCL) pValidatorRec;
|
---|
88 | /** Alignmnet padding. */
|
---|
89 | RTHCPTR Alignment;
|
---|
90 | } RTCRITSECT;
|
---|
91 | AssertCompileSize(RTCRITSECT, HC_ARCH_BITS == 32 ? 32 : 48);
|
---|
92 |
|
---|
93 | /** RTCRITSECT::u32Magic value. (Hiromi Uehara) */
|
---|
94 | #define RTCRITSECT_MAGIC UINT32_C(0x19790326)
|
---|
95 |
|
---|
96 | /** @name RTCritSectInitEx flags / RTCRITSECT::fFlags
|
---|
97 | * @{ */
|
---|
98 | /** If set, nesting(/recursion) is not allowed. */
|
---|
99 | #define RTCRITSECT_FLAGS_NO_NESTING UINT32_C(0x00000001)
|
---|
100 | /** Disables lock validation. */
|
---|
101 | #define RTCRITSECT_FLAGS_NO_LOCK_VAL UINT32_C(0x00000002)
|
---|
102 | /** Bootstrap hack for use with certain memory allocator locks only! */
|
---|
103 | #define RTCRITSECT_FLAGS_BOOTSTRAP_HACK UINT32_C(0x00000004)
|
---|
104 | /** If set, the critical section becomes a dummy that doesn't serialize any
|
---|
105 | * threads. This flag can only be set at creation time.
|
---|
106 | *
|
---|
107 | * The intended use is avoiding lots of conditional code where some component
|
---|
108 | * might or might not require entering a critical section before access. */
|
---|
109 | #define RTCRITSECT_FLAGS_NOP UINT32_C(0x00000008)
|
---|
110 | /** @} */
|
---|
111 |
|
---|
112 | #ifdef IN_RING3
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Initialize a critical section.
|
---|
116 | */
|
---|
117 | RTDECL(int) RTCritSectInit(PRTCRITSECT pCritSect);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Initialize a critical section.
|
---|
121 | *
|
---|
122 | * @returns iprt status code.
|
---|
123 | * @param pCritSect Pointer to the critical section structure.
|
---|
124 | * @param fFlags Flags, any combination of the RTCRITSECT_FLAGS
|
---|
125 | * \#defines.
|
---|
126 | * @param hClass The class (no reference consumed). If NIL, no
|
---|
127 | * lock order validation will be performed on this
|
---|
128 | * lock.
|
---|
129 | * @param uSubClass The sub-class. This is used to define lock
|
---|
130 | * order within a class. RTLOCKVAL_SUB_CLASS_NONE
|
---|
131 | * is the recommended value here.
|
---|
132 | * @param pszNameFmt Name format string for the lock validator,
|
---|
133 | * optional (NULL). Max length is 32 bytes.
|
---|
134 | * @param ... Format string arguments.
|
---|
135 | */
|
---|
136 | RTDECL(int) RTCritSectInitEx(PRTCRITSECT pCritSect, uint32_t fFlags,
|
---|
137 | RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Changes the lock validator sub-class of the critical section.
|
---|
141 | *
|
---|
142 | * It is recommended to try make sure that nobody is using this critical section
|
---|
143 | * while changing the value.
|
---|
144 | *
|
---|
145 | * @returns The old sub-class. RTLOCKVAL_SUB_CLASS_INVALID is returns if the
|
---|
146 | * lock validator isn't compiled in or either of the parameters are
|
---|
147 | * invalid.
|
---|
148 | * @param pCritSect The critical section.
|
---|
149 | * @param uSubClass The new sub-class value.
|
---|
150 | */
|
---|
151 | RTDECL(uint32_t) RTCritSectSetSubClass(PRTCRITSECT pCritSect, uint32_t uSubClass);
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Enter a critical section.
|
---|
155 | *
|
---|
156 | * @returns VINF_SUCCESS on success.
|
---|
157 | * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
158 | * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
159 | * @param pCritSect The critical section.
|
---|
160 | */
|
---|
161 | RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect);
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Enter a critical section.
|
---|
165 | *
|
---|
166 | * @retval VINF_SUCCESS on success.
|
---|
167 | * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
168 | * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
169 | *
|
---|
170 | * @param pCritSect The critical section.
|
---|
171 | * @param uId Where we're entering the section.
|
---|
172 | * @param pszFile The source position - file.
|
---|
173 | * @param iLine The source position - line.
|
---|
174 | * @param pszFunction The source position - function.
|
---|
175 | */
|
---|
176 | RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Try enter a critical section.
|
---|
180 | *
|
---|
181 | * @retval VINF_SUCCESS on success.
|
---|
182 | * @retval VERR_SEM_BUSY if the critsect was owned.
|
---|
183 | * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
184 | * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
185 | *
|
---|
186 | * @param pCritSect The critical section.
|
---|
187 | */
|
---|
188 | RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect);
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Try enter a critical section.
|
---|
192 | *
|
---|
193 | * @retval VINF_SUCCESS on success.
|
---|
194 | * @retval VERR_SEM_BUSY if the critsect was owned.
|
---|
195 | * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
196 | * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
197 | *
|
---|
198 | * @param pCritSect The critical section.
|
---|
199 | * @param uId Where we're entering the section.
|
---|
200 | * @param pszFile The source position - file.
|
---|
201 | * @param iLine The source position - line.
|
---|
202 | * @param pszFunction The source position - function.
|
---|
203 | */
|
---|
204 | RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Enter multiple critical sections.
|
---|
208 | *
|
---|
209 | * This function will enter ALL the specified critical sections before returning.
|
---|
210 | *
|
---|
211 | * @returns VINF_SUCCESS on success.
|
---|
212 | * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
213 | * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
214 | * @param cCritSects Number of critical sections in the array.
|
---|
215 | * @param papCritSects Array of critical section pointers.
|
---|
216 | *
|
---|
217 | * @remark Please note that this function will not necessarily come out favourable in a
|
---|
218 | * fight with other threads which are using the normal RTCritSectEnter() function.
|
---|
219 | * Therefore, avoid having to enter multiple critical sections!
|
---|
220 | */
|
---|
221 | RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Enter multiple critical sections.
|
---|
225 | *
|
---|
226 | * This function will enter ALL the specified critical sections before returning.
|
---|
227 | *
|
---|
228 | * @returns VINF_SUCCESS on success.
|
---|
229 | * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
|
---|
230 | * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
|
---|
231 | *
|
---|
232 | * @param cCritSects Number of critical sections in the array.
|
---|
233 | * @param papCritSects Array of critical section pointers.
|
---|
234 | * @param uId Where we're entering the section.
|
---|
235 | * @param pszFile The source position - file.
|
---|
236 | * @param iLine The source position - line.
|
---|
237 | * @param pszFunction The source position - function.
|
---|
238 | *
|
---|
239 | * @remark See RTCritSectEnterMultiple().
|
---|
240 | */
|
---|
241 | RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL);
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Leave a critical section.
|
---|
245 | *
|
---|
246 | * @returns VINF_SUCCESS.
|
---|
247 | * @param pCritSect The critical section.
|
---|
248 | */
|
---|
249 | RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect);
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Leave multiple critical sections.
|
---|
253 | *
|
---|
254 | * @returns VINF_SUCCESS.
|
---|
255 | * @param cCritSects Number of critical sections in the array.
|
---|
256 | * @param papCritSects Array of critical section pointers.
|
---|
257 | */
|
---|
258 | RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Deletes a critical section.
|
---|
262 | *
|
---|
263 | * @returns VINF_SUCCESS.
|
---|
264 | * @param pCritSect The critical section.
|
---|
265 | */
|
---|
266 | RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect);
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Checks the caller is the owner of the critical section.
|
---|
270 | *
|
---|
271 | * @returns true if owner.
|
---|
272 | * @returns false if not owner.
|
---|
273 | * @param pCritSect The critical section.
|
---|
274 | */
|
---|
275 | DECLINLINE(bool) RTCritSectIsOwner(PCRTCRITSECT pCritSect)
|
---|
276 | {
|
---|
277 | return pCritSect->NativeThreadOwner == RTThreadNativeSelf();
|
---|
278 | }
|
---|
279 |
|
---|
280 | #endif /* IN_RING3 */
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Checks the section is owned by anyone.
|
---|
284 | *
|
---|
285 | * @returns true if owned.
|
---|
286 | * @returns false if not owned.
|
---|
287 | * @param pCritSect The critical section.
|
---|
288 | */
|
---|
289 | DECLINLINE(bool) RTCritSectIsOwned(PCRTCRITSECT pCritSect)
|
---|
290 | {
|
---|
291 | return pCritSect->NativeThreadOwner != NIL_RTNATIVETHREAD;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Gets the thread id of the critical section owner.
|
---|
296 | *
|
---|
297 | * @returns Thread id of the owner thread if owned.
|
---|
298 | * @returns NIL_RTNATIVETHREAD is not owned.
|
---|
299 | * @param pCritSect The critical section.
|
---|
300 | */
|
---|
301 | DECLINLINE(RTNATIVETHREAD) RTCritSectGetOwner(PCRTCRITSECT pCritSect)
|
---|
302 | {
|
---|
303 | return pCritSect->NativeThreadOwner;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Checks if a critical section is initialized or not.
|
---|
308 | *
|
---|
309 | * @returns true if initialized.
|
---|
310 | * @returns false if not initialized.
|
---|
311 | * @param pCritSect The critical section.
|
---|
312 | */
|
---|
313 | DECLINLINE(bool) RTCritSectIsInitialized(PCRTCRITSECT pCritSect)
|
---|
314 | {
|
---|
315 | return pCritSect->u32Magic == RTCRITSECT_MAGIC;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Gets the recursion depth.
|
---|
320 | *
|
---|
321 | * @returns The recursion depth.
|
---|
322 | * @param pCritSect The Critical section
|
---|
323 | */
|
---|
324 | DECLINLINE(uint32_t) RTCritSectGetRecursion(PCRTCRITSECT pCritSect)
|
---|
325 | {
|
---|
326 | return pCritSect->cNestings;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Gets the waiter count
|
---|
331 | *
|
---|
332 | * @returns The waiter count
|
---|
333 | * @param pCritSect The Critical section
|
---|
334 | */
|
---|
335 | DECLINLINE(int32_t) RTCritSectGetWaiters(PCRTCRITSECT pCritSect)
|
---|
336 | {
|
---|
337 | return pCritSect->cLockers;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* Lock strict build: Remap the three enter calls to the debug versions. */
|
---|
341 | #if defined(RT_LOCK_STRICT) && !defined(RTCRITSECT_WITHOUT_REMAPPING) && !defined(RT_WITH_MANGLING)
|
---|
342 | # ifdef ___iprt_asm_h
|
---|
343 | # define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
|
---|
344 | # define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
|
---|
345 | # define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
|
---|
346 | # else
|
---|
347 | # define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, 0, RT_SRC_POS)
|
---|
348 | # define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, 0, RT_SRC_POS)
|
---|
349 | # define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), 0, RT_SRC_POS)
|
---|
350 | # endif
|
---|
351 | #endif
|
---|
352 |
|
---|
353 | /* Strict lock order: Automatically classify locks by init location. */
|
---|
354 | #if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3) && !defined(RTCRITSECT_WITHOUT_REMAPPING) &&!defined(RT_WITH_MANGLING)
|
---|
355 | # define RTCritSectInit(pCritSect) \
|
---|
356 | RTCritSectInitEx((pCritSect), 0 /*fFlags*/, \
|
---|
357 | RTLockValidatorClassForSrcPos(RT_SRC_POS, NULL), \
|
---|
358 | RTLOCKVAL_SUB_CLASS_NONE, NULL)
|
---|
359 | #endif
|
---|
360 |
|
---|
361 | /** @} */
|
---|
362 |
|
---|
363 | RT_C_DECLS_END
|
---|
364 |
|
---|
365 | #endif
|
---|
366 |
|
---|