VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semfastmutex-r0drv-linux.c@ 32043

Last change on this file since 32043 was 29661, checked in by vboxsync, 15 years ago

IPRT: IPRT_DEBUG_SEMS cleanup - use macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/* $Id: semfastmutex-r0drv-linux.c 29661 2010-05-19 14:29:49Z vboxsync $ */
2/** @file
3 * IPRT - Fast Mutex Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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#ifdef 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 */
51typedef struct RTSEMFASTMUTEXINTERNAL
52{
53 /** Magic value (RTSEMFASTMUTEX_MAGIC). */
54 uint32_t u32Magic;
55 /** the linux semaphore. */
56 struct semaphore Semaphore;
57#ifdef IPRT_DEBUG_SEMS
58 /** For check. */
59 RTNATIVETHREAD volatile Owner;
60#endif
61} RTSEMFASTMUTEXINTERNAL, *PRTSEMFASTMUTEXINTERNAL;
62
63
64RTDECL(int) RTSemFastMutexCreate(PRTSEMFASTMUTEX phFastMtx)
65{
66 /*
67 * Allocate.
68 */
69 PRTSEMFASTMUTEXINTERNAL pThis;
70 pThis = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
71 if (!pThis)
72 return VERR_NO_MEMORY;
73
74 /*
75 * Initialize.
76 */
77 pThis->u32Magic = RTSEMFASTMUTEX_MAGIC;
78 sema_init(&pThis->Semaphore, 1);
79#ifdef IPRT_DEBUG_SEMS
80 pThis->Owner = NIL_RTNATIVETHREAD;
81#endif
82
83 *phFastMtx = pThis;
84 return VINF_SUCCESS;
85}
86RT_EXPORT_SYMBOL(RTSemFastMutexCreate);
87
88
89RTDECL(int) RTSemFastMutexDestroy(RTSEMFASTMUTEX hFastMtx)
90{
91 /*
92 * Validate.
93 */
94 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
95 if (pThis == NIL_RTSEMFASTMUTEX)
96 return VINF_SUCCESS;
97 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
98 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
99
100 ASMAtomicWriteU32(&pThis->u32Magic, RTSEMFASTMUTEX_MAGIC_DEAD);
101 RTMemFree(pThis);
102 return VINF_SUCCESS;
103}
104RT_EXPORT_SYMBOL(RTSemFastMutexDestroy);
105
106
107RTDECL(int) RTSemFastMutexRequest(RTSEMFASTMUTEX hFastMtx)
108{
109 /*
110 * Validate.
111 */
112 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
113 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
114 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
115
116 IPRT_DEBUG_SEMS_STATE(pThis, 'd');
117 down(&pThis->Semaphore);
118#ifdef IPRT_DEBUG_SEMS
119 IPRT_DEBUG_SEMS_STATE(pThis, 'o');
120 AssertRelease(pThis->Owner == NIL_RTNATIVETHREAD);
121 ASMAtomicUoWriteSize(&pThis->Owner, RTThreadNativeSelf());
122#endif
123 return VINF_SUCCESS;
124}
125RT_EXPORT_SYMBOL(RTSemFastMutexRequest);
126
127
128RTDECL(int) RTSemFastMutexRelease(RTSEMFASTMUTEX hFastMtx)
129{
130 /*
131 * Validate.
132 */
133 PRTSEMFASTMUTEXINTERNAL pThis = hFastMtx;
134 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
135 AssertMsgReturn(pThis->u32Magic == RTSEMFASTMUTEX_MAGIC, ("u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
136
137#ifdef IPRT_DEBUG_SEMS
138 AssertRelease(pThis->Owner == RTThreadNativeSelf());
139 ASMAtomicUoWriteSize(&pThis->Owner, NIL_RTNATIVETHREAD);
140#endif
141 up(&pThis->Semaphore);
142 IPRT_DEBUG_SEMS_STATE(pThis, 'u');
143 return VINF_SUCCESS;
144}
145RT_EXPORT_SYMBOL(RTSemFastMutexRelease);
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