VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semaphore-r0drv-darwin.cpp@ 25717

最後變更 在這個檔案從25717是 25717,由 vboxsync 提交於 15 年 前

iprt: Added RTSemEventCreateEx and did some cleanups of the RTSemEventDestroy behavior wrt NIL handles.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 23.9 KB
 
1/* $Id: semaphore-r0drv-darwin.cpp 25717 2010-01-11 13:24:09Z vboxsync $ */
2/** @file
3 * IPRT - Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-darwin-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38
39#include <iprt/alloc.h>
40#include <iprt/assert.h>
41#include <iprt/asm.h>
42#include <iprt/err.h>
43#include <iprt/mp.h>
44#include <iprt/thread.h>
45
46#include "internal/magics.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * Darwin event semaphore.
54 */
55typedef struct RTSEMEVENTINTERNAL
56{
57 /** Magic value (RTSEMEVENT_MAGIC). */
58 uint32_t volatile u32Magic;
59 /** The number of waiting threads. */
60 uint32_t volatile cWaiters;
61 /** Set if the event object is signaled. */
62 uint8_t volatile fSignaled;
63 /** The number of threads in the process of waking up. */
64 uint32_t volatile cWaking;
65 /** The spinlock protecting us. */
66 lck_spin_t *pSpinlock;
67} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
68
69
70/**
71 * Darwin multiple release event semaphore.
72 */
73typedef struct RTSEMEVENTMULTIINTERNAL
74{
75 /** Magic value (RTSEMEVENTMULTI_MAGIC). */
76 uint32_t volatile u32Magic;
77 /** The number of waiting threads. */
78 uint32_t volatile cWaiters;
79 /** Set if the event object is signaled. */
80 uint8_t volatile fSignaled;
81 /** The number of threads in the process of waking up. */
82 uint32_t volatile cWaking;
83 /** The spinlock protecting us. */
84 lck_spin_t *pSpinlock;
85} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
86
87
88#if 0 /** @todo */
89/**
90 * Darwin mutex semaphore.
91 */
92typedef struct RTSEMMUTEXINTERNAL
93{
94 /** Magic value (RTSEMMUTEX_MAGIC). */
95 uint32_t volatile u32Magic;
96 /** The mutex. */
97 lck_mtx_t *pMtx;
98} RTSEMMUTEXINTERNAL, *PRTSEMMUTEXINTERNAL;
99
100#endif
101
102
103/**
104 * Wrapper for the darwin semaphore structure.
105 */
106typedef struct RTSEMFASTMUTEXINTERNAL
107{
108 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
109 uint32_t u32Magic;
110 /** The mutex. */
111 lck_mtx_t *pMtx;
112} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
113
114
115
116RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
117{
118 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
119}
120
121
122RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
123{
124 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
125 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
126 AssertPtrReturn(phEventSem, VERR_INVALID_POINTER);
127 RT_ASSERT_PREEMPTIBLE();
128
129 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
130 if (pThis)
131 {
132 pThis->u32Magic = RTSEMEVENT_MAGIC;
133 pThis->cWaiters = 0;
134 pThis->cWaking = 0;
135 pThis->fSignaled = 0;
136 Assert(g_pDarwinLockGroup);
137 pThis->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
138 if (pThis->pSpinlock)
139 {
140 *phEventSem = pThis;
141 return VINF_SUCCESS;
142 }
143
144 pThis->u32Magic = 0;
145 RTMemFree(pThis);
146 }
147 return VERR_NO_MEMORY;
148}
149
150
151RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
152{
153 PRTSEMEVENTINTERNAL pThis = hEventSem;
154 if (pThis == NIL_RTSEMEVENT)
155 return VINF_SUCCESS;
156 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
157 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
158 RT_ASSERT_INTS_ON();
159
160 lck_spin_lock(pThis->pSpinlock);
161 ASMAtomicIncU32(&pThis->u32Magic); /* make the handle invalid */
162 if (pThis->cWaiters > 0)
163 {
164 /* abort waiting thread, last man cleans up. */
165 ASMAtomicXchgU32(&pThis->cWaking, pThis->cWaking + pThis->cWaiters);
166 thread_wakeup_prim((event_t)pThis, FALSE /* all threads */, THREAD_RESTART);
167 lck_spin_unlock(pThis->pSpinlock);
168 }
169 else if (pThis->cWaking)
170 /* the last waking thread is gonna do the cleanup */
171 lck_spin_unlock(pThis->pSpinlock);
172 else
173 {
174 lck_spin_unlock(pThis->pSpinlock);
175 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
176 RTMemFree(pThis);
177 }
178
179 return VINF_SUCCESS;
180}
181
182
183RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
184{
185 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
186 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
187 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
188 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
189 VERR_INVALID_HANDLE);
190 RT_ASSERT_PREEMPT_CPUID_VAR();
191 RT_ASSERT_INTS_ON();
192
193 /** @todo should probably disable interrupts here... update
194 * semspinmutex-r0drv-generic.c when done. */
195 lck_spin_lock(pThis->pSpinlock);
196
197 if (pThis->cWaiters > 0)
198 {
199 ASMAtomicDecU32(&pThis->cWaiters);
200 ASMAtomicIncU32(&pThis->cWaking);
201 thread_wakeup_prim((event_t)pThis, TRUE /* one thread */, THREAD_AWAKENED);
202 /** @todo this isn't safe. a scheduling interrupt on the other cpu while we're in here
203 * could cause the thread to be timed out before we manage to wake it up and the event
204 * ends up in the wrong state. ditto for posix signals.
205 * Update: check the return code; it will return KERN_NOT_WAITING if no one is around. */
206 }
207 else
208 ASMAtomicXchgU8(&pThis->fSignaled, true);
209
210 lck_spin_unlock(pThis->pSpinlock);
211
212 RT_ASSERT_PREEMPT_CPUID();
213 return VINF_SUCCESS;
214}
215
216
217static int rtSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies, wait_interrupt_t fInterruptible)
218{
219 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
220 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
221 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC,
222 ("pThis=%p u32Magic=%#x\n", pThis, pThis->u32Magic),
223 VERR_INVALID_HANDLE);
224 if (cMillies)
225 RT_ASSERT_PREEMPTIBLE();
226
227 lck_spin_lock(pThis->pSpinlock);
228
229 int rc;
230 if (pThis->fSignaled)
231 {
232 Assert(!pThis->cWaiters);
233 ASMAtomicXchgU8(&pThis->fSignaled, false);
234 rc = VINF_SUCCESS;
235 }
236 else if (!cMillies)
237 rc = VERR_TIMEOUT;
238 else
239 {
240 ASMAtomicIncU32(&pThis->cWaiters);
241
242 wait_result_t rcWait;
243 if (cMillies == RT_INDEFINITE_WAIT)
244 rcWait = lck_spin_sleep(pThis->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pThis, fInterruptible);
245 else
246 {
247 uint64_t u64AbsTime;
248 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
249 u64AbsTime += mach_absolute_time();
250
251 rcWait = lck_spin_sleep_deadline(pThis->pSpinlock, LCK_SLEEP_DEFAULT,
252 (event_t)pThis, fInterruptible, u64AbsTime);
253 }
254 switch (rcWait)
255 {
256 case THREAD_AWAKENED:
257 Assert(pThis->cWaking > 0);
258 if ( !ASMAtomicDecU32(&pThis->cWaking)
259 && pThis->u32Magic != RTSEMEVENT_MAGIC)
260 {
261 /* the event was destroyed after we woke up, as the last thread do the cleanup. */
262 lck_spin_unlock(pThis->pSpinlock);
263 Assert(g_pDarwinLockGroup);
264 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
265 RTMemFree(pThis);
266 return VINF_SUCCESS;
267 }
268 rc = VINF_SUCCESS;
269 break;
270
271 case THREAD_TIMED_OUT:
272 Assert(cMillies != RT_INDEFINITE_WAIT);
273 ASMAtomicDecU32(&pThis->cWaiters);
274 rc = VERR_TIMEOUT;
275 break;
276
277 case THREAD_INTERRUPTED:
278 Assert(fInterruptible);
279 ASMAtomicDecU32(&pThis->cWaiters);
280 rc = VERR_INTERRUPTED;
281 break;
282
283 case THREAD_RESTART:
284 /* Last one out does the cleanup. */
285 if (!ASMAtomicDecU32(&pThis->cWaking))
286 {
287 lck_spin_unlock(pThis->pSpinlock);
288 Assert(g_pDarwinLockGroup);
289 lck_spin_destroy(pThis->pSpinlock, g_pDarwinLockGroup);
290 RTMemFree(pThis);
291 return VERR_SEM_DESTROYED;
292 }
293
294 rc = VERR_SEM_DESTROYED;
295 break;
296
297 default:
298 AssertMsgFailed(("rcWait=%d\n", rcWait));
299 rc = VERR_GENERAL_FAILURE;
300 break;
301 }
302 }
303
304 lck_spin_unlock(pThis->pSpinlock);
305 return rc;
306}
307
308
309RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies)
310{
311 return rtSemEventWait(hEventSem, cMillies, THREAD_UNINT);
312}
313
314
315RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, unsigned cMillies)
316{
317 return rtSemEventWait(hEventSem, cMillies, THREAD_ABORTSAFE);
318}
319
320
321
322RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI pEventMultiSem)
323{
324 Assert(sizeof(RTSEMEVENTMULTIINTERNAL) > sizeof(void *));
325 AssertPtrReturn(pEventMultiSem, VERR_INVALID_POINTER);
326 RT_ASSERT_PREEMPTIBLE();
327
328 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pEventMultiInt));
329 if (pEventMultiInt)
330 {
331 pEventMultiInt->u32Magic = RTSEMEVENTMULTI_MAGIC;
332 pEventMultiInt->cWaiters = 0;
333 pEventMultiInt->cWaking = 0;
334 pEventMultiInt->fSignaled = 0;
335 Assert(g_pDarwinLockGroup);
336 pEventMultiInt->pSpinlock = lck_spin_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
337 if (pEventMultiInt->pSpinlock)
338 {
339 *pEventMultiSem = pEventMultiInt;
340 return VINF_SUCCESS;
341 }
342
343 pEventMultiInt->u32Magic = 0;
344 RTMemFree(pEventMultiInt);
345 }
346 return VERR_NO_MEMORY;
347}
348
349
350RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI EventMultiSem)
351{
352 if (EventMultiSem == NIL_RTSEMEVENTMULTI) /* don't bitch */
353 return VERR_INVALID_HANDLE;
354 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
355 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
356 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
357 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
358 VERR_INVALID_HANDLE);
359 RT_ASSERT_INTS_ON();
360
361 lck_spin_lock(pEventMultiInt->pSpinlock);
362 ASMAtomicIncU32(&pEventMultiInt->u32Magic); /* make the handle invalid */
363 if (pEventMultiInt->cWaiters > 0)
364 {
365 /* abort waiting thread, last man cleans up. */
366 ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
367 thread_wakeup_prim((event_t)pEventMultiInt, FALSE /* all threads */, THREAD_RESTART);
368 lck_spin_unlock(pEventMultiInt->pSpinlock);
369 }
370 else if (pEventMultiInt->cWaking)
371 /* the last waking thread is gonna do the cleanup */
372 lck_spin_unlock(pEventMultiInt->pSpinlock);
373 else
374 {
375 lck_spin_unlock(pEventMultiInt->pSpinlock);
376 lck_spin_destroy(pEventMultiInt->pSpinlock, g_pDarwinLockGroup);
377 RTMemFree(pEventMultiInt);
378 }
379
380 return VINF_SUCCESS;
381}
382
383
384RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI EventMultiSem)
385{
386 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
387 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
388 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
389 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
390 VERR_INVALID_HANDLE);
391 RT_ASSERT_PREEMPT_CPUID_VAR();
392 RT_ASSERT_INTS_ON();
393
394 lck_spin_lock(pEventMultiInt->pSpinlock);
395
396 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, true);
397 if (pEventMultiInt->cWaiters > 0)
398 {
399 ASMAtomicXchgU32(&pEventMultiInt->cWaking, pEventMultiInt->cWaking + pEventMultiInt->cWaiters);
400 ASMAtomicXchgU32(&pEventMultiInt->cWaiters, 0);
401 thread_wakeup_prim((event_t)pEventMultiInt, FALSE /* all threads */, THREAD_AWAKENED);
402 }
403
404 lck_spin_unlock(pEventMultiInt->pSpinlock);
405
406 RT_ASSERT_PREEMPT_CPUID();
407 return VINF_SUCCESS;
408}
409
410
411RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI EventMultiSem)
412{
413 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
414 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
415 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
416 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
417 VERR_INVALID_HANDLE);
418 RT_ASSERT_PREEMPT_CPUID_VAR();
419 RT_ASSERT_INTS_ON();
420
421 lck_spin_lock(pEventMultiInt->pSpinlock);
422 ASMAtomicXchgU8(&pEventMultiInt->fSignaled, false);
423 lck_spin_unlock(pEventMultiInt->pSpinlock);
424
425 RT_ASSERT_PREEMPT_CPUID();
426 return VINF_SUCCESS;
427}
428
429
430static int rtSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies, wait_interrupt_t fInterruptible)
431{
432 PRTSEMEVENTMULTIINTERNAL pEventMultiInt = (PRTSEMEVENTMULTIINTERNAL)EventMultiSem;
433 AssertPtrReturn(pEventMultiInt, VERR_INVALID_HANDLE);
434 AssertMsgReturn(pEventMultiInt->u32Magic == RTSEMEVENTMULTI_MAGIC,
435 ("pEventMultiInt=%p u32Magic=%#x\n", pEventMultiInt, pEventMultiInt->u32Magic),
436 VERR_INVALID_HANDLE);
437 if (cMillies)
438 RT_ASSERT_PREEMPTIBLE();
439
440 lck_spin_lock(pEventMultiInt->pSpinlock);
441
442 int rc;
443 if (pEventMultiInt->fSignaled)
444 rc = VINF_SUCCESS;
445 else if (!cMillies)
446 rc = VERR_TIMEOUT;
447 else
448 {
449 ASMAtomicIncU32(&pEventMultiInt->cWaiters);
450
451 wait_result_t rcWait;
452 if (cMillies == RT_INDEFINITE_WAIT)
453 rcWait = lck_spin_sleep(pEventMultiInt->pSpinlock, LCK_SLEEP_DEFAULT, (event_t)pEventMultiInt, fInterruptible);
454 else
455 {
456 uint64_t u64AbsTime;
457 nanoseconds_to_absolutetime(cMillies * UINT64_C(1000000), &u64AbsTime);
458 u64AbsTime += mach_absolute_time();
459
460 rcWait = lck_spin_sleep_deadline(pEventMultiInt->pSpinlock, LCK_SLEEP_DEFAULT,
461 (event_t)pEventMultiInt, fInterruptible, u64AbsTime);
462 }
463 switch (rcWait)
464 {
465 case THREAD_AWAKENED:
466 Assert(pEventMultiInt->cWaking > 0);
467 if ( !ASMAtomicDecU32(&pEventMultiInt->cWaking)
468 && pEventMultiInt->u32Magic != RTSEMEVENTMULTI_MAGIC)
469 {
470 /* the event was destroyed after we woke up, as the last thread do the cleanup. */
471 lck_spin_unlock(pEventMultiInt->pSpinlock);
472 Assert(g_pDarwinLockGroup);
473 lck_spin_destroy(pEventMultiInt->pSpinlock, g_pDarwinLockGroup);
474 RTMemFree(pEventMultiInt);
475 return VINF_SUCCESS;
476 }
477 rc = VINF_SUCCESS;
478 break;
479
480 case THREAD_TIMED_OUT:
481 Assert(cMillies != RT_INDEFINITE_WAIT);
482 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
483 rc = VERR_TIMEOUT;
484 break;
485
486 case THREAD_INTERRUPTED:
487 Assert(fInterruptible);
488 ASMAtomicDecU32(&pEventMultiInt->cWaiters);
489 rc = VERR_INTERRUPTED;
490 break;
491
492 case THREAD_RESTART:
493 /* Last one out does the cleanup. */
494 if (!ASMAtomicDecU32(&pEventMultiInt->cWaking))
495 {
496 lck_spin_unlock(pEventMultiInt->pSpinlock);
497 Assert(g_pDarwinLockGroup);
498 lck_spin_destroy(pEventMultiInt->pSpinlock, g_pDarwinLockGroup);
499 RTMemFree(pEventMultiInt);
500 return VERR_SEM_DESTROYED;
501 }
502
503 rc = VERR_SEM_DESTROYED;
504 break;
505
506 default:
507 AssertMsgFailed(("rcWait=%d\n", rcWait));
508 rc = VERR_GENERAL_FAILURE;
509 break;
510 }
511 }
512
513 lck_spin_unlock(pEventMultiInt->pSpinlock);
514 return rc;
515}
516
517
518RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
519{
520 return rtSemEventMultiWait(EventMultiSem, cMillies, THREAD_UNINT);
521}
522
523
524RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI EventMultiSem, unsigned cMillies)
525{
526 return rtSemEventMultiWait(EventMultiSem, cMillies, THREAD_ABORTSAFE);
527}
528
529
530
531
532
533#if 0 /* need proper timeout lock function! */
534RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
535{
536 RT_ASSERT_PREEMPTIBLE();
537 AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
538 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
539 if (pMutexInt)
540 {
541 pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
542 Assert(g_pDarwinLockGroup);
543 pMutexInt->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
544 if (pMutexInt->pMtx)
545 {
546 *pMutexSem = pMutexInt;
547 return VINF_SUCCESS;
548 }
549 RTMemFree(pMutexInt);
550 }
551 return VERR_NO_MEMORY;
552}
553
554
555RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
556{
557 /*
558 * Validate input.
559 */
560 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
561 if (!pMutexInt)
562 return VERR_INVALID_PARAMETER;
563 AssertPtrReturn(pMutexInt, VERR_INVALID_POINTER);
564 AssertMsg(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC,
565 ("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt)
566 VERR_INVALID_PARAMETER);
567 RT_ASSERT_INTS_ON();
568
569 /*
570 * Invalidate it and signal the object just in case.
571 */
572 ASMAtomicIncU32(&pMutexInt->u32Magic);
573
574 Assert(g_pDarwinLockGroup);
575 lck_mtx_free(pMutexInt->pMtx, g_pDarwinLockGroup);
576 pMutexInt->pMtx = NULL;
577
578 RTMemFree(pMutexInt);
579 return VINF_SUCCESS;
580}
581
582
583RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
584{
585 /*
586 * Validate input.
587 */
588 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
589 if (!pMutexInt)
590 return VERR_INVALID_PARAMETER;
591 AssertPtrReturn(pMutexInt, VERR_INVALID_POINTER);
592 AssertMsg(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC,
593 ("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt)
594 VERR_INVALID_PARAMETER);
595 if (cMillies)
596 RT_ASSERT_PREEMPTIBLE();
597
598 /*
599 * Get the mutex.
600 */
601 wait_result_t rc = lck_mtx_lock_deadlink
602#if 1
603#else
604 NTSTATUS rcNt;
605 if (cMillies == RT_INDEFINITE_WAIT)
606 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, NULL);
607 else
608 {
609 LARGE_INTEGER Timeout;
610 Timeout.QuadPart = -(int64_t)cMillies * 10000;
611 rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, TRUE, &Timeout);
612 }
613 switch (rcNt)
614 {
615 case STATUS_SUCCESS:
616 if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
617 return VINF_SUCCESS;
618 return VERR_SEM_DESTROYED;
619 case STATUS_ALERTED:
620 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
621 case STATUS_USER_APC:
622 return VERR_INTERRUPTED; /** @todo VERR_INTERRUPTED isn't correct anylonger. please fix r0drv stuff! */
623 case STATUS_TIMEOUT:
624 return VERR_TIMEOUT;
625 default:
626 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
627 pMutexInt->u32Magic, pMutexInt, (long)rcNt));
628 return VERR_INTERNAL_ERROR;
629 }
630#endif
631 return VINF_SUCCESS;
632}
633
634
635RTDECL(int) RTSemMutexRelease(RTSEMMUTEX MutexSem)
636{
637 /*
638 * Validate input.
639 */
640 PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
641 if (!pMutexInt)
642 return VERR_INVALID_PARAMETER;
643 if ( !pMutexInt
644 || pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
645 {
646 AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
647 return VERR_INVALID_PARAMETER;
648 }
649 RT_ASSERT_PREEMPTIBLE();
650
651 /*
652 * Release the mutex.
653 */
654#ifdef RT_USE_FAST_MUTEX
655 ExReleaseFastMutex(&pMutexInt->Mutex);
656#else
657 KeReleaseMutex(&pMutexInt->Mutex, FALSE);
658#endif
659 return VINF_SUCCESS;
660}
661
662#endif /* later */
663
664
665
666
667RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
668{
669 AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
670 AssertPtrReturn(pMutexSem, VERR_INVALID_POINTER);
671 RT_ASSERT_PREEMPTIBLE();
672
673 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
674 if (pFastInt)
675 {
676 pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
677 Assert(g_pDarwinLockGroup);
678 pFastInt->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
679 if (pFastInt->pMtx)
680 {
681 *pMutexSem = pFastInt;
682 return VINF_SUCCESS;
683 }
684
685 RTMemFree(pFastInt);
686 }
687 return VERR_NO_MEMORY;
688}
689
690
691RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
692{
693 if (MutexSem == NIL_RTSEMFASTMUTEX) /* don't bitch */
694 return VERR_INVALID_PARAMETER;
695 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
696 AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
697 AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
698 ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
699 VERR_INVALID_PARAMETER);
700 RT_ASSERT_INTS_ON();
701
702 ASMAtomicIncU32(&pFastInt->u32Magic); /* make the handle invalid. */
703 Assert(g_pDarwinLockGroup);
704 lck_mtx_free(pFastInt->pMtx, g_pDarwinLockGroup);
705 pFastInt->pMtx = NULL;
706 RTMemFree(pFastInt);
707
708 return VINF_SUCCESS;
709}
710
711
712RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
713{
714 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
715 AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
716 AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
717 ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
718 VERR_INVALID_PARAMETER);
719 RT_ASSERT_PREEMPTIBLE();
720 lck_mtx_lock(pFastInt->pMtx);
721 return VINF_SUCCESS;
722}
723
724
725RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
726{
727 PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
728 AssertPtrReturn(pFastInt, VERR_INVALID_PARAMETER);
729 AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
730 ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
731 VERR_INVALID_PARAMETER);
732 RT_ASSERT_PREEMPTIBLE();
733 lck_mtx_unlock(pFastInt->pMtx);
734 return VINF_SUCCESS;
735}
736
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette