VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/semevent-r0drv-haiku.c

Last change on this file was 98103, checked in by vboxsync, 17 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: 8.7 KB
Line 
1/* $Id: semevent-r0drv-haiku.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Single 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/asm.h>
46#include <iprt/assert.h>
47#include <iprt/err.h>
48#include <iprt/lockvalidator.h>
49#include <iprt/mem.h>
50
51#include "internal/magics.h"
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57/**
58 * Haiku event semaphore.
59 */
60typedef struct RTSEMEVENTINTERNAL
61{
62 /** Magic value (RTSEMEVENT_MAGIC). */
63 uint32_t volatile u32Magic;
64 /** Reference counter. */
65 uint32_t volatile cRefs;
66 /** The semaphore Id. */
67 sem_id SemId;
68} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
69
70
71RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
72{
73 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
74}
75
76
77RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
78{
79 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
80 AssertReturn(!(fFlags & ~(RTSEMEVENT_FLAGS_NO_LOCK_VAL | RTSEMEVENT_FLAGS_BOOTSTRAP_HACK)), VERR_INVALID_PARAMETER);
81 Assert(!(fFlags & RTSEMEVENT_FLAGS_BOOTSTRAP_HACK) || (fFlags & RTSEMEVENT_FLAGS_NO_LOCK_VAL));
82 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
83
84 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAllocZ(sizeof(*pThis));
85 if (!pThis)
86 return VERR_NO_MEMORY;
87
88 pThis->u32Magic = RTSEMEVENT_MAGIC;
89 pThis->cRefs = 1;
90 pThis->SemId = create_sem(0, "IPRT Semaphore Event");
91 if (pThis->SemId >= B_OK)
92 {
93 set_sem_owner(pThis->SemId, B_SYSTEM_TEAM);
94 *phEventSem = pThis;
95 return VINF_SUCCESS;
96 }
97
98 RTMemFree(pThis);
99 return VERR_TOO_MANY_SEMAPHORES; /** @todo r=ramshankar: use RTErrConvertFromHaikuKernReturn */
100}
101
102
103/**
104 * Retains a reference to the event semaphore.
105 *
106 * @param pThis The event semaphore.
107 */
108DECLINLINE(void) rtR0SemEventHkuRetain(PRTSEMEVENTINTERNAL pThis)
109{
110 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
111 Assert(cRefs < 100000); NOREF(cRefs);
112}
113
114
115/**
116 * Releases a reference to the event semaphore.
117 *
118 * @param pThis The event semaphore.
119 */
120DECLINLINE(void) rtR0SemEventHkuRelease(PRTSEMEVENTINTERNAL pThis)
121{
122 if (RT_UNLIKELY(ASMAtomicDecU32(&pThis->cRefs) == 0))
123 RTMemFree(pThis);
124}
125
126
127RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
128{
129 /*
130 * Validate input.
131 */
132 PRTSEMEVENTINTERNAL pThis = hEventSem;
133 if (pThis == NIL_RTSEMEVENT)
134 return VINF_SUCCESS;
135 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
136 Assert(pThis->cRefs > 0);
137
138 /*
139 * Invalidate it and delete the semaphore to unblock everyone.
140 */
141 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
142 delete_sem(pThis->SemId);
143 pThis->SemId = -1;
144 rtR0SemEventHkuRelease(pThis);
145 return VINF_SUCCESS;
146}
147
148
149RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
150{
151 /*
152 * Validate input.
153 */
154 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
155 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
156 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
157 rtR0SemEventHkuRetain(pThis);
158
159 /*
160 * Signal the event object.
161 * We must use B_DO_NOT_RESCHEDULE since we are being used from an irq handler.
162 */
163 release_sem_etc(pThis->SemId, 1, B_DO_NOT_RESCHEDULE);
164 rtR0SemEventHkuRelease(pThis);
165 return VINF_SUCCESS;
166}
167
168
169/**
170 * Worker for RTSemEventWaitEx and RTSemEventWaitExDebug.
171 *
172 * @returns VBox status code.
173 * @param pThis The event semaphore.
174 * @param fFlags See RTSemEventWaitEx.
175 * @param uTimeout See RTSemEventWaitEx.
176 * @param pSrcPos The source code position of the wait.
177 */
178static int rtR0SemEventWait(PRTSEMEVENTINTERNAL pThis, uint32_t fFlags, uint64_t uTimeout,
179 PCRTLOCKVALSRCPOS pSrcPos)
180{
181 status_t status;
182 int rc;
183 int32 flags = 0;
184 bigtime_t timeout; /* in microseconds */
185
186 /*
187 * Validate the input.
188 */
189 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
190 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
191 AssertReturn(RTSEMWAIT_FLAGS_ARE_VALID(fFlags), VERR_INVALID_PARAMETER);
192
193 if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
194 timeout = B_INFINITE_TIMEOUT;
195 else
196 {
197 if (fFlags & RTSEMWAIT_FLAGS_NANOSECS)
198 timeout = uTimeout / 1000;
199 else if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
200 timeout = uTimeout * 1000;
201 else
202 return VERR_INVALID_PARAMETER;
203
204 if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
205 flags |= B_RELATIVE_TIMEOUT;
206 else if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
207 flags |= B_ABSOLUTE_TIMEOUT;
208 else
209 return VERR_INVALID_PARAMETER;
210 }
211
212 if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
213 flags |= B_CAN_INTERRUPT;
214 // likely not:
215 //else
216 // flags |= B_KILL_CAN_INTERRUPT;
217
218 rtR0SemEventHkuRetain(pThis);
219
220 status = acquire_sem_etc(pThis->SemId, 1, flags, timeout);
221
222 switch (status)
223 {
224 case B_OK:
225 rc = VINF_SUCCESS;
226 break;
227 case B_BAD_SEM_ID:
228 rc = VERR_SEM_DESTROYED;
229 break;
230 case B_INTERRUPTED:
231 rc = VERR_INTERRUPTED;
232 break;
233 case B_WOULD_BLOCK:
234 /* fallthrough ? */
235 case B_TIMED_OUT:
236 rc = VERR_TIMEOUT;
237 break;
238 default:
239 rc = RTErrConvertFromHaikuKernReturn(status);
240 break;
241 }
242
243 rtR0SemEventHkuRelease(pThis);
244 return rc;
245}
246
247
248RTDECL(int) RTSemEventWaitEx(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout)
249{
250#ifndef RTSEMEVENT_STRICT
251 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, NULL);
252#else
253 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_NORMAL_API();
254 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
255#endif
256}
257RT_EXPORT_SYMBOL(RTSemEventWaitEx);
258
259
260RTDECL(int) RTSemEventWaitExDebug(RTSEMEVENT hEventSem, uint32_t fFlags, uint64_t uTimeout,
261 RTHCUINTPTR uId, RT_SRC_POS_DECL)
262{
263 RTLOCKVALSRCPOS SrcPos = RTLOCKVALSRCPOS_INIT_DEBUG_API();
264 return rtR0SemEventWait(hEventSem, fFlags, uTimeout, &SrcPos);
265}
266RT_EXPORT_SYMBOL(RTSemEventWaitExDebug);
267
268
269RTDECL(uint32_t) RTSemEventGetResolution(void)
270{
271 /* At least that's what the API supports. */
272 return 1000;
273}
274
275
276RTR0DECL(bool) RTSemEventIsSignalSafe(void)
277{
278 /** @todo check the code... */
279 return false;
280}
281RT_EXPORT_SYMBOL(RTSemEventIsSignalSafe);
282
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use