VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/semfastmutex-r0drv-darwin.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.0 KB
Line 
1/* $Id: semfastmutex-r0drv-darwin.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-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-darwin-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/semaphore.h>
44
45#include <iprt/assert.h>
46#include <iprt/asm.h>
47#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
48# include <iprt/asm-amd64-x86.h>
49#endif
50#include <iprt/errcore.h>
51#include <iprt/mem.h>
52#include <iprt/mp.h>
53#include <iprt/thread.h>
54
55#include "internal/magics.h"
56
57
58/*********************************************************************************************************************************
59* Structures and Typedefs *
60*********************************************************************************************************************************/
61/**
62 * Wrapper for the darwin semaphore structure.
63 */
64typedef struct RTSEMFASTMUTEXINTERNAL
65{
66 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
67 uint32_t u32Magic;
68 /** The mutex. */
69 lck_mtx_t *pMtx;
70} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
71
72
73
74RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
75{
76 AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
77 AssertPtrReturn(phFastMtx, VERR_INVALID_POINTER);
78 RT_ASSERT_PREEMPTIBLE();
79 IPRT_DARWIN_SAVE_EFL_AC();
80
81 PRTSEMFASTMUTEXINTERNAL pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
82 if (pThis)
83 {
84 pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
85 Assert(g_pDarwinLockGroup);
86 pThis->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
87 if (pThis->pMtx)
88 {
89 *phFastMtx = pThis;
90 IPRT_DARWIN_RESTORE_EFL_AC();
91 return VINF_SUCCESS;
92 }
93
94 RTMemFree(pThis);
95 }
96 IPRT_DARWIN_RESTORE_EFL_AC();
97 return VERR_NO_MEMORY;
98}
99
100
101RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
102{
103 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
104 if (pThis == NIL_RTSEMFASTMUTEX)
105 return VINF_SUCCESS;
106 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
107 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
108 RT_ASSERT_INTS_ON();
109 IPRT_DARWIN_SAVE_EFL_AC();
110
111 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
112 Assert(g_pDarwinLockGroup);
113 lck_mtx_free(pThis->pMtx, g_pDarwinLockGroup);
114 pThis->pMtx = NULL;
115 RTMemFree(pThis);
116
117 IPRT_DARWIN_RESTORE_EFL_AC();
118 return VINF_SUCCESS;
119}
120
121
122RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
123{
124 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
125 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
126 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
127 RT_ASSERT_PREEMPTIBLE();
128 IPRT_DARWIN_SAVE_EFL_AC();
129
130 lck_mtx_lock(pThis->pMtx);
131
132 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
133 return VINF_SUCCESS;
134}
135
136
137RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
138{
139 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
140 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
141 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("%p: u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_HANDLE);
142 RT_ASSERT_PREEMPTIBLE();
143 IPRT_DARWIN_SAVE_EFL_AC();
144
145 lck_mtx_unlock(pThis->pMtx);
146
147 IPRT_DARWIN_RESTORE_EFL_ONLY_AC();
148 return VINF_SUCCESS;
149}
150
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use