VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semmutex-r0drv-linux.c@ 28436

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

IPRT: Reimplemented RTSemMutex for ring-0 linux - untested.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.8 KB
Line 
1/* $Id: semmutex-r0drv-linux.c 28436 2010-04-17 21:40:51Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-linux-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/mem.h>
42#include <iprt/err.h>
43#include <iprt/list.h>
44
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51typedef struct RTSEMMUTEXLNXWAITER
52{
53 /** The list entry. */
54 RTLISTNODE ListEntry;
55 /** The waiting task. */
56 struct task_struct *pTask;
57 /** Why did we wake up? */
58 enum
59 {
60 /** Wakeup to take the semaphore. */
61 RTSEMMUTEXLNXWAITER_WAKEUP,
62 /** Mutex is being destroyed. */
63 RTSEMMUTEXLNXWAITER_DESTROYED,
64 /** Some other reason. */
65 RTSEMMUTEXLNXWAITER_OTHER
66 } volatile enmReason;
67} RTSEMMUTEXLNXWAITER, *PRTSEMMUTEXLNXWAITER;
68
69/**
70 * Wrapper for the linux semaphore structure.
71 */
72typedef struct RTSEMMUTEXINTERNAL
73{
74 /** Magic value (RTSEMMUTEX_MAGIC). */
75 uint32_t u32Magic;
76 /** The number of recursions. */
77 uint32_t cRecursions;
78 /** The list of waiting threads. */
79 RTLISTNODE WaiterList;
80 /** The current owner, NULL if none. */
81 struct task_struct *pOwnerTask;
82 /** The number of references to this piece of memory. This is used to
83 * prevent it from being kicked from underneath us while waiting. */
84 uint32_t volatile cRefs;
85 /** The spinlock protecting the members and falling asleep. */
86 spinlock_t Spinlock;
87} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
88
89
90RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMtx)
91{
92 /*
93 * Allocate.
94 */
95 PRTSEMMUTEXINTERNAL pThis;
96 pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
97 if (!pThis)
98 return VERR_NO_MEMORY;
99
100 /*
101 * Initialize.
102 */
103 pThis->u32Magic = RTSEMMUTEX_MAGIC;
104 pThis->cRecursions = 0;
105 pThis->pOwnerTask = NULL;
106 pThis->cRefs = 1;
107 RTListInit(&pThis->WaiterList);
108 spin_lock_init(&pThis->Spinlock);
109
110 *phMtx = pThis;
111 return VINF_SUCCESS;
112}
113RT_EXPORT_SYMBOL(RTSemMutexCreate);
114
115
116RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMtx)
117{
118 PRTSEMMUTEXINTERNAL pThis = hMtx;
119 PRTSEMMUTEXLNXWAITER pCur;
120 unsigned long fSavedIrq;
121
122 /*
123 * Validate.
124 */
125 if (pThis == NIL_RTSEMMUTEX)
126 return VINF_SUCCESS;
127 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
128 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
129
130 /*
131 * Kill it, kick waiters and release it.
132 */
133 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
134
135 spin_lock_irqsave(&pThis->Spinlock, fSavedIrq);
136 RTListForEach(&pThis->WaiterList, pCur, RTSEMMUTEXLNXWAITER, ListEntry)
137 {
138 pCur->enmReason = RTSEMMUTEXLNXWAITER_DESTROYED;
139 wake_up_process(pCur->pTask);
140 }
141
142 if (ASMAtomicDecU32(&pThis->cRefs) != 0)
143 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
144 else
145 {
146 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
147 RTMemFree(pThis);
148 }
149
150 return VINF_SUCCESS;
151}
152RT_EXPORT_SYMBOL(RTSemMutexDestroy);
153
154
155/**
156 * Worker for rtSemMutexLinuxRequest that handles the case where we go to sleep.
157 *
158 * @returns VINF_SUCCESS, VERR_INTERRUPTED, VERR_TIMEOUT or VERR_SEM_DESTROYED.
159 * Returns without owning the spinlock.
160 * @param pThis The mutex instance.
161 * @param cMillies The timeout.
162 * @param fInterruptible The wait type.
163 * @param fSavedIrq The saved IRQ flags.
164 */
165static int rtSemMutexLinuxRequestSleep(PRTSEMMUTEXINTERNAL pThis, RTMSINTERVAL cMillies,
166 bool fInterruptible, unsigned long fSavedIrq)
167{
168 struct task_struct *pSelf = current;
169 int rc = VERR_TIMEOUT;
170 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
171 RTSEMMUTEXLNXWAITER Waiter;
172
173 IPRT_DEBUG_SEMS_STATE(pThis, 'm');
174
175 /*
176 * Grab a reference to the mutex and add ourselves to the waiter list.
177 */
178 ASMAtomicIncU32(&pThis->cRefs);
179
180 Waiter.pTask = pSelf;
181 Waiter.enmReason = RTSEMMUTEXLNXWAITER_OTHER;
182 RTListAppend(&pThis->WaiterList, &Waiter.ListEntry);
183
184 /*
185 * Do the waiting.
186 */
187 for (;;)
188 {
189 /* Check signal and timeout conditions. */
190 if ( fInterruptible
191 && signal_pending(pSelf))
192 {
193 rc = VERR_INTERRUPTED;
194 break;
195 }
196
197 if (!lTimeout)
198 break;
199
200 /* Go to sleep. */
201 set_task_state(pSelf, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
202 spin_unlock_irq(&pThis->Spinlock);
203
204 lTimeout = schedule_timeout(lTimeout);
205
206 spin_lock_irq(&pThis->Spinlock);
207 set_current_state(TASK_RUNNING);
208
209 /* Did someone wake us up? */
210 if (Waiter.enmReason == RTSEMMUTEXLNXWAITER_WAKEUP)
211 {
212 Assert(pThis->cRecursions == 0);
213 pThis->cRecursions = 1;
214 pThis->pOwnerTask = NULL;
215 rc = VINF_SUCCESS;
216 break;
217 }
218
219 /* Is the mutex being destroyed? */
220 if (RT_UNLIKELY( Waiter.enmReason == RTSEMMUTEXLNXWAITER_DESTROYED
221 || pThis->u32Magic != RTSEMMUTEX_MAGIC))
222 {
223 rc = VERR_SEM_DESTROYED;
224 break;
225 }
226 }
227
228 /*
229 * Unlink ourself from the waiter list, dereference the mutex and exit the
230 * lock. We might have to free the mutex if it was the destroyed.
231 */
232 RTListNodeRemove(&Waiter.ListEntry);
233 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'M', rc);
234
235 if (RT_LIKELY(ASMAtomicDecU32(&pThis->cRefs) != 0))
236 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
237 else
238 {
239 Assert(RT_FAILURE_NP(rc));
240 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
241 RTMemFree(pThis);
242 }
243 return rc;
244}
245
246
247/**
248 * Internal worker.
249 */
250DECLINLINE(int) rtSemMutexLinuxRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, bool fInterruptible)
251{
252 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
253 struct task_struct *pSelf = current;
254 unsigned long fSavedIrq;
255 int rc;
256
257 /*
258 * Validate.
259 */
260 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
261 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
262 Assert(pThis->cRefs >= 1);
263
264 /*
265 * Lock it and check if it's a recursion.
266 */
267 spin_lock_irqsave(&pThis->Spinlock, fSavedIrq);
268 if (pThis->pOwnerTask == pSelf)
269 {
270 pThis->cRecursions++;
271 Assert(pThis->cRecursions > 1);
272 Assert(pThis->cRecursions < 256);
273 rc = VINF_SUCCESS;
274 }
275 /*
276 * Not a recursion, maybe it's not owned by anyone then?
277 */
278 else if (pThis->pOwnerTask == NULL)
279 {
280 Assert(pThis->cRecursions == 0);
281 pThis->cRecursions = 1;
282 pThis->pOwnerTask = pSelf;
283 rc = VINF_SUCCESS;
284 }
285 /*
286 * Was it a polling call?
287 */
288 else if (cMillies == 0)
289 rc = VERR_TIMEOUT;
290 /*
291 * No, so go to sleep.
292 */
293 else
294 return rtSemMutexLinuxRequestSleep(pThis, cMillies, fInterruptible, fSavedIrq);
295
296 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'M', rc);
297 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
298 return rc;
299}
300
301
302#undef RTSemMutexRequest
303RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
304{
305 return rtSemMutexLinuxRequest(hMutexSem, cMillies, false /*fInterruptible*/);
306}
307RT_EXPORT_SYMBOL(RTSemMutexRequest);
308
309
310RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
311{
312 return RTSemMutexRequest(hMutexSem, cMillies);
313}
314RT_EXPORT_SYMBOL(RTSemMutexRequestDebug);
315
316
317#undef RTSemMutexRequestNoResume
318RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
319{
320 return rtSemMutexLinuxRequest(hMutexSem, cMillies, true /*fInterruptible*/);
321}
322RT_EXPORT_SYMBOL(RTSemMutexRequest);
323
324
325RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
326{
327 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
328}
329RT_EXPORT_SYMBOL(RTSemMutexRequestNoResumeDebug);
330
331
332RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMtx)
333{
334 PRTSEMMUTEXINTERNAL pThis = hMtx;
335 struct task_struct *pSelf = current;
336 unsigned long fSavedIrq;
337 int rc;
338
339 /*
340 * Validate.
341 */
342 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
343 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
344 Assert(pThis->cRefs >= 1);
345
346 /*
347 * Take the lock and release one recursion.
348 */
349 spin_lock_irqsave(&pThis->Spinlock, fSavedIrq);
350 if (pThis->pOwnerTask == pSelf)
351 {
352 Assert(pThis->cRecursions > 0);
353 if (--pThis->cRecursions == 0)
354 {
355 pThis->pOwnerTask = NULL;
356
357 /* anyone to wake up? */
358 if (!RTListIsEmpty(&pThis->WaiterList))
359 {
360 PRTSEMMUTEXLNXWAITER pWaiter = RTListNodeGetFirst(&pThis->WaiterList, RTSEMMUTEXLNXWAITER, ListEntry);
361 pWaiter->enmReason = RTSEMMUTEXLNXWAITER_WAKEUP;
362 wake_up_process(pWaiter->pTask);
363 }
364 IPRT_DEBUG_SEMS_STATE(pThis, 'u');
365 }
366 }
367 else
368 rc = VERR_NOT_OWNER;
369 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
370
371 AssertRC(rc);
372 return rc;
373}
374RT_EXPORT_SYMBOL(RTSemMutexRelease);
375
376
377RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
378{
379 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
380 unsigned long fSavedIrq;
381 bool fOwned;
382
383 /*
384 * Validate.
385 */
386 AssertPtrReturn(pThis, false);
387 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), false);
388 Assert(pThis->cRefs >= 1);
389
390 /*
391 * Take the lock and release one recursion.
392 */
393 spin_lock_irqsave(&pThis->Spinlock, fSavedIrq);
394 fOwned = pThis->pOwnerTask != NULL;
395 spin_unlock_irqrestore(&pThis->Spinlock, fSavedIrq);
396
397 return fOwned;
398
399}
400RT_EXPORT_SYMBOL(RTSemMutexIsOwned);
401
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