VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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