VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/haiku/spinlock-r0drv-haiku.c@ 44579

Last change on this file since 44579 was 43403, checked in by vboxsync, 12 years ago

r0drv/haiku: cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/* $Id: spinlock-r0drv-haiku.c 43403 2012-09-22 11:48:24Z vboxsync $ */
2/** @file
3 * IPRT - Spinlocks, 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.virtualbox.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/spinlock.h>
34
35#include <iprt/assert.h>
36#include <iprt/asm.h>
37#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
38#include <iprt/asm-amd64-x86.h>
39#endif
40#include <iprt/err.h>
41#include <iprt/mem.h>
42#include <iprt/thread.h>
43
44#include "internal/magics.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * Wrapper for the KSPIN_LOCK type.
52 */
53typedef struct RTSPINLOCKINTERNAL
54{
55 /** Spinlock magic value (RTSPINLOCK_MAGIC). */
56 uint32_t volatile u32Magic;
57 /** Spinlock creation flags */
58 uint32_t fFlags;
59 /** Saved interrupt CPU status. */
60 cpu_status volatile fIntSaved;
61 /** The Haiku spinlock structure. */
62 spinlock hSpinLock;
63} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
64
65
66
67RTDECL(int) RTSpinlockCreate(PRTSPINLOCK pSpinlock, uint32_t fFlags, const char *pszName)
68{
69 RT_ASSERT_PREEMPTIBLE();
70 NOREF(pszName);
71
72 /*
73 * Allocate.
74 */
75 AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
76 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
77 if (RT_UNLIKELY(!pSpinlockInt))
78 return VERR_NO_MEMORY;
79
80 /*
81 * Initialize & return.
82 */
83 pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
84 pSpinlockInt->fFlags = fFlags;
85 pSpinlockInt->fIntSaved = 0;
86 B_INITIALIZE_SPINLOCK(&pSpinlockInt->hSpinLock);
87
88 *pSpinlock = pSpinlockInt;
89 return VINF_SUCCESS;
90}
91
92
93RTDECL(int) RTSpinlockDestroy(RTSPINLOCK Spinlock)
94{
95 /*
96 * Validate input.
97 */
98 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
99 if (RT_UNLIKELY(!pSpinlockInt))
100 return VERR_INVALID_PARAMETER;
101 AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
102 ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
103 VERR_INVALID_PARAMETER);
104
105 /*
106 * Make the lock invalid and release the memory.
107 */
108 ASMAtomicIncU32(&pSpinlockInt->u32Magic);
109
110 B_INITIALIZE_SPINLOCK(&pSpinlockInt->hSpinLock);
111
112 RTMemFree(pSpinlockInt);
113 return VINF_SUCCESS;
114}
115
116
117RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock)
118{
119 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
120 AssertPtr(pSpinlockInt);
121 Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
122
123 /* Haiku cannot take spinlocks without disabling interrupts. Ignore our spinlock creation flags. */
124 pSpinlockInt->fIntSaved = disable_interrupts();
125 acquire_spinlock(&pSpinlockInt->hSpinLock);
126}
127
128
129RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock)
130{
131 PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
132 AssertPtr(pSpinlockInt);
133 Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
134
135 release_spinlock(&pSpinlockInt->hSpinLock);
136 restore_interrupts(pSpinlockInt->fIntSaved);
137}
138
139
140RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock)
141{
142 if (!(Spinlock->fFlags & RTSPINLOCK_FLAGS_INTERRUPT_SAFE))
143 RTAssertMsg2("RTSpinlockReleaseNoInts: p=%p (magic=%#x)\n", Spinlock, Spinlock->u32Magic);
144 RTSpinlockRelease(Spinlock);
145}
146
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