VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/spinlock-r0drv-os2.cpp@ 95294

Last change on this file since 95294 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: spinlock-r0drv-os2.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, Ring-0 Driver, OS/2.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2022 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 * --------------------------------------------------------------------
28 *
29 * This code is based on:
30 *
31 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
32 *
33 * Permission is hereby granted, free of charge, to any person
34 * obtaining a copy of this software and associated documentation
35 * files (the "Software"), to deal in the Software without
36 * restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell
38 * copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following
40 * conditions:
41 *
42 * The above copyright notice and this permission notice shall be
43 * included in all copies or substantial portions of the Software.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
46 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
47 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
48 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
49 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
50 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
52 * OTHER DEALINGS IN THE SOFTWARE.
53 */
54
55
56/*********************************************************************************************************************************
57* Header Files *
58*********************************************************************************************************************************/
59#include "the-os2-kernel.h"
60
61#include <iprt/spinlock.h>
62#include <iprt/errcore.h>
63#include <iprt/alloc.h>
64#include <iprt/assert.h>
65#include <iprt/asm.h>
66#ifdef RT_STRICT
67# include <iprt/asm-amd64-x86.h>
68#endif
69
70#include "internal/magics.h"
71
72
73/*********************************************************************************************************************************
74* Structures and Typedefs *
75*********************************************************************************************************************************/
76/**
77 * Wrapper for the SpinLock_t type.
78 */
79typedef struct RTSPINLOCKINTERNAL
80{
81 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
82 uint32_t volatile u32Magic;
83 /** Spinlock creation flags. */
84 uint32_t fFlags;
85 /** The OS/2 spinlock structure. */
86 SpinLock_t Spinlock;
87} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
88
89
90RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
91{
92 AssertReturn(fFlags == RTSPINLOCK_FLAGS_INTERRUPT_SAFE || fFlags == RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, VERR_INVALID_PARAMETER);
93 RT_NOREF(pszName);
94
95 /*
96 * Allocate.
97 */
98 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
99 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
100 if (!pThis)
101 return VERR_NO_MEMORY;
102
103 /*
104 * Initialize & return.
105 */
106 pThis->u32Magic = RTSPINLOCK_MAGIC;
107 pThis->fFlags = fFlags;
108 KernAllocSpinLock(&pThis->Spinlock);
109 *pSpinlock = pThis;
110 return VINF_SUCCESS;
111}
112
113
114RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
115{
116 /*
117 * Validate input.
118 */
119 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
120 if (!pThis)
121 return VERR_INVALID_PARAMETER;
122 AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
123 ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
124 VERR_INVALID_PARAMETER);
125
126 /*
127 * Make the lock invalid and release the memory.
128 */
129 ASMAtomicIncU32(&pThis->u32Magic);
130 KernFreeSpinLock(&pThis->Spinlock);
131 RTMemFree(pThis);
132 return VINF_SUCCESS;
133}
134
135
136RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
137{
138 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
139 AssertPtr(pThis);
140 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
141
142 KernAcquireSpinLock(&pThis->Spinlock);
143 Assert(!ASMIntAreEnabled()); /** @todo verify that interrupts are disabled. */
144}
145
146
147RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
148{
149 PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
150 AssertPtr(pThis);
151 Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
152
153 KernReleaseSpinLock(&pThis->Spinlock);
154}
155
Note: See TracBrowser for help on using the repository browser.

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