VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/semmutex-r0drv-freebsd.c@ 28499

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

Wrong flag

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: semmutex-r0drv-freebsd.c 28499 2010-04-19 21:03:13Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (C) 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-freebsd-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include <iprt/mem.h>
43#include <iprt/thread.h>
44#include <iprt/time.h>
45
46#include "internal/magics.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * Wrapper for the FreeBSD (sleep) mutex.
54 */
55typedef struct RTSEMMUTEXINTERNAL
56{
57 /** Magic value (RTSEMMUTEX_MAGIC). */
58 uint32_t u32Magic;
59 /** The FreeBSD shared/exclusive lock mutex. */
60 struct sx SxLock;
61} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
62
63
64RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
65{
66 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
67 AssertPtrReturn(phMutexSem, VERR_INVALID_POINTER);
68
69 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
70 if (pThis)
71 {
72 pThis->u32Magic = RTSEMMUTEX_MAGIC;
73 sx_init_flags(&pThis->SxLock, "IPRT Mutex Semaphore", SX_RECURSE);
74
75 *phMutexSem = pThis;
76 return VINF_SUCCESS;
77 }
78 return VERR_NO_MEMORY;
79}
80
81
82RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
83{
84 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
85 if (pThis == NIL_RTSEMMUTEX)
86 return VINF_SUCCESS;
87 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
88 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
89
90 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
91
92 sx_destroy(&pThis->SxLock);
93 RTMemFree(pThis);
94
95 return VINF_SUCCESS;
96}
97
98
99#undef RTSemMutexRequest
100RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
101{
102 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
103 int rc;
104 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
105 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
106
107 if (cMillies == RT_INDEFINITE_WAIT)
108 {
109 sx_xlock(&pThis->SxLock);
110 rc = VINF_SUCCESS;
111 }
112 else if (!cMillies)
113 {
114 if (sx_try_xlock(&pThis->SxLock))
115 rc = VINF_SUCCESS;
116 else
117 rc = VERR_TIMEOUT;
118 }
119 /*
120 * GROSS HACK: poll implementation of timeout.
121 */
122 /** @todo Implement timeouts in RTSemMutexRequest. */
123 else if (sx_try_xlock(&pThis->SxLock))
124 rc = VINF_SUCCESS;
125 else
126 {
127 uint64_t StartTS = RTTimeSystemMilliTS();
128 rc = VERR_TIMEOUT;
129 do
130 {
131 RTThreadSleep(1);
132 if (sx_try_xlock(&pThis->SxLock))
133 {
134 rc = VINF_SUCCESS;
135 break;
136 }
137 } while (RTTimeSystemMilliTS() - StartTS < cMillies);
138 }
139
140 return VINF_SUCCESS;
141}
142
143
144RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
145{
146 return RTSemMutexRequest(hMutexSem, cMillies);
147}
148
149
150#undef RTSemMutexRequestNoResume
151RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
152{
153 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
154 int rc;
155 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
156 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
157
158 if (cMillies == RT_INDEFINITE_WAIT)
159 {
160 if (!sx_xlock_sig(&pThis->SxLock))
161 rc = VINF_SUCCESS;
162 else
163 rc = VERR_INTERRUPTED;
164 }
165 else if (!cMillies)
166 {
167 if (sx_try_xlock(&pThis->SxLock))
168 rc = VINF_SUCCESS;
169 else
170 rc = VERR_TIMEOUT;
171 }
172 /*
173 * GROSS HACK: poll implementation of timeout.
174 */
175 /** @todo Implement timeouts and interrupt checks in
176 * RTSemMutexRequestNoResume. */
177 else if (sx_try_xlock(&pThis->SxLock))
178 rc = VINF_SUCCESS;
179 else
180 {
181 uint64_t StartTS = RTTimeSystemMilliTS();
182 rc = VERR_TIMEOUT;
183 do
184 {
185 RTThreadSleep(1);
186 if (sx_try_xlock(&pThis->SxLock))
187 {
188 rc = VINF_SUCCESS;
189 break;
190 }
191 } while (RTTimeSystemMilliTS() - StartTS < cMillies);
192 }
193
194 return VINF_SUCCESS;
195}
196
197
198RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
199{
200 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
201}
202
203
204RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
205{
206 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
207 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
208 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
209
210 sx_xunlock(&pThis->SxLock);
211 return VINF_SUCCESS;
212}
213
214
215
216RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
217{
218 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
219 AssertPtrReturn(pThis, false);
220 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
221
222 return sx_xlocked(&pThis->SxLock);
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