VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semevent-r0drv-linux.c@ 25724

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

iprt: Use RTMSINTERVAL for timeouts. Fixed missing timeout underflow checks in two RTFileAioCtxWait implementations.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: semevent-r0drv-linux.c 25724 2010-01-11 14:45:34Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-linux-kernel.h"
36#include "internal/iprt.h"
37#include <iprt/semaphore.h>
38#include <iprt/alloc.h>
39#include <iprt/assert.h>
40#include <iprt/asm.h>
41#include <iprt/err.h>
42
43#include "internal/magics.h"
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Linux event semaphore.
51 */
52typedef struct RTSEMEVENTINTERNAL
53{
54 /** Magic value (RTSEMEVENT_MAGIC). */
55 uint32_t volatile u32Magic;
56 /** The object status - !0 when signaled and 0 when reset. */
57 uint32_t volatile fState;
58 /** The wait queue. */
59 wait_queue_head_t Head;
60} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
61
62
63
64RTDECL(int) RTSemEventCreate(PRTSEMEVENT phEventSem)
65{
66 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
67}
68
69
70RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
71{
72 PRTSEMEVENTINTERNAL pThis;
73
74 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
75 pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
76 if (!pThis)
77 return VERR_NO_MEMORY;
78
79 pThis->u32Magic = RTSEMEVENT_MAGIC;
80 pThis->fState = 0;
81 init_waitqueue_head(&pThis->Head);
82
83 *phEventSem = pThis;
84 return VINF_SUCCESS;
85}
86RT_EXPORT_SYMBOL(RTSemEventCreate);
87
88
89RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
90{
91 /*
92 * Validate input.
93 */
94 PRTSEMEVENTINTERNAL pThis = hEventSem;
95 if (pThis == NIL_RTSEMEVENT)
96 return VINF_SUCCESS;
97 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
98
99 /*
100 * Invalidate it and signal the object just in case.
101 */
102 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
103 ASMAtomicWriteU32(&pThis->fState, 0);
104 Assert(!waitqueue_active(&pThis->Head));
105 wake_up_all(&pThis->Head);
106 RTMemFree(pThis);
107 return VINF_SUCCESS;
108}
109RT_EXPORT_SYMBOL(RTSemEventDestroy);
110
111
112RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
113{
114 /*
115 * Validate input.
116 */
117 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
118 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
119 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
120
121 /*
122 * Signal the event object.
123 */
124 ASMAtomicWriteU32(&pThis->fState, 1);
125 wake_up(&pThis->Head);
126
127 return VINF_SUCCESS;
128}
129RT_EXPORT_SYMBOL(RTSemEventSignal);
130
131
132/**
133 * Worker for RTSemEvent and RTSemEventNoResume.
134 *
135 * @returns VBox status code.
136 * @param pThis The event semaphore.
137 * @param cMillies The number of milliseconds to wait.
138 * @param fInterruptible Whether it's an interruptible wait or not.
139 */
140static int rtSemEventWait(PRTSEMEVENTINTERNAL pThis, RTMSINTERVAL cMillies, bool fInterruptible)
141{
142 /*
143 * Ok wait for it.
144 */
145 DEFINE_WAIT(Wait);
146 int rc = VINF_SUCCESS;
147 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
148#ifdef IPRT_DEBUG_SEMS
149 snprintf(current->comm, TASK_COMM_LEN, "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pThis));
150#endif
151 for (;;)
152 {
153 /* make everything thru schedule_timeout() atomic scheduling wise. */
154 prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
155
156 /* check the condition. */
157 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
158 break;
159
160 /* check for pending signals. */
161 if (fInterruptible && signal_pending(current))
162 {
163 rc = VERR_INTERRUPTED;
164 break;
165 }
166
167 /* wait */
168 lTimeout = schedule_timeout(lTimeout);
169
170 after_wait(&Wait);
171
172 /* Check if someone destroyed the semaphore while we were waiting. */
173 if (pThis->u32Magic != RTSEMEVENT_MAGIC)
174 {
175 rc = VERR_SEM_DESTROYED;
176 break;
177 }
178
179 /* check for timeout. */
180 if (!lTimeout)
181 {
182 rc = VERR_TIMEOUT;
183 break;
184 }
185 }
186
187 finish_wait(&pThis->Head, &Wait);
188#ifdef IPRT_DEBUG_SEMS
189 snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
190#endif
191 return rc;
192}
193
194
195RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
196{
197 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
198 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
199 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
200
201 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
202 return VINF_SUCCESS;
203 return rtSemEventWait(pThis, cMillies, false /* fInterruptible */);
204}
205RT_EXPORT_SYMBOL(RTSemEventWait);
206
207
208RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
209{
210 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
211 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
212 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
213
214 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
215 return VINF_SUCCESS;
216 return rtSemEventWait(pThis, cMillies, true /* fInterruptible */);
217}
218RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
219
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