VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/netbsd/semmutex-r0drv-netbsd.c@ 78381

Last change on this file since 78381 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • 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-netbsd.c 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, NetBSD.
4 */
5
6/*
7 * Copyright (C) 2010-2019 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#define RTSEMMUTEX_WITHOUT_REMAPPING
32#include "the-netbsd-kernel.h"
33#include "internal/iprt.h"
34#include <iprt/semaphore.h>
35
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/errcore.h>
39#include <iprt/mem.h>
40#include <iprt/thread.h>
41#include <iprt/time.h>
42
43#include "internal/magics.h"
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49/**
50 * Wrapper for the NetBSD (sleep) mutex.
51 */
52typedef struct RTSEMMUTEXINTERNAL
53{
54 /** Magic value (RTSEMMUTEX_MAGIC). */
55 uint32_t u32Magic;
56 /** The NetBSD shared/exclusive lock mutex. */
57 struct sx SxLock;
58} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
59
60
61RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
62{
63 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
64 AssertPtrReturn(phMutexSem, VERR_INVALID_POINTER);
65
66 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
67 if (pThis)
68 {
69 pThis->u32Magic = RTSEMMUTEX_MAGIC;
70 sx_init_flags(&pThis->SxLock, "IPRT Mutex Semaphore", SX_RECURSE);
71
72 *phMutexSem = pThis;
73 return VINF_SUCCESS;
74 }
75 return VERR_NO_MEMORY;
76}
77
78
79RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
80{
81 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
82 if (pThis == NIL_RTSEMMUTEX)
83 return VINF_SUCCESS;
84 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
85 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
86
87 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
88
89 sx_destroy(&pThis->SxLock);
90 RTMemFree(pThis);
91
92 return VINF_SUCCESS;
93}
94
95
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 rc;
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
146RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
147{
148 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
149 int rc;
150 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
151 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
152
153 if (cMillies == RT_INDEFINITE_WAIT)
154 {
155 if (!sx_xlock_sig(&pThis->SxLock))
156 rc = VINF_SUCCESS;
157 else
158 rc = VERR_INTERRUPTED;
159 }
160 else if (!cMillies)
161 {
162 if (sx_try_xlock(&pThis->SxLock))
163 rc = VINF_SUCCESS;
164 else
165 rc = VERR_TIMEOUT;
166 }
167 /*
168 * GROSS HACK: poll implementation of timeout.
169 */
170 /** @todo Implement timeouts and interrupt checks in
171 * RTSemMutexRequestNoResume. */
172 else if (sx_try_xlock(&pThis->SxLock))
173 rc = VINF_SUCCESS;
174 else
175 {
176 uint64_t StartTS = RTTimeSystemMilliTS();
177 rc = VERR_TIMEOUT;
178 do
179 {
180 RTThreadSleep(1);
181 if (sx_try_xlock(&pThis->SxLock))
182 {
183 rc = VINF_SUCCESS;
184 break;
185 }
186 } while (RTTimeSystemMilliTS() - StartTS < cMillies);
187 }
188
189 return rc;
190}
191
192
193RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
194{
195 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
196}
197
198
199RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
200{
201 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
202 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
203 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
204
205 sx_xunlock(&pThis->SxLock);
206 return VINF_SUCCESS;
207}
208
209
210
211RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
212{
213 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
214 AssertPtrReturn(pThis, false);
215 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
216
217 return sx_xlocked(&pThis->SxLock);
218}
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