1 /* $Id: semfastmutex-r0drv-linux.c $ */
3 * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Linux.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
28 /*********************************************************************************************************************************
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>
38 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
39 # include <iprt/thread.h>
42 #include "internal/magics.h"
45 /*********************************************************************************************************************************
46 * Structures and Typedefs *
47 *********************************************************************************************************************************/
49 * Wrapper for the linux semaphore structure.
51 typedef struct RTSEMFASTMUTEXINTERNAL
53 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
55 /** the linux semaphore. */
56 struct semaphore Semaphore
;
57 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
59 RTNATIVETHREAD
volatile Owner
;
61 } RTSEMFASTMUTEXINTERNAL
, *PRTSEMFASTMUTEXINTERNAL
;
64 RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx
)
66 IPRT_LINUX_SAVE_EFL_AC();
71 PRTSEMFASTMUTEXINTERNAL pThis
;
72 pThis
= (PRTSEMFASTMUTEXINTERNAL
)RTMemAlloc(sizeof(*pThis
));
74 return VERR_NO_MEMORY
;
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
;
86 IPRT_LINUX_RESTORE_EFL_AC();
89 RT_EXPORT_SYMBOL(RTSemFastMutexCreate
);
92 RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx
)
97 PRTSEMFASTMUTEXINTERNAL pThis
= hFastMtx
;
98 if (pThis
== NIL_RTSEMFASTMUTEX
)
100 AssertPtrReturn(pThis
, VERR_INVALID_HANDLE
);
101 AssertMsgReturn(pThis
->u32Magic
== RTSEMFASTMUTEX_MAGIC
, ("u32Magic=%RX32 pThis=%p\n", pThis
->u32Magic
, pThis
), VERR_INVALID_HANDLE
);
103 ASMAtomicWriteU32(&pThis
->u32Magic
, RTSEMFASTMUTEX_MAGIC_DEAD
);
107 RT_EXPORT_SYMBOL(RTSemFastMutexDestroy
);
110 RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx
)
112 IPRT_LINUX_SAVE_EFL_AC();
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
);
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());
129 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
132 RT_EXPORT_SYMBOL(RTSemFastMutexRequest
);
135 RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx
)
137 IPRT_LINUX_SAVE_EFL_AC();
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
);
146 #if defined(RT_STRICT) || defined(IPRT_DEBUG_SEMS)
147 AssertRelease(pThis
->Owner
== RTThreadNativeSelf());
148 ASMAtomicUoWriteSize(&pThis
->Owner
, NIL_RTNATIVETHREAD
);
150 up(&pThis
->Semaphore
);
151 IPRT_DEBUG_SEMS_STATE(pThis
, 'u');
153 IPRT_LINUX_RESTORE_EFL_ONLY_AC();
156 RT_EXPORT_SYMBOL(RTSemFastMutexRelease
);