VirtualBox

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

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

semmutex-r0drv-freebsd.c: just remembered seeing SX_LOCK_RECURSED.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/* $Id: semmutex-r0drv-freebsd.c 28479 2010-04-19 16:12:57Z 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_LOCK_RECURSED);
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
99RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
100{
101 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
102 int rc;
103 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
104 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
105
106 if (cMillies == RT_INDEFINITE_WAIT)
107 {
108 sx_xlock(&pThis->SxLock);
109 rc = VINF_SUCCESS;
110 }
111 else if (!cMillies)
112 {
113 if (sx_try_xlock(&pThis->SxLock))
114 rc = VINF_SUCCESS;
115 else
116 rc = VERR_TIMEOUT;
117 }
118 /*
119 * GROSS HACK: poll implementation of timeout.
120 */
121 /** @todo Implement timeouts in RTSemMutexRequest. */
122 else if (sx_try_xlock(&pThis->SxLock))
123 rc = VINF_SUCCESS;
124 else
125 {
126 uint64_t StartTS = RTTimeSystemMilliTS();
127 rc = VERR_TIMEOUT;
128 do
129 {
130 RTThreadSleep(1);
131 if (sx_try_xlock(&pThis->SxLock))
132 {
133 rc = VINF_SUCCESS;
134 break;
135 }
136 } while (RTTimeSystemMilliTS() - StartTS < cMillies);
137 }
138
139 return VINF_SUCCESS;
140}
141
142
143RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
144{
145 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
146 int rc;
147 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
148 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
149
150 if (cMillies == RT_INDEFINITE_WAIT)
151 {
152 if (!sx_xlock_sig(&pThis->SxLock))
153 rc = VINF_SUCCESS;
154 else
155 rc = VERR_INTERRUPTED;
156 }
157 else if (!cMillies)
158 {
159 if (sx_try_xlock(&pThis->SxLock))
160 rc = VINF_SUCCESS;
161 else
162 rc = VERR_TIMEOUT;
163 }
164 /*
165 * GROSS HACK: poll implementation of timeout.
166 */
167 /** @todo Implement timeouts and interrupt checks in
168 * RTSemMutexRequestNoResume. */
169 else if (sx_try_xlock(&pThis->SxLock))
170 rc = VINF_SUCCESS;
171 else
172 {
173 uint64_t StartTS = RTTimeSystemMilliTS();
174 rc = VERR_TIMEOUT;
175 do
176 {
177 RTThreadSleep(1);
178 if (sx_try_xlock(&pThis->SxLock))
179 {
180 rc = VINF_SUCCESS;
181 break;
182 }
183 } while (RTTimeSystemMilliTS() - StartTS < cMillies);
184 }
185
186 return VINF_SUCCESS;
187}
188
189
190RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
191{
192 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
193 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
194 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
195
196 sx_xunlock(&pThis->SxLock);
197 return VINF_SUCCESS;
198}
199
200
201
202RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
203{
204 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
205 AssertPtrReturn(pThis, false);
206 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
207
208 return sx_xlocked(&pThis->SxLock);
209}
210
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