VirtualBox

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

Last change on this file since 25370 was 25368, checked in by vboxsync, 15 years ago

RTCritSect,PDMCritSect,iprt/lockvalidator.h: Reworked the deadlocking detection for critical sections and preparing for lock order validation. This change generalizes the RTCRITSECT::Strict data and moves it out of the RTCRITSECT, leaving a pointer behind. This saves a bit of space in release builds.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 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/lockvalidator.h>
36#ifdef IN_RING3
37#include <iprt/thread.h>
38#endif
39
40
41RT_C_DECLS_BEGIN
42
43/** @defgroup grp_rt_critsect RTCritSect - Critical Sections
44 *
45 * "Critical section" synchronization primitives can be used to
46 * protect a section of code or data to which access must be exclusive;
47 * only one thread can hold access to a critical section at one time.
48 *
49 * A critical section is a fast recursive write lock; if the critical
50 * section is not acquired, then entering it is fast (requires no system
51 * call). IPRT uses the Windows terminology here; on other platform, this
52 * might be called a "futex" or a "fast mutex". As opposed to IPRT
53 * "fast mutexes" (see @ref grp_rt_sems_fast_mutex ), critical sections
54 * are recursive.
55 *
56 * Use RTCritSectInit to initialize a critical section; use RTCritSectEnter
57 * and RTCritSectLeave to acquire and release access.
58 *
59 * For an overview of all types of synchronization primitives provided
60 * by IPRT (event, mutex/fast mutex/read-write mutex semaphores), see
61 * @ref grp_rt_sems .
62 *
63 * @ingroup grp_rt
64 * @{
65 */
66
67/**
68 * Critical section.
69 */
70typedef struct RTCRITSECT
71{
72 /** Magic used to validate the section state.
73 * RTCRITSECT_MAGIC is the value of an initialized & operational section. */
74 volatile uint32_t u32Magic;
75 /** Number of lockers.
76 * -1 if the section is free. */
77 volatile int32_t cLockers;
78 /** The owner thread. */
79 volatile RTNATIVETHREAD NativeThreadOwner;
80 /** Number of nested enter operations performed.
81 * Greater or equal to 1 if owned, 0 when free.
82 */
83 volatile int32_t cNestings;
84 /** Section flags - the RTCRITSECT_FLAGS_* \#defines. */
85 uint32_t fFlags;
86 /** The semaphore to block on. */
87 RTSEMEVENT EventSem;
88 /** Lock validator record. Only used in strict builds. */
89 R3R0PTRTYPE(PRTLOCKVALIDATORREC) pValidatorRec;
90 /** Alignmnet padding. */
91 RTHCPTR Alignment;
92} RTCRITSECT;
93AssertCompileSize(RTCRITSECT, HC_ARCH_BITS == 32 ? 32 : 48);
94/** Pointer to a critical section. */
95typedef RTCRITSECT *PRTCRITSECT;
96/** Pointer to a const critical section. */
97typedef const RTCRITSECT *PCRTCRITSECT;
98
99/** RTCRITSECT::u32Magic value. (Hiromi Uehara) */
100#define RTCRITSECT_MAGIC UINT32_C(0x19790326)
101
102/** If set, nesting(/recursion) is not allowed. */
103#define RTCRITSECT_FLAGS_NO_NESTING UINT32_C(0x00000001)
104
105#ifdef IN_RING3
106
107/**
108 * Initialize a critical section.
109 */
110RTDECL(int) RTCritSectInit(PRTCRITSECT pCritSect);
111
112/**
113 * Initialize a critical section.
114 *
115 * @returns iprt status code.
116 * @param pCritSect Pointer to the critical section structure.
117 * @param fFlags Flags, any combination of the RTCRITSECT_FLAGS \#defines.
118 */
119RTDECL(int) RTCritSectInitEx(PRTCRITSECT pCritSect, uint32_t fFlags);
120
121/**
122 * Enter a critical section.
123 *
124 * @returns VINF_SUCCESS on success.
125 * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
126 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
127 * @param pCritSect The critical section.
128 */
129RTDECL(int) RTCritSectEnter(PRTCRITSECT pCritSect);
130
131/**
132 * Enter a critical section.
133 *
134 * @retval VINF_SUCCESS on success.
135 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
136 * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
137 *
138 * @param pCritSect The critical section.
139 * @param uId Where we're entering the section.
140 * @param RT_SRC_POS_DECL The source position.
141 */
142RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
143
144/**
145 * Try enter a critical section.
146 *
147 * @retval VINF_SUCCESS on success.
148 * @retval VERR_SEM_BUSY if the critsect was owned.
149 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
150 * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
151 *
152 * @param pCritSect The critical section.
153 */
154RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect);
155
156/**
157 * Try enter a critical section.
158 *
159 * @retval VINF_SUCCESS on success.
160 * @retval VERR_SEM_BUSY if the critsect was owned.
161 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
162 * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
163 *
164 * @param pCritSect The critical section.
165 * @param uId Where we're entering the section.
166 * @param RT_SRC_POS_DECL The source position.
167 */
168RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
169
170/**
171 * Enter multiple critical sections.
172 *
173 * This function will enter ALL the specified critical sections before returning.
174 *
175 * @returns VINF_SUCCESS on success.
176 * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
177 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
178 * @param cCritSects Number of critical sections in the array.
179 * @param papCritSects Array of critical section pointers.
180 *
181 * @remark Please note that this function will not necessarily come out favourable in a
182 * fight with other threads which are using the normal RTCritSectEnter() function.
183 * Therefore, avoid having to enter multiple critical sections!
184 */
185RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
186
187/**
188 * Enter multiple critical sections.
189 *
190 * This function will enter ALL the specified critical sections before returning.
191 *
192 * @returns VINF_SUCCESS on success.
193 * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
194 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
195 *
196 * @param cCritSects Number of critical sections in the array.
197 * @param papCritSects Array of critical section pointers.
198 * @param uId Where we're entering the section.
199 * @param RT_SRC_POS_DECL The source position.
200 *
201 * @remark See RTCritSectEnterMultiple().
202 */
203RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL);
204
205/**
206 * Leave a critical section.
207 *
208 * @returns VINF_SUCCESS.
209 * @param pCritSect The critical section.
210 */
211RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect);
212
213/**
214 * Leave multiple critical sections.
215 *
216 * @returns VINF_SUCCESS.
217 * @param cCritSects Number of critical sections in the array.
218 * @param papCritSects Array of critical section pointers.
219 */
220RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
221
222/**
223 * Deletes a critical section.
224 *
225 * @returns VINF_SUCCESS.
226 * @param pCritSect The critical section.
227 */
228RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect);
229
230/**
231 * Checks the caller is the owner of the critical section.
232 *
233 * @returns true if owner.
234 * @returns false if not owner.
235 * @param pCritSect The critical section.
236 */
237DECLINLINE(bool) RTCritSectIsOwner(PCRTCRITSECT pCritSect)
238{
239 return pCritSect->NativeThreadOwner == RTThreadNativeSelf();
240}
241
242#endif /* IN_RING3 */
243
244/**
245 * Checks the section is owned by anyone.
246 *
247 * @returns true if owned.
248 * @returns false if not owned.
249 * @param pCritSect The critical section.
250 */
251DECLINLINE(bool) RTCritSectIsOwned(PCRTCRITSECT pCritSect)
252{
253 return pCritSect->NativeThreadOwner != NIL_RTNATIVETHREAD;
254}
255
256/**
257 * Gets the thread id of the critical section owner.
258 *
259 * @returns Thread id of the owner thread if owned.
260 * @returns NIL_RTNATIVETHREAD is not owned.
261 * @param pCritSect The critical section.
262 */
263DECLINLINE(RTNATIVETHREAD) RTCritSectGetOwner(PCRTCRITSECT pCritSect)
264{
265 return pCritSect->NativeThreadOwner;
266}
267
268/**
269 * Checks if a critical section is initialized or not.
270 *
271 * @returns true if initialized.
272 * @returns false if not initialized.
273 * @param pCritSect The critical section.
274 */
275DECLINLINE(bool) RTCritSectIsInitialized(PCRTCRITSECT pCritSect)
276{
277 return pCritSect->u32Magic == RTCRITSECT_MAGIC;
278}
279
280/**
281 * Gets the recursion depth.
282 *
283 * @returns The recursion depth.
284 * @param pCritSect The Critical section
285 */
286DECLINLINE(uint32_t) RTCritSectGetRecursion(PCRTCRITSECT pCritSect)
287{
288 return pCritSect->cNestings;
289}
290
291/**
292 * Gets the waiter count
293 *
294 * @returns The waiter count
295 * @param pCritSect The Critical section
296 */
297DECLINLINE(int32_t) RTCritSectGetWaiters(PCRTCRITSECT pCritSect)
298{
299 return pCritSect->cLockers;
300}
301
302/* Strict build: Remap the three enter calls to the debug versions. */
303#ifdef RT_STRICT
304# ifdef ___iprt_asm_h
305# define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
306# define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
307# define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
308# else
309# define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, 0, RT_SRC_POS)
310# define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, 0, RT_SRC_POS)
311# define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), 0, RT_SRC_POS)
312# endif
313#endif
314
315/** @} */
316
317RT_C_DECLS_END
318
319#endif
320
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