VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/semmutex-r0drv-haiku.c@ 106212

Last change on this file since 106212 was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: semmutex-r0drv-haiku.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Mutex Semaphores, Ring-0 Driver, Haiku.
4 */
5
6/*
7 * Copyright (C) 2012-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-haiku-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/semaphore.h>
44
45#include <iprt/asm.h>
46#include <iprt/assert.h>
47#include <iprt/err.h>
48#include <iprt/mem.h>
49#include <iprt/thread.h>
50#include <iprt/time.h>
51
52#include "internal/magics.h"
53
54
55/*********************************************************************************************************************************
56* Structures and Typedefs *
57*********************************************************************************************************************************/
58/**
59 * Wrapper for the Haiku (sleep) mutex.
60 */
61/* XXX: not optimal, maybe should use the (private)
62 kernel recursive_lock ? (but it's not waitable) */
63typedef struct RTSEMMUTEXINTERNAL
64{
65 /** Magic value (RTSEMMUTEX_MAGIC). */
66 uint32_t u32Magic;
67 /** Kernel semaphore. */
68 sem_id SemId;
69 /** Current holder */
70 volatile thread_id OwnerId;
71 /** Recursion count */
72 int32 cRecursion;
73} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
74
75
76RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
77{
78 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
79 AssertPtrReturn(phMutexSem, VERR_INVALID_POINTER);
80
81 PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
82 if (RT_UNLIKELY(!pThis))
83 return VERR_NO_MEMORY;
84
85 pThis->u32Magic = RTSEMMUTEX_MAGIC;
86 pThis->SemId = create_sem(0, "IPRT Mutex Semaphore");
87 if (pThis->SemId < B_OK)
88 {
89 pThis->OwnerId = -1;
90 pThis->cRecursion = 0;
91 *phMutexSem = pThis;
92 return VINF_SUCCESS;
93 }
94 RTMemFree(pThis);
95 return VERR_TOO_MANY_SEMAPHORES; /** @todo r=ramshankar: use RTErrConvertFromHaikuKernReturn */
96}
97
98
99RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
100{
101 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
102 if (pThis == NIL_RTSEMMUTEX)
103 return VINF_SUCCESS;
104 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
105 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
106
107 AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
108
109 delete_sem(pThis->SemId);
110 RTMemFree(pThis);
111
112 return VINF_SUCCESS;
113}
114
115
116/**
117 * Worker function for acquiring the mutex.
118 *
119 * @param hMutexSem The mutex object.
120 * @param fFlags Mutex flags (see RTSEMWAIT_FLAGS_*)
121 * @param uTimeout Timeout in units specified by the flags.
122 *
123 * @return IPRT status code.
124 */
125static int rtSemMutexRequestEx(RTSEMMUTEX hMutexSem, uint32_t fFlags, uint64_t uTimeout)
126{
127 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
128 int rc;
129 status_t status;
130 int32 flags = 0;
131 bigtime_t timeout; /* in microseconds */
132 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
133 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
134
135 if (pThis->OwnerId == find_thread(NULL))
136 {
137 pThis->OwnerId++;
138 return VINF_SUCCESS;
139 }
140
141 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
142 timeout = B_INFINITE_TIMEOUT;
143 else
144 {
145 if (fFlags & RTSEMWAIT_FLAGS_NANOSECS)
146 timeout = uTimeout / 1000;
147 else if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
148 timeout = uTimeout * 1000;
149 else
150 return VERR_INVALID_PARAMETER;
151
152 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
153 flags |= B_RELATIVE_TIMEOUT;
154 else if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
155 flags |= B_ABSOLUTE_TIMEOUT;
156 else
157 return VERR_INVALID_PARAMETER;
158 }
159
160 if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
161 flags |= B_CAN_INTERRUPT;
162
163 status = acquire_sem_etc(pThis->SemId, 1, flags, timeout);
164
165 switch (status)
166 {
167 case B_OK:
168 rc = VINF_SUCCESS;
169 pThis->cRecursion = 1;
170 pThis->OwnerId = find_thread(NULL);
171 break;
172 case B_BAD_SEM_ID:
173 rc = VERR_SEM_DESTROYED;
174 break;
175 case B_INTERRUPTED:
176 rc = VERR_INTERRUPTED;
177 break;
178 case B_WOULD_BLOCK:
179 /* fallthrough? */
180 case B_TIMED_OUT:
181 rc = VERR_TIMEOUT;
182 break;
183 default:
184 rc = VERR_INVALID_PARAMETER;
185 break;
186 }
187
188 return rc;
189}
190
191
192RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
193{
194 return rtSemMutexRequestEx(hMutexSem, RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS, cMillies);
195}
196
197
198RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
199{
200 return RTSemMutexRequest(hMutexSem, cMillies);
201}
202
203
204RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
205{
206 return rtSemMutexRequestEx(hMutexSem, RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NORESUME | RTSEMWAIT_FLAGS_MILLISECS, cMillies);
207}
208
209
210RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
211{
212 return RTSemMutexRequestNoResume(hMutexSem, cMillies);
213}
214
215
216RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
217{
218 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
219 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
220 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
221
222 if (pThis->OwnerId != find_thread(NULL))
223 return VERR_INVALID_HANDLE;
224
225 if (--pThis->cRecursion == 0)
226 {
227 pThis->OwnerId == -1;
228 release_sem(pThis->SemId);
229 }
230
231 return VINF_SUCCESS;
232}
233
234
235RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
236{
237 PRTSEMMUTEXINTERNAL pThis = hMutexSem;
238 AssertPtrReturn(pThis, false);
239 AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
240
241 return pThis->OwnerId != -1;
242}
243
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