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