1 | /* $Id: semfastmutex-r0drv-haiku.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Haiku.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 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 |
|
---|
43 | #include <iprt/semaphore.h>
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/alloc.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/asm.h>
|
---|
48 |
|
---|
49 | #include "internal/magics.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Structures and Typedefs *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /**
|
---|
56 | * Wrapper for the Haiku (sleep) mutex.
|
---|
57 | */
|
---|
58 | typedef struct RTSEMFASTMUTEXINTERNAL
|
---|
59 | {
|
---|
60 | /** Magic value (RTSEMFASTMUTEX_MAGIC). */
|
---|
61 | uint32_t u32Magic;
|
---|
62 | /** A good old Benaphore. */
|
---|
63 | vint32 BenId;
|
---|
64 | sem_id SemId;
|
---|
65 | } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
|
---|
66 |
|
---|
67 |
|
---|
68 | RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
|
---|
69 | {
|
---|
70 | AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
|
---|
71 | AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
|
---|
72 |
|
---|
73 | PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
74 | if (RT_UNLIKELY(!pThis))
|
---|
75 | return VERR_NO_MEMORY;
|
---|
76 |
|
---|
77 | pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
|
---|
78 | pThis->BenId = 0;
|
---|
79 | pThis->SemId = create_sem(0, "IPRT Fast Mutex Semaphore");
|
---|
80 | if (pThis->SemId >= B_OK)
|
---|
81 | {
|
---|
82 | *phFastMtx = pThis;
|
---|
83 | return VINF_SUCCESS;
|
---|
84 | }
|
---|
85 | RTMemFree(pThis);
|
---|
86 | return VERR_TOO_MANY_SEMAPHORES; /** @todo r=ramshankar: use RTErrConvertFromHaikuKernReturn */
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
|
---|
91 | {
|
---|
92 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
93 | if (pThis == NIL_RTSEMFASTMUTEX)
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
96 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
97 |
|
---|
98 | ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
|
---|
99 | delete_sem(pThis->SemId);
|
---|
100 | RTMemFree(pThis);
|
---|
101 |
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
|
---|
107 | {
|
---|
108 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
109 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
110 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
111 |
|
---|
112 | if (atomic_add(&pThis->BenId, 1) > 0)
|
---|
113 | acquire_sem(pThis->SemId);
|
---|
114 |
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
|
---|
120 | {
|
---|
121 | PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
|
---|
122 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
123 | AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
124 |
|
---|
125 | if (atomic_add(&pThis->BenId, -1) > 1)
|
---|
126 | release_sem(pThis->SemId);
|
---|
127 |
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 |
|
---|