VirtualBox

source: vbox/trunk/include/iprt/critsect.h@ 27615

Last change on this file since 27615 was 25759, checked in by vboxsync, 15 years ago

iprt/semaphore.h: RT_LOCK_CHECK_ORDER && IN_RING3 -> wrap RTSemRWCreate and RTSemMutexCreate so automatic order validation is performed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1/** @file
2 * IPRT - Critical Sections.
3 */
4
5/*
6 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_critsect_h
31#define ___iprt_critsect_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#include <iprt/assert.h>
36#ifdef IN_RING3
37# include <iprt/thread.h>
38#endif
39#ifdef RT_LOCK_STRICT_ORDER
40# include <iprt/lockvalidator.h>
41#endif
42
43RT_C_DECLS_BEGIN
44
45/** @defgroup grp_rt_critsect RTCritSect - Critical Sections
46 *
47 * "Critical section" synchronization primitives can be used to
48 * protect a section of code or data to which access must be exclusive;
49 * only one thread can hold access to a critical section at one time.
50 *
51 * A critical section is a fast recursive write lock; if the critical
52 * section is not acquired, then entering it is fast (requires no system
53 * call). IPRT uses the Windows terminology here; on other platform, this
54 * might be called a "futex" or a "fast mutex". As opposed to IPRT
55 * "fast mutexes" (see @ref grp_rt_sems_fast_mutex ), critical sections
56 * are recursive.
57 *
58 * Use RTCritSectInit to initialize a critical section; use RTCritSectEnter
59 * and RTCritSectLeave to acquire and release access.
60 *
61 * For an overview of all types of synchronization primitives provided
62 * by IPRT (event, mutex/fast mutex/read-write mutex semaphores), see
63 * @ref grp_rt_sems .
64 *
65 * @ingroup grp_rt
66 * @{
67 */
68
69/**
70 * Critical section.
71 */
72typedef struct RTCRITSECT
73{
74 /** Magic used to validate the section state.
75 * RTCRITSECT_MAGIC is the value of an initialized & operational section. */
76 volatile uint32_t u32Magic;
77 /** Number of lockers.
78 * -1 if the section is free. */
79 volatile int32_t cLockers;
80 /** The owner thread. */
81 volatile RTNATIVETHREAD NativeThreadOwner;
82 /** Number of nested enter operations performed.
83 * Greater or equal to 1 if owned, 0 when free.
84 */
85 volatile int32_t cNestings;
86 /** Section flags - the RTCRITSECT_FLAGS_* \#defines. */
87 uint32_t fFlags;
88 /** The semaphore to block on. */
89 RTSEMEVENT EventSem;
90 /** Lock validator record. Only used in strict builds. */
91 R3R0PTRTYPE(PRTLOCKVALRECEXCL) pValidatorRec;
92 /** Alignmnet padding. */
93 RTHCPTR Alignment;
94} RTCRITSECT;
95AssertCompileSize(RTCRITSECT, HC_ARCH_BITS == 32 ? 32 : 48);
96/** Pointer to a critical section. */
97typedef RTCRITSECT *PRTCRITSECT;
98/** Pointer to a const critical section. */
99typedef const RTCRITSECT *PCRTCRITSECT;
100
101/** RTCRITSECT::u32Magic value. (Hiromi Uehara) */
102#define RTCRITSECT_MAGIC UINT32_C(0x19790326)
103
104/** @name RTCritSectInitEx flags / RTCRITSECT::fFlags
105 * @{ */
106/** If set, nesting(/recursion) is not allowed. */
107#define RTCRITSECT_FLAGS_NO_NESTING UINT32_C(0x00000001)
108/** Disables lock validation. */
109#define RTCRITSECT_FLAGS_NO_LOCK_VAL UINT32_C(0x00000002)
110/** @} */
111
112#ifdef IN_RING3
113
114/**
115 * Initialize a critical section.
116 */
117RTDECL(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 */
136RTDECL(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 */
151RTDECL(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 */
161RTDECL(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 */
176RTDECL(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 */
188RTDECL(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 */
204RTDECL(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 */
221RTDECL(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 */
241RTDECL(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 */
249RTDECL(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 */
258RTDECL(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 */
266RTDECL(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 */
275DECLINLINE(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 */
289DECLINLINE(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 */
301DECLINLINE(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 */
313DECLINLINE(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 */
324DECLINLINE(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 */
335DECLINLINE(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#ifdef RT_LOCK_STRICT
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)
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
363RT_C_DECLS_END
364
365#endif
366
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette