VirtualBox

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

Last change on this file since 25671 was 25645, checked in by vboxsync, 15 years ago

IPRT,DoxyFile.Core: Mopped up the errors in the IPRT doxygen run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 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
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(PRTLOCKVALRECEXCL) 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 pszFile The source position - file.
141 * @param iLine The source position - line.
142 * @param pszFunction The source position - function.
143 */
144RTDECL(int) RTCritSectEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
145
146/**
147 * Try enter a critical section.
148 *
149 * @retval VINF_SUCCESS on success.
150 * @retval VERR_SEM_BUSY if the critsect was owned.
151 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
152 * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
153 *
154 * @param pCritSect The critical section.
155 */
156RTDECL(int) RTCritSectTryEnter(PRTCRITSECT pCritSect);
157
158/**
159 * Try enter a critical section.
160 *
161 * @retval VINF_SUCCESS on success.
162 * @retval VERR_SEM_BUSY if the critsect was owned.
163 * @retval VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
164 * @retval VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
165 *
166 * @param pCritSect The critical section.
167 * @param uId Where we're entering the section.
168 * @param pszFile The source position - file.
169 * @param iLine The source position - line.
170 * @param pszFunction The source position - function.
171 */
172RTDECL(int) RTCritSectTryEnterDebug(PRTCRITSECT pCritSect, RTHCUINTPTR uId, RT_SRC_POS_DECL);
173
174/**
175 * Enter multiple critical sections.
176 *
177 * This function will enter ALL the specified critical sections before returning.
178 *
179 * @returns VINF_SUCCESS on success.
180 * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
181 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
182 * @param cCritSects Number of critical sections in the array.
183 * @param papCritSects Array of critical section pointers.
184 *
185 * @remark Please note that this function will not necessarily come out favourable in a
186 * fight with other threads which are using the normal RTCritSectEnter() function.
187 * Therefore, avoid having to enter multiple critical sections!
188 */
189RTDECL(int) RTCritSectEnterMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
190
191/**
192 * Enter multiple critical sections.
193 *
194 * This function will enter ALL the specified critical sections before returning.
195 *
196 * @returns VINF_SUCCESS on success.
197 * @returns VERR_SEM_NESTED if nested enter on a no nesting section. (Asserted.)
198 * @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
199 *
200 * @param cCritSects Number of critical sections in the array.
201 * @param papCritSects Array of critical section pointers.
202 * @param uId Where we're entering the section.
203 * @param pszFile The source position - file.
204 * @param iLine The source position - line.
205 * @param pszFunction The source position - function.
206 *
207 * @remark See RTCritSectEnterMultiple().
208 */
209RTDECL(int) RTCritSectEnterMultipleDebug(size_t cCritSects, PRTCRITSECT *papCritSects, RTUINTPTR uId, RT_SRC_POS_DECL);
210
211/**
212 * Leave a critical section.
213 *
214 * @returns VINF_SUCCESS.
215 * @param pCritSect The critical section.
216 */
217RTDECL(int) RTCritSectLeave(PRTCRITSECT pCritSect);
218
219/**
220 * Leave multiple critical sections.
221 *
222 * @returns VINF_SUCCESS.
223 * @param cCritSects Number of critical sections in the array.
224 * @param papCritSects Array of critical section pointers.
225 */
226RTDECL(int) RTCritSectLeaveMultiple(size_t cCritSects, PRTCRITSECT *papCritSects);
227
228/**
229 * Deletes a critical section.
230 *
231 * @returns VINF_SUCCESS.
232 * @param pCritSect The critical section.
233 */
234RTDECL(int) RTCritSectDelete(PRTCRITSECT pCritSect);
235
236/**
237 * Checks the caller is the owner of the critical section.
238 *
239 * @returns true if owner.
240 * @returns false if not owner.
241 * @param pCritSect The critical section.
242 */
243DECLINLINE(bool) RTCritSectIsOwner(PCRTCRITSECT pCritSect)
244{
245 return pCritSect->NativeThreadOwner == RTThreadNativeSelf();
246}
247
248#endif /* IN_RING3 */
249
250/**
251 * Checks the section is owned by anyone.
252 *
253 * @returns true if owned.
254 * @returns false if not owned.
255 * @param pCritSect The critical section.
256 */
257DECLINLINE(bool) RTCritSectIsOwned(PCRTCRITSECT pCritSect)
258{
259 return pCritSect->NativeThreadOwner != NIL_RTNATIVETHREAD;
260}
261
262/**
263 * Gets the thread id of the critical section owner.
264 *
265 * @returns Thread id of the owner thread if owned.
266 * @returns NIL_RTNATIVETHREAD is not owned.
267 * @param pCritSect The critical section.
268 */
269DECLINLINE(RTNATIVETHREAD) RTCritSectGetOwner(PCRTCRITSECT pCritSect)
270{
271 return pCritSect->NativeThreadOwner;
272}
273
274/**
275 * Checks if a critical section is initialized or not.
276 *
277 * @returns true if initialized.
278 * @returns false if not initialized.
279 * @param pCritSect The critical section.
280 */
281DECLINLINE(bool) RTCritSectIsInitialized(PCRTCRITSECT pCritSect)
282{
283 return pCritSect->u32Magic == RTCRITSECT_MAGIC;
284}
285
286/**
287 * Gets the recursion depth.
288 *
289 * @returns The recursion depth.
290 * @param pCritSect The Critical section
291 */
292DECLINLINE(uint32_t) RTCritSectGetRecursion(PCRTCRITSECT pCritSect)
293{
294 return pCritSect->cNestings;
295}
296
297/**
298 * Gets the waiter count
299 *
300 * @returns The waiter count
301 * @param pCritSect The Critical section
302 */
303DECLINLINE(int32_t) RTCritSectGetWaiters(PCRTCRITSECT pCritSect)
304{
305 return pCritSect->cLockers;
306}
307
308/* Strict build: Remap the three enter calls to the debug versions. */
309#ifdef RT_STRICT
310# ifdef ___iprt_asm_h
311# define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
312# define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
313# define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), (uintptr_t)ASMReturnAddress(), RT_SRC_POS)
314# else
315# define RTCritSectEnter(pCritSect) RTCritSectEnterDebug(pCritSect, 0, RT_SRC_POS)
316# define RTCritSectTryEnter(pCritSect) RTCritSectTryEnterDebug(pCritSect, 0, RT_SRC_POS)
317# define RTCritSectEnterMultiple(cCritSects, pCritSect) RTCritSectEnterMultipleDebug((cCritSects), (pCritSect), 0, RT_SRC_POS)
318# endif
319#endif
320
321/** @} */
322
323RT_C_DECLS_END
324
325#endif
326
Note: See TracBrowser for help on using the repository browser.

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