1 | /** @file
|
---|
2 | * IPRT - Lock Validator.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 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_lockvalidator_h
|
---|
31 | #define ___iprt_lockvalidator_h
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /** @defgroup grp_ldr RTLockValidator - Lock Validator
|
---|
39 | * @ingroup grp_rt
|
---|
40 | * @{
|
---|
41 | */
|
---|
42 |
|
---|
43 | RT_C_DECLS_BEGIN
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Record recording the ownership of a lock.
|
---|
47 | *
|
---|
48 | * This is typically part of the per-lock data structure when compiling with
|
---|
49 | * the lock validator.
|
---|
50 | */
|
---|
51 | typedef struct RTLOCKVALIDATORREC
|
---|
52 | {
|
---|
53 | /** Magic value (RTLOCKVALIDATORREC_MAGIC). */
|
---|
54 | uint32_t u32Magic;
|
---|
55 | /** The line number in the file. */
|
---|
56 | uint32_t volatile uLine;
|
---|
57 | /** The file where the lock was taken. */
|
---|
58 | R3R0PTRTYPE(const char * volatile) pszFile;
|
---|
59 | /** The function where the lock was taken. */
|
---|
60 | R3R0PTRTYPE(const char * volatile) pszFunction;
|
---|
61 | /** Some ID indicating where the lock was taken, typically an address. */
|
---|
62 | RTHCUINTPTR volatile uId;
|
---|
63 | /** The current owner thread. */
|
---|
64 | RTTHREAD volatile hThread;
|
---|
65 | /** Pointer to the lock record below us. Only accessed by the owner. */
|
---|
66 | R3R0PTRTYPE(PRTLOCKVALIDATORREC) pDown;
|
---|
67 | /** The lock class. */
|
---|
68 | RTLOCKVALIDATORCLASS hClass;
|
---|
69 | /** The lock sub-class. */
|
---|
70 | uint32_t volatile uSubClass;
|
---|
71 | /** Reserved for future use. */
|
---|
72 | uint32_t u32Reserved;
|
---|
73 | /** Pointer to the lock. */
|
---|
74 | RTHCPTR hLock;
|
---|
75 | /** The lock name. */
|
---|
76 | R3R0PTRTYPE(const char *) pszName;
|
---|
77 | } RTLOCKVALIDATORREC;
|
---|
78 | AssertCompileSize(RTLOCKVALIDATORREC, HC_ARCH_BITS == 32 ? 48 : 80);
|
---|
79 | /* The pointer is defined in iprt/types.h */
|
---|
80 |
|
---|
81 |
|
---|
82 | /** @name Special sub-class values.
|
---|
83 | * The range 16..UINT32_MAX is available to the user, the range 0..15 is
|
---|
84 | * reserved for the lock validator.
|
---|
85 | * @{ */
|
---|
86 | /** Not allowed to be taken with any other locks in the same class.
|
---|
87 | * This is the recommended value. */
|
---|
88 | #define RTLOCKVALIDATOR_SUB_CLASS_NONE UINT32_C(0)
|
---|
89 | /** Any order is allowed within the class. */
|
---|
90 | #define RTLOCKVALIDATOR_SUB_CLASS_ANY UINT32_C(1)
|
---|
91 | /** The first user value. */
|
---|
92 | #define RTLOCKVALIDATOR_SUB_CLASS_USER UINT32_C(16)
|
---|
93 | /** @} */
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Initialize a lock validator record.
|
---|
97 | *
|
---|
98 | * Use RTLockValidatorDelete to deinitialize it.
|
---|
99 | *
|
---|
100 | * @param pRec The record.
|
---|
101 | * @param hClass The class. If NIL, the no lock order
|
---|
102 | * validation will be performed on this lock.
|
---|
103 | * @param uSubClass The sub-class. This is used to define lock
|
---|
104 | * order inside the same class. If you don't know,
|
---|
105 | * then pass RTLOCKVALIDATOR_SUB_CLASS_NONE.
|
---|
106 | * @param pszName The lock name (optional).
|
---|
107 | * @param hLock The lock handle.
|
---|
108 | */
|
---|
109 | RTDECL(void) RTLockValidatorInit(PRTLOCKVALIDATORREC pRec, RTLOCKVALIDATORCLASS hClass,
|
---|
110 | uint32_t uSubClass, const char *pszName, void *hLock);
|
---|
111 | /**
|
---|
112 | * Uninitialize a lock validator record previously initialized by
|
---|
113 | * RTLockValidatorInit.
|
---|
114 | *
|
---|
115 | * @param pRec The record. Must be valid.
|
---|
116 | */
|
---|
117 | RTDECL(void) RTLockValidatorDelete(PRTLOCKVALIDATORREC pRec);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Create and initialize a lock validator record.
|
---|
121 | *
|
---|
122 | * Use RTLockValidatorDestroy to deinitialize and destroy the returned record.
|
---|
123 | *
|
---|
124 | * @return VINF_SUCCESS or VERR_NO_MEMORY.
|
---|
125 | * @param ppRec Where to return the record pointer.
|
---|
126 | * @param hClass The class. If NIL, the no lock order
|
---|
127 | * validation will be performed on this lock.
|
---|
128 | * @param uSubClass The sub-class. This is used to define lock
|
---|
129 | * order inside the same class. If you don't know,
|
---|
130 | * then pass RTLOCKVALIDATOR_SUB_CLASS_NONE.
|
---|
131 | * @param pszName The lock name (optional).
|
---|
132 | * @param hLock The lock handle.
|
---|
133 | */
|
---|
134 | RTDECL(int) RTLockValidatorCreate(PRTLOCKVALIDATORREC *ppRec, RTLOCKVALIDATORCLASS hClass,
|
---|
135 | uint32_t uSubClass, const char *pszName, void *hLock);
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Deinitialize and destroy a record created by RTLockValidatorCreate.
|
---|
139 | *
|
---|
140 | * @param ppRec Pointer to the record pointer. Will be set to
|
---|
141 | * NULL.
|
---|
142 | */
|
---|
143 | RTDECL(void) RTLockValidatorDestroy(PRTLOCKVALIDATORREC *ppRec);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Check the locking order.
|
---|
147 | *
|
---|
148 | * This is called by routines implementing lock acquisition.
|
---|
149 | *
|
---|
150 | * @retval VINF_SUCCESS on success.
|
---|
151 | * @retval VERR_DEADLOCK if the order is wrong, after having whined and
|
---|
152 | * asserted.
|
---|
153 | *
|
---|
154 | * @param pRec The validator record.
|
---|
155 | * @param hThread The handle of the calling thread. If not known,
|
---|
156 | * pass NIL_RTTHREAD and this method will figure it
|
---|
157 | * out.
|
---|
158 | * @param uId Some kind of locking location ID. Typically a
|
---|
159 | * return address up the stack. Optional (0).
|
---|
160 | * @param pszFile The file where the lock is being acquired from.
|
---|
161 | * Optional.
|
---|
162 | * @param iLine The line number in that file. Optional (0).
|
---|
163 | * @param pszFunction The functionn where the lock is being acquired
|
---|
164 | * from. Optional.
|
---|
165 | */
|
---|
166 | RTDECL(int) RTLockValidatorCheckOrder(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, RTHCUINTPTR uId, RT_SRC_POS_DECL);
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Record the specified thread as lock owner.
|
---|
171 | *
|
---|
172 | * This is typically called after acquiring the lock.
|
---|
173 | *
|
---|
174 | * @returns hThread resolved. Can return NIL_RTHREAD iff we fail to adopt the
|
---|
175 | * alien thread or if pRec is invalid.
|
---|
176 | *
|
---|
177 | * @param pRec The validator record.
|
---|
178 | * @param hThread The handle of the calling thread. If not known,
|
---|
179 | * pass NIL_RTTHREAD and this method will figure it
|
---|
180 | * out.
|
---|
181 | * @param uId Some kind of locking location ID. Typically a
|
---|
182 | * return address up the stack. Optional (0).
|
---|
183 | * @param pszFile The file where the lock is being acquired from.
|
---|
184 | * Optional.
|
---|
185 | * @param iLine The line number in that file. Optional (0).
|
---|
186 | * @param pszFunction The functionn where the lock is being acquired
|
---|
187 | * from. Optional.
|
---|
188 | */
|
---|
189 | RTDECL(RTTHREAD) RTLockValidatorSetOwner(PRTLOCKVALIDATORREC pRec, RTTHREAD hThread, RTHCUINTPTR uId, RT_SRC_POS_DECL);
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Clear the lock ownership.
|
---|
194 | *
|
---|
195 | * This is typically called before release the lock.
|
---|
196 | *
|
---|
197 | * @returns The thread handle of the previous owner. NIL_RTTHREAD if the record
|
---|
198 | * is invalid or didn't have any owner.
|
---|
199 | * @param pRec The validator record.
|
---|
200 | */
|
---|
201 | RTDECL(RTTHREAD) RTLockValidatorUnsetOwner(PRTLOCKVALIDATORREC pRec);
|
---|
202 |
|
---|
203 |
|
---|
204 | /*RTDECL(int) RTLockValidatorClassCreate();*/
|
---|
205 |
|
---|
206 | RT_C_DECLS_END
|
---|
207 |
|
---|
208 | /** @} */
|
---|
209 |
|
---|
210 | #endif
|
---|
211 |
|
---|
212 |
|
---|