VirtualBox

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

Last change on this file since 13665 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/* $Id: semmutex-r0drv-linux.c 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include "the-linux-kernel.h"
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Linux mutex semaphore.
51 */
52typedef struct RTSEMMUTEXINTERNAL
53{
54 /** Magic value (RTSEMMUTEX_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** Number of recursive locks - 0 if not owned by anyone, > 0 if owned. */
57 uint32_t volatile cRecursion;
58 /** The wait queue. */
59 wait_queue_head_t Head;
60 /** The current owner. */
61 void * volatile pOwner;
62} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
63
64
65
66RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
67{
68 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
69 if (pMutexInt)
70 {
71 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
72 init_waitqueue_head(&pMutexInt->Head);
73 *pMutexSem = pMutexInt;
74AssertReleaseMsgFailed(("This mutex implementation is buggy, fix it!\n"));
75 return VINF_SUCCESS;
76 }
77 return VERR_NO_MEMORY;
78}
79
80
81RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
82{
83 /*
84 * Validate input.
85 */
86 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
87 if (!pMutexInt)
88 return VERR_INVALID_PARAMETER;
89 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
90 {
91 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
92 return VERR_INVALID_PARAMETER;
93 }
94
95 /*
96 * Invalidate it and signal the object just in case.
97 */
98 ASMAtomicIncU32(&pMutexInt->u32Magic);
99 ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
100 Assert(!waitqueue_active(&pMutexInt->Head));
101 wake_up_all(&pMutexInt->Head);
102 RTMemFree(pMutexInt);
103 return VINF_SUCCESS;
104}
105
106
107RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
108{
109 /*
110 * Validate input.
111 */
112 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
113 if (!pMutexInt)
114 return VERR_INVALID_PARAMETER;
115 if ( !pMutexInt
116 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
117 {
118 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
119 return VERR_INVALID_PARAMETER;
120 }
121
122 /*
123 * Check for recursive request.
124 */
125 if (pMutexInt->pOwner == current)
126 {
127 Assert(pMutexInt->cRecursion < 1000);
128 ASMAtomicIncU32(&pMutexInt->cRecursion);
129 return VINF_SUCCESS;
130 }
131
132 /*
133 * Try aquire it.
134 */
135 if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
136 {
137 ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
138 return VINF_SUCCESS;
139 }
140 else
141 {
142 /*
143 * Ok wait for it.
144 */
145 DEFINE_WAIT(Wait);
146 int rc = VINF_SUCCESS;
147 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
148 for (;;)
149 {
150 /* make everything thru schedule() atomic scheduling wise. */
151 prepare_to_wait(&pMutexInt->Head, &Wait, TASK_INTERRUPTIBLE);
152
153 /* check the condition. */
154 if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
155 {
156 ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
157 break;
158 }
159
160 /* check for pending signals. */
161 if (signal_pending(current))
162 {
163 rc = VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
164 break;
165 }
166
167 /* wait */
168 lTimeout = schedule_timeout(lTimeout);
169
170 /* Check if someone destroyed the semaphore while we was waiting. */
171 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
172 {
173 rc = VERR_SEM_DESTROYED;
174 break;
175 }
176
177 /* check for timeout. */
178 if (!lTimeout)
179 {
180 rc = VERR_TIMEOUT;
181 break;
182 }
183 }
184 finish_wait(&pMutexInt->Head, &Wait);
185 return rc;
186 }
187 return VINF_SUCCESS;
188}
189
190
191RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
192{
193 /*
194 * Validate input.
195 */
196 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
197 if (!pMutexInt)
198 return VERR_INVALID_PARAMETER;
199 if ( !pMutexInt
200 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
201 {
202 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
203 return VERR_INVALID_PARAMETER;
204 }
205 if (pMutexInt->pOwner != current)
206 {
207 AssertMsgFailed(("Not owner, pOwner=%p current=%p\n", (void *)pMutexInt->pOwner, (void *)current));
208 return VERR_NOT_OWNER;
209 }
210
211 /*
212 * Release the mutex.
213 */
214 if (pMutexInt->cRecursion == 1)
215 {
216 ASMAtomicXchgPtr(&pMutexInt->pOwner, NULL);
217 ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
218 }
219 else
220 ASMAtomicDecU32(&pMutexInt->cRecursion);
221
222 return VINF_SUCCESS;
223}
224
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