1 | /* $Id: semmutex-r0drv-haiku.c 43366 2012-09-20 12:31:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Mutex Semaphores, Ring-0 Driver, Haiku.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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/asm.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 |
|
---|
42 | #include "internal/magics.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * Wrapper for the Haiku (sleep) mutex.
|
---|
50 | */
|
---|
51 | /* XXX: not optimal, maybe should use the (private)
|
---|
52 | kernel recursive_lock ? (but it's not waitable) */
|
---|
53 | typedef struct RTSEMMUTEXINTERNAL
|
---|
54 | {
|
---|
55 | /** Magic value (RTSEMMUTEX_MAGIC). */
|
---|
56 | uint32_t u32Magic;
|
---|
57 | /** Kernel semaphore. */
|
---|
58 | sem_id SemId;
|
---|
59 | /** Current holder */
|
---|
60 | volatile thread_id OwnerId;
|
---|
61 | /** Recursion count */
|
---|
62 | int32 cRecursion;
|
---|
63 | } RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
|
---|
67 | {
|
---|
68 | AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
|
---|
69 | AssertPtrReturn(phMutexSem, VERR_INVALID_POINTER);
|
---|
70 |
|
---|
71 | PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAllocZ(sizeof(*pThis));
|
---|
72 | if (RT_UNLIKELY(!pThis))
|
---|
73 | return VERR_NO_MEMORY;
|
---|
74 |
|
---|
75 | pThis->u32Magic = RTSEMMUTEX_MAGIC;
|
---|
76 | pThis->SemId = create_sem(0, "IPRT Mutex Semaphore");
|
---|
77 | if (pThis->SemId < B_OK)
|
---|
78 | {
|
---|
79 | pThis->OwnerId = -1;
|
---|
80 | pThis->cRecursion = 0;
|
---|
81 | *phMutexSem = pThis;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 | RTMemFree(pThis);
|
---|
85 | return VERR_TOO_MANY_SEMAPHORES; /** @todo r=ramshankar: use RTErrConvertFromHaikuKernReturn */
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX hMutexSem)
|
---|
90 | {
|
---|
91 | PRTSEMMUTEXINTERNAL pThis = hMutexSem;
|
---|
92 | if (pThis == NIL_RTSEMMUTEX)
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
95 | AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
96 |
|
---|
97 | AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
|
---|
98 |
|
---|
99 | delete_sem(pThis->SemId);
|
---|
100 | RTMemFree(pThis);
|
---|
101 |
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /** @todo doxygen */
|
---|
107 | static int rtSemMutexRequestEx(RTSEMMUTEX hMutexSem, uint32_t fFlags, uint64_t uTimeout)
|
---|
108 | {
|
---|
109 | PRTSEMMUTEXINTERNAL pThis = hMutexSem;
|
---|
110 | int rc;
|
---|
111 | status_t status;
|
---|
112 | int32 flags = 0;
|
---|
113 | bigtime_t timeout; /* in microseconds */
|
---|
114 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
115 | AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
116 |
|
---|
117 | if (pThis->OwnerId == find_thread(NULL))
|
---|
118 | {
|
---|
119 | pThis->OwnerId++;
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (fFlags & RTSEMWAIT_FLAGS_INDEFINITE)
|
---|
124 | timeout = B_INFINITE_TIMEOUT;
|
---|
125 | else
|
---|
126 | {
|
---|
127 | if (fFlags & RTSEMWAIT_FLAGS_NANOSECS)
|
---|
128 | timeout = uTimeout / 1000;
|
---|
129 | else if (fFlags & RTSEMWAIT_FLAGS_MILLISECS)
|
---|
130 | timeout = uTimeout * 1000;
|
---|
131 | else
|
---|
132 | return VERR_INVALID_PARAMETER;
|
---|
133 |
|
---|
134 | if (fFlags & RTSEMWAIT_FLAGS_RELATIVE)
|
---|
135 | flags |= B_RELATIVE_TIMEOUT;
|
---|
136 | else if (fFlags & RTSEMWAIT_FLAGS_ABSOLUTE)
|
---|
137 | flags |= B_ABSOLUTE_TIMEOUT;
|
---|
138 | else
|
---|
139 | return VERR_INVALID_PARAMETER;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (fFlags & RTSEMWAIT_FLAGS_INTERRUPTIBLE)
|
---|
143 | flags |= B_CAN_INTERRUPT;
|
---|
144 |
|
---|
145 | status = acquire_sem_etc(pThis->SemId, 1, flags, timeout);
|
---|
146 |
|
---|
147 | switch (status)
|
---|
148 | {
|
---|
149 | case B_OK:
|
---|
150 | rc = VINF_SUCCESS;
|
---|
151 | pThis->cRecursion = 1;
|
---|
152 | pThis->OwnerId = find_thread(NULL);
|
---|
153 | break;
|
---|
154 | case B_BAD_SEM_ID:
|
---|
155 | rc = VERR_SEM_DESTROYED;
|
---|
156 | break;
|
---|
157 | case B_INTERRUPTED:
|
---|
158 | rc = VERR_INTERRUPTED;
|
---|
159 | break;
|
---|
160 | case B_WOULD_BLOCK:
|
---|
161 | /* fallthrough? */
|
---|
162 | case B_TIMED_OUT:
|
---|
163 | rc = VERR_TIMEOUT;
|
---|
164 | break;
|
---|
165 | default:
|
---|
166 | rc = VERR_INVALID_PARAMETER;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 |
|
---|
170 | return rc;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(int) RTSemMutexRequest(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
175 | {
|
---|
176 | return rtSemMutexRequestEx(hMutexSem, RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_RESUME | RTSEMWAIT_FLAGS_MILLISECS, cMillies);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | RTDECL(int) RTSemMutexRequestDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
181 | {
|
---|
182 | return RTSemMutexRequest(hMutexSem, cMillies);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies)
|
---|
187 | {
|
---|
188 | return rtSemMutexRequestEx(hMutexSem, RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NORESUME | RTSEMWAIT_FLAGS_MILLISECS, cMillies);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | RTDECL(int) RTSemMutexRequestNoResumeDebug(RTSEMMUTEX hMutexSem, RTMSINTERVAL cMillies, RTHCUINTPTR uId, RT_SRC_POS_DECL)
|
---|
193 | {
|
---|
194 | return RTSemMutexRequestNoResume(hMutexSem, cMillies);
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | RTDECL(int) RTSemMutexRelease(RTSEMMUTEX hMutexSem)
|
---|
199 | {
|
---|
200 | PRTSEMMUTEXINTERNAL pThis = hMutexSem;
|
---|
201 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
202 | AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
|
---|
203 |
|
---|
204 | if (pThis->OwnerId != find_thread(NULL))
|
---|
205 | return VERR_INVALID_HANDLE;
|
---|
206 |
|
---|
207 | if (--pThis->cRecursion == 0)
|
---|
208 | {
|
---|
209 | pThis->OwnerId == -1;
|
---|
210 | release_sem(pThis->SemId);
|
---|
211 | }
|
---|
212 |
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutexSem)
|
---|
218 | {
|
---|
219 | PRTSEMMUTEXINTERNAL pThis = hMutexSem;
|
---|
220 | AssertPtrReturn(pThis, false);
|
---|
221 | AssertMsgReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), false);
|
---|
222 |
|
---|
223 | return pThis->OwnerId != -1;
|
---|
224 | }
|
---|
225 |
|
---|