VirtualBox

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

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

Use RTSemMutexRequest*Debug in strict builds.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/* $Id: semmutex-r0drv-linux.c 25378 2009-12-14 19:30:31Z 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.215389.xyz. 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#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/* Undefine debug mappings. */
66#undef RTSemMutexRequest
67#undef RTSemMutexRequestNoResume
68
69
70RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
71{
72 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
73 if (pMutexInt)
74 {
75 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
76 init_waitqueue_head(&pMutexInt->Head);
77 *pMutexSem = pMutexInt;
78AssertReleaseMsgFailed(("This mutex implementation is buggy, fix it!\n"));
79 return VINF_SUCCESS;
80 }
81 return VERR_NO_MEMORY;
82}
83RT_EXPORT_SYMBOL(RTSemMutexCreate);
84
85
86RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
87{
88 /*
89 * Validate input.
90 */
91 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
92 if (!pMutexInt)
93 return VERR_INVALID_PARAMETER;
94 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
95 {
96 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
97 return VERR_INVALID_PARAMETER;
98 }
99
100 /*
101 * Invalidate it and signal the object just in case.
102 */
103 ASMAtomicIncU32(&pMutexInt->u32Magic);
104 ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
105 Assert(!waitqueue_active(&pMutexInt->Head));
106 wake_up_all(&pMutexInt->Head);
107 RTMemFree(pMutexInt);
108 return VINF_SUCCESS;
109}
110RT_EXPORT_SYMBOL(RTSemMutexDestroy);
111
112
113RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
114{
115 /*
116 * Validate input.
117 */
118 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
119 if (!pMutexInt)
120 return VERR_INVALID_PARAMETER;
121 if ( !pMutexInt
122 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
123 {
124 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
125 return VERR_INVALID_PARAMETER;
126 }
127
128 /*
129 * Check for recursive request.
130 */
131 if (pMutexInt->pOwner == current)
132 {
133 Assert(pMutexInt->cRecursion < 1000);
134 ASMAtomicIncU32(&pMutexInt->cRecursion);
135 return VINF_SUCCESS;
136 }
137
138 /*
139 * Try aquire it.
140 */
141 if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
142 {
143 ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
144 return VINF_SUCCESS;
145 }
146 else
147 {
148 /*
149 * Ok wait for it.
150 */
151 DEFINE_WAIT(Wait);
152 int rc = VINF_SUCCESS;
153 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
154 for (;;)
155 {
156 /* make everything thru schedule() atomic scheduling wise. */
157 prepare_to_wait(&pMutexInt->Head, &Wait, TASK_INTERRUPTIBLE);
158
159 /* check the condition. */
160 if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
161 {
162 ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
163 break;
164 }
165
166 /* check for pending signals. */
167 if (signal_pending(current))
168 {
169 rc = VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
170 break;
171 }
172
173 /* wait */
174 lTimeout = schedule_timeout(lTimeout);
175
176 after_wait(&Wait);
177
178 /* Check if someone destroyed the semaphore while we was waiting. */
179 if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
180 {
181 rc = VERR_SEM_DESTROYED;
182 break;
183 }
184
185 /* check for timeout. */
186 if (!lTimeout)
187 {
188 rc = VERR_TIMEOUT;
189 break;
190 }
191 }
192 finish_wait(&pMutexInt->Head, &Wait);
193 return rc;
194 }
195 return VINF_SUCCESS;
196}
197RT_EXPORT_SYMBOL(RTSemMutexRequest);
198
199
200RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
201{
202 /*
203 * Validate input.
204 */
205 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
206 if (!pMutexInt)
207 return VERR_INVALID_PARAMETER;
208 if ( !pMutexInt
209 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
210 {
211 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
212 return VERR_INVALID_PARAMETER;
213 }
214 if (pMutexInt->pOwner != current)
215 {
216 AssertMsgFailed(("Not owner, pOwner=%p current=%p\n", (void *)pMutexInt->pOwner, (void *)current));
217 return VERR_NOT_OWNER;
218 }
219
220 /*
221 * Release the mutex.
222 */
223 if (pMutexInt->cRecursion == 1)
224 {
225 ASMAtomicXchgPtr(&pMutexInt->pOwner, NULL);
226 ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
227 }
228 else
229 ASMAtomicDecU32(&pMutexInt->cRecursion);
230
231 return VINF_SUCCESS;
232}
233RT_EXPORT_SYMBOL(RTSemMutexRelease);
234
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