VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semevent-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: 6.6 KB
Line 
1/* $Id: semevent-r0drv-linux.c 29661 2010-05-19 14:29:49Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event 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
39#include "internal/magics.h"
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45/**
46 * Linux event semaphore.
47 */
48typedef struct RTSEMEVENTINTERNAL
49{
50 /** Magic value (RTSEMEVENT_MAGIC). */
51 uint32_t volatile u32Magic;
52 /** The object status - !0 when signaled and 0 when reset. */
53 uint32_t volatile fState;
54 /** The wait queue. */
55 wait_queue_head_t Head;
56} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
57
58
59
60RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
61{
62 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
63}
64
65
66RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
67{
68 PRTSEMEVENTINTERNAL pThis;
69
70 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
71 pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
72 if (!pThis)
73 return VERR_NO_MEMORY;
74
75 pThis->u32Magic = RTSEMEVENT_MAGIC;
76 pThis->fState = 0;
77 init_waitqueue_head(&pThis->Head);
78
79 *phEventSem = pThis;
80 return VINF_SUCCESS;
81}
82RT_EXPORT_SYMBOL(RTSemEventCreate);
83
84
85RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
86{
87 /*
88 * Validate input.
89 */
90 PRTSEMEVENTINTERNAL pThis = hEventSem;
91 if (pThis == NIL_RTSEMEVENT)
92 return VINF_SUCCESS;
93 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
94
95 /*
96 * Invalidate it and signal the object just in case.
97 */
98 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
99 ASMAtomicWriteU32(&pThis->fState, 0);
100 Assert(!waitqueue_active(&pThis->Head));
101 wake_up_all(&pThis->Head);
102 RTMemFree(pThis);
103 return VINF_SUCCESS;
104}
105RT_EXPORT_SYMBOL(RTSemEventDestroy);
106
107
108RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
109{
110 /*
111 * Validate input.
112 */
113 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
114 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
115 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
116
117 /*
118 * Signal the event object.
119 */
120 ASMAtomicWriteU32(&pThis->fState, 1);
121 wake_up(&pThis->Head);
122
123 return VINF_SUCCESS;
124}
125RT_EXPORT_SYMBOL(RTSemEventSignal);
126
127
128/**
129 * Worker for RTSemEvent and RTSemEventNoResume.
130 *
131 * @returns VBox status code.
132 * @param pThis The event semaphore.
133 * @param cMillies The number of milliseconds to wait.
134 * @param fInterruptible Whether it's an interruptible wait or not.
135 */
136static int rtSemEventWait(PRTSEMEVENTINTERNAL pThis, RTMSINTERVAL cMillies, bool fInterruptible)
137{
138 /*
139 * Ok wait for it.
140 */
141 DEFINE_WAIT(Wait);
142 int rc = VINF_SUCCESS;
143 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
144 IPRT_DEBUG_SEMS_STATE(pThis, 'e');
145 for (;;)
146 {
147 /* make everything thru schedule_timeout() atomic scheduling wise. */
148 prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
149
150 /* check the condition. */
151 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
152 break;
153
154 /* check for pending signals. */
155 if (fInterruptible && signal_pending(current))
156 {
157 rc = VERR_INTERRUPTED;
158 break;
159 }
160
161 /* wait */
162 lTimeout = schedule_timeout(lTimeout);
163
164 after_wait(&Wait);
165
166 /* Check if someone destroyed the semaphore while we were waiting. */
167 if (pThis->u32Magic != RTSEMEVENT_MAGIC)
168 {
169 rc = VERR_SEM_DESTROYED;
170 break;
171 }
172
173 /* check for timeout. */
174 if (!lTimeout)
175 {
176 rc = VERR_TIMEOUT;
177 break;
178 }
179 }
180
181 finish_wait(&pThis->Head, &Wait);
182 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'e', rc);
183 return rc;
184}
185
186
187RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
188{
189 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
190 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
191 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
192
193 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
194 return VINF_SUCCESS;
195 return rtSemEventWait(pThis, cMillies, false /* fInterruptible */);
196}
197RT_EXPORT_SYMBOL(RTSemEventWait);
198
199
200RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
201{
202 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
203 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
204 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
205
206 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
207 return VINF_SUCCESS;
208 return rtSemEventWait(pThis, cMillies, true /* fInterruptible */);
209}
210RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
211
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