]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - ubuntu/vbox/r0drv/linux/semfastmutex-r0drv-linux.c
UBUNTU: ubuntu: vbox -- update to 5.1.6-dfsg-1
[mirror_ubuntu-zesty-kernel.git] / ubuntu / vbox / r0drv / linux / semfastmutex-r0drv-linux.c
1 /* $Id: semfastmutex-r0drv-linux.c $ */
2 /** @file
3 * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Linux.
4 */
5
6 /*
7 * Copyright (C) 2006-2016 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-linux-kernel.h"
32 #include "internal/iprt.h"
33 #include <iprt/semaphore.h>
34 #include <iprt/alloc.h>
35 #include <iprt/assert.h>
36 #include <iprt/asm.h>
37 #include <iprt/err.h>
38 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
39 # include <iprt/thread.h>
40 #endif
41
42 #include "internal/magics.h"
43
44
45 /*********************************************************************************************************************************
46 * Structures and Typedefs *
47 *********************************************************************************************************************************/
48 /**
49 * Wrapper for the linux semaphore structure.
50 */
51 typedef struct RTSEMFASTMUTEXINTERNAL
52 {
53 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
54 uint32_t u32Magic;
55 /** the linux semaphore. */
56 struct semaphore Semaphore;
57 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
58 /** For check. */
59 RTNATIVETHREAD volatile Owner;
60 #endif
61 } RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
62
63
64 RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
65 {
66 IPRT_LINUX_SAVE_EFL_AC();
67
68 /*
69 * Allocate.
70 */
71 PRTSEMFASTMUTEXINTERNAL pThis;
72 pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
73 if (!pThis)
74 return VERR_NO_MEMORY;
75
76 /*
77 * Initialize.
78 */
79 pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
80 sema_init(&pThis->Semaphore, 1);
81 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
82 pThis->Owner = NIL_RTNATIVETHREAD;
83 #endif
84
85 *phFastMtx = pThis;
86 IPRT_LINUX_RESTORE_EFL_AC();
87 return VINF_SUCCESS;
88 }
89 RT_EXPORT_SYMBOL(RTSemFastMutexCreate);
90
91
92 RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
93 {
94 /*
95 * Validate.
96 */
97 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
98 if (pThis == NIL_RTSEMFASTMUTEX)
99 return VINF_SUCCESS;
100 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
101 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
102
103 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
104 RTMemFree(pThis);
105 return VINF_SUCCESS;
106 }
107 RT_EXPORT_SYMBOL(RTSemFastMutexDestroy);
108
109
110 RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
111 {
112 IPRT_LINUX_SAVE_EFL_AC();
113
114 /*
115 * Validate.
116 */
117 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
118 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
119 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
120
121 IPRT_DEBUG_SEMS_STATE(pThis, 'd');
122 down(&pThis->Semaphore);
123 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
124 IPRT_DEBUG_SEMS_STATE(pThis, 'o');
125 AssertRelease(pThis->Owner == NIL_RTNATIVETHREAD);
126 ASMAtomicUoWriteSize(&pThis->Owner, RTThreadNativeSelf());
127 #endif
128
129 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
130 return VINF_SUCCESS;
131 }
132 RT_EXPORT_SYMBOL(RTSemFastMutexRequest);
133
134
135 RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
136 {
137 IPRT_LINUX_SAVE_EFL_AC();
138
139 /*
140 * Validate.
141 */
142 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
143 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
144 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
145
146 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
147 AssertRelease(pThis->Owner == RTThreadNativeSelf());
148 ASMAtomicUoWriteSize(&pThis->Owner, NIL_RTNATIVETHREAD);
149 #endif
150 up(&pThis->Semaphore);
151 IPRT_DEBUG_SEMS_STATE(pThis, 'u');
152
153 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
154 return VINF_SUCCESS;
155 }
156 RT_EXPORT_SYMBOL(RTSemFastMutexRelease);
157