VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/semeventmulti-r0drv-haiku.c@ 101472

Last change on this file since 101472 was 98103, checked in by vboxsync, 22 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: 9.7 KB
Line 
1/* $Id: semeventmulti-r0drv-haiku.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Multiple Release Event Semaphores, Ring-0 Driver, Haiku.
4 */
5
6/*
7 * Copyright (C) 2012-2023 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/assert.h>
46#include <iprt/asm.h>
47#include <iprt/err.h>
48#include <iprt/mem.h>
49#include <iprt/lockvalidator.h>
50
51#include "internal/magics.h"
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57/**
58 * Haiku multiple release event semaphore.
59 */
60typedef struct RTSEMEVENTMULTIINTERNAL
61{
62 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
63 uint32_t volatile u32Magic;
64 /** Reference counter. */
65 uint32_t volatile cRefs;
66 /** The semaphore Id. */
67 sem_id SemId;
68} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
69
70
71RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
72{
73 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /* fFlags */, NIL_RTLOCKVALCLASS, NULL);
74}
75
76
77RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
78 const char *pszNameFmt, ...)
79{
80 PRTSEMEVENTMULTIINTERNAL pThis;
81
82 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
83 pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
84 if (!pThis)
85 return VERR_NO_MEMORY;
86
87 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
88 pThis->cRefs = 1;
89 pThis->SemId = create_sem(0, "IPRT Semaphore Event Multi");
90 if (pThis->SemId < B_OK)
91 {
92 set_sem_owner(pThis->SemId, B_SYSTEM_TEAM);
93 *phEventMultiSem = pThis;
94 return VINF_SUCCESS;
95 }
96
97 RTMemFree(pThis);
98 return VERR_TOO_MANY_SEMAPHORES; /** @todo r=ramshankar: use RTErrConvertFromHaikuKernReturn */
99}
100
101
102/**
103 * Retain a reference to the semaphore.
104 *
105 * @param pThis The semaphore.
106 */
107DECLINLINE(void) rtR0SemEventMultiHkuRetain(PRTSEMEVENTMULTIINTERNAL pThis)
108{
109 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
110 Assert(cRefs && cRefs < 100000);
111}
112
113
114/**
115 * Release a reference, destroy the thing if necessary.
116 *
117 * @param pThis The semaphore.
118 */
119DECLINLINE(void) rtR0SemEventMultiHkuRelease(PRTSEMEVENTMULTIINTERNAL pThis)
120{
121 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
122 {
123 Assert(pThis->u32Magic != RTSEMEVENTMULTI_MAGIC);
124 RTMemFree(pThis);
125 }
126}
127
128
129RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
130{
131 /*
132 * Validate input.
133 */
134 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
135 if (pThis == NIL_RTSEMEVENTMULTI)
136 return VINF_SUCCESS;
137 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
138 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
139 Assert(pThis->cRefs > 0);
140
141 /*
142 * Invalidate it and signal the object just in case.
143 */
144 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC);
145 delete_sem(pThis->SemId);
146 pThis->SemId = -1;
147 rtR0SemEventMultiHkuRelease(pThis);
148 return VINF_SUCCESS;
149}
150
151
152RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
153{
154 /*
155 * Validate input.
156 */
157 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
158 if (!pThis)
159 return VERR_INVALID_PARAMETER;
160 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
161 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
162 rtR0SemEventMultiHkuRetain(pThis);
163
164 /*
165 * Signal the event object.
166 * We must use B_DO_NOT_RESCHEDULE since we are being used from an irq handler.
167 */
168 release_sem_etc(pThis->SemId, 1, B_RELEASE_ALL | B_DO_NOT_RESCHEDULE);
169 rtR0SemEventMultiHkuRelease(pThis);
170 return VINF_SUCCESS;
171}
172
173
174RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
175{
176 /*
177 * Validate input.
178 */
179 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
180 if (!pThis)
181 return VERR_INVALID_PARAMETER;
182 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
183 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
184 rtR0SemEventMultiHkuRetain(pThis);
185
186 /*
187 * Reset it.
188 */
189 //FIXME: what should I do ???
190 // delete_sem + create_sem ??
191 rtR0SemEventMultiHkuRelease(pThis);
192 return VINF_SUCCESS;
193}
194
195
196/**
197 * Worker for RTSemEventMultiWaitEx and RTSemEventMultiWaitExDebug.
198 *
199 * @returns VBox status code.
200 * @param pThis The event semaphore.
201 * @param fFlags See RTSemEventMultiWaitEx.
202 * @param uTimeout See RTSemEventMultiWaitEx.
203 * @param pSrcPos The source code position of the wait.
204 */
205static int rtR0SemEventMultiHkuWait(PRTSEMEVENTMULTIINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
206 PCRTLOCKVALSRCPOS pSrcPos)
207{
208 status_t status;
209 int rc;
210 int32 flags = 0;
211 bigtime_t timeout; /* in microseconds */
212
213 /*
214 * Validate the input.
215 */
216 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
217 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
218 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
219
220 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
221 timeout = B_INFINITE_TIMEOUT;
222 else
223 {
224 if (fFlags & RTSEMWAIT_FLAGS_NANOSECS)
225 timeout = uTimeout / 1000;
226 else if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
227 timeout = uTimeout * 1000;
228 else
229 return VERR_INVALID_PARAMETER;
230
231 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
232 flags |= B_RELATIVE_TIMEOUT;
233 else if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
234 flags |= B_ABSOLUTE_TIMEOUT;
235 else
236 return VERR_INVALID_PARAMETER;
237 }
238
239 if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
240 flags |= B_CAN_INTERRUPT;
241 // likely not:
242 //else
243 // flags |= B_KILL_CAN_INTERRUPT;
244
245 rtR0SemEventMultiHkuRetain(pThis);
246
247 status = acquire_sem_etc(pThis->SemId, 1, flags, timeout);
248
249 switch (status)
250 {
251 case B_OK:
252 rc = VINF_SUCCESS;
253 break;
254 case B_BAD_SEM_ID:
255 rc = VERR_SEM_DESTROYED;
256 break;
257 case B_INTERRUPTED:
258 rc = VERR_INTERRUPTED;
259 break;
260 case B_WOULD_BLOCK:
261 /* fallthrough? */
262 case B_TIMED_OUT:
263 rc = VERR_TIMEOUT;
264 break;
265 default:
266 rc = RTErrConvertFromHaikuKernReturn(status);
267 break;
268 }
269
270 rtR0SemEventMultiHkuRelease(pThis);
271 return rc;
272}
273
274
275RTDECL(int) RTSemEventMultiWaitEx(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout)
276{
277#ifndef RTSEMEVENT_STRICT
278 return rtR0SemEventMultiHkuWait(hEventMultiSem, fFlags, uTimeout, NULL);
279#else
280 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
281 return rtR0SemEventMultiHkuWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
282#endif
283}
284RT_EXPORT_SYMBOL(RTSemEventMultiWaitEx);
285
286
287RTDECL(int) RTSemEventMultiWaitExDebug(RTSEMEVENTMULTI hEventMultiSem, uint32_t fFlags, uint64_t uTimeout,
288 RTHCUINTPTR uId, RT_SRC_POS_DECL)
289{
290 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
291 return rtR0SemEventMultiHkuWait(hEventMultiSem, fFlags, uTimeout, &SrcPos);
292}
293RT_EXPORT_SYMBOL(RTSemEventMultiWaitExDebug);
294
295
296RTDECL(uint32_t) RTSemEventMultiGetResolution(void)
297{
298 /* At least that's what the API supports. */
299 return 1000;
300}
301RT_EXPORT_SYMBOL(RTSemEventMultiGetResolution);
302
303
304RTR0DECL(bool) RTSemEventMultiIsSignalSafe(void)
305{
306 /** @todo check the code... */
307 return false;
308}
309RT_EXPORT_SYMBOL(RTSemEventMultiIsSignalSafe);
310
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