VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/semeventmulti-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: 7.6 KB
Line 
1/* $Id: semeventmulti-r0drv-linux.c 29661 2010-05-19 14:29:49Z vboxsync $ */
2/** @file
3 * IPRT - Multiple 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 RTSEMEVENTMULTIINTERNAL
49{
50 /** Magic value (RTSEMEVENTMULTI_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} RTSEMEVENTMULTIINTERNAL, *PRTSEMEVENTMULTIINTERNAL;
57
58
59
60RTDECL(int) RTSemEventMultiCreate(PRTSEMEVENTMULTI phEventMultiSem)
61{
62 return RTSemEventMultiCreateEx(phEventMultiSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
63}
64
65
66RTDECL(int) RTSemEventMultiCreateEx(PRTSEMEVENTMULTI phEventMultiSem, uint32_t fFlags, RTLOCKVALCLASS hClass,
67 const char *pszNameFmt, ...)
68{
69 PRTSEMEVENTMULTIINTERNAL pThis;
70
71 AssertReturn(!(fFlags & ~RTSEMEVENTMULTI_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
72 pThis = (PRTSEMEVENTMULTIINTERNAL)RTMemAlloc(sizeof(*pThis));
73 if (pThis)
74 {
75 pThis->u32Magic = RTSEMEVENTMULTI_MAGIC;
76 pThis->fState = 0;
77 init_waitqueue_head(&pThis->Head);
78
79 *phEventMultiSem = pThis;
80 return VINF_SUCCESS;
81 }
82 return VERR_NO_MEMORY;
83}
84RT_EXPORT_SYMBOL(RTSemEventMultiCreate);
85
86
87RTDECL(int) RTSemEventMultiDestroy(RTSEMEVENTMULTI hEventMultiSem)
88{
89 /*
90 * Validate input.
91 */
92 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
93 if (pThis == NIL_RTSEMEVENTMULTI)
94 return VINF_SUCCESS;
95 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
96 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
97
98 /*
99 * Invalidate it and signal the object just in case.
100 */
101 ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENTMULTI_MAGIC);
102 ASMAtomicXchgU32(&pThis->fState, 0);
103 Assert(!waitqueue_active(&pThis->Head));
104 wake_up_all(&pThis->Head);
105 RTMemFree(pThis);
106 return VINF_SUCCESS;
107}
108RT_EXPORT_SYMBOL(RTSemEventMultiDestroy);
109
110
111RTDECL(int) RTSemEventMultiSignal(RTSEMEVENTMULTI hEventMultiSem)
112{
113 /*
114 * Validate input.
115 */
116 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
117 if (!pThis)
118 return VERR_INVALID_PARAMETER;
119 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
120 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
121
122 /*
123 * Signal the event object.
124 */
125 ASMAtomicXchgU32(&pThis->fState, 1);
126 wake_up_all(&pThis->Head);
127 return VINF_SUCCESS;
128}
129RT_EXPORT_SYMBOL(RTSemEventMultiSignal);
130
131
132RTDECL(int) RTSemEventMultiReset(RTSEMEVENTMULTI hEventMultiSem)
133{
134 /*
135 * Validate input.
136 */
137 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
138 if (!pThis)
139 return VERR_INVALID_PARAMETER;
140 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
141 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
142
143 /*
144 * Reset it.
145 */
146 ASMAtomicXchgU32(&pThis->fState, 0);
147 return VINF_SUCCESS;
148}
149RT_EXPORT_SYMBOL(RTSemEventMultiReset);
150
151
152/**
153 * Worker for RTSemEventMulti and RTSemEventMultiNoResume.
154 *
155 * @returns VBox status code.
156 * @param pThis The event semaphore.
157 * @param cMillies The number of milliseconds to wait.
158 * @param fInterruptible Whether it's an interruptible wait or not.
159 */
160static int rtSemEventMultiWait(PRTSEMEVENTMULTIINTERNAL pThis, RTMSINTERVAL cMillies, bool fInterruptible)
161{
162 /*
163 * Ok wait for it.
164 */
165 DEFINE_WAIT(Wait);
166 int rc = VINF_SUCCESS;
167 long lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
168 IPRT_DEBUG_SEMS_STATE(pThis, 'E');
169 for (;;)
170 {
171 /* make everything thru schedule() atomic scheduling wise. */
172 prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
173
174 /* check the condition. */
175 if (pThis->fState)
176 break;
177
178 /* check for pending signals. */
179 if (fInterruptible && signal_pending(current))
180 {
181 rc = VERR_INTERRUPTED;
182 break;
183 }
184
185 /* wait */
186 lTimeout = schedule_timeout(lTimeout);
187
188 after_wait(&Wait);
189
190 /* Check if someone destroyed the semaphore while we were waiting. */
191 if (pThis->u32Magic != RTSEMEVENTMULTI_MAGIC)
192 {
193 rc = VERR_SEM_DESTROYED;
194 break;
195 }
196
197 /* check for timeout. */
198 if (!lTimeout)
199 {
200 rc = VERR_TIMEOUT;
201 break;
202 }
203 }
204
205 finish_wait(&pThis->Head, &Wait);
206 IPRT_DEBUG_SEMS_STATE_RC(pThis, 'E', rc);
207 return rc;
208}
209
210
211RTDECL(int) RTSemEventMultiWait(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
212{
213 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
214 if (!pThis)
215 return VERR_INVALID_PARAMETER;
216 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
217 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
218
219 if (pThis->fState)
220 return VINF_SUCCESS;
221 return rtSemEventMultiWait(pThis, cMillies, false /* fInterruptible */);
222}
223RT_EXPORT_SYMBOL(RTSemEventMultiWait);
224
225
226RTDECL(int) RTSemEventMultiWaitNoResume(RTSEMEVENTMULTI hEventMultiSem, RTMSINTERVAL cMillies)
227{
228 PRTSEMEVENTMULTIINTERNAL pThis = (PRTSEMEVENTMULTIINTERNAL)hEventMultiSem;
229 if (!pThis)
230 return VERR_INVALID_PARAMETER;
231 AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
232 AssertMsgReturn(pThis->u32Magic == RTSEMEVENTMULTI_MAGIC, ("%p u32Magic=%RX32\n", pThis, pThis->u32Magic), VERR_INVALID_PARAMETER);
233
234 if (pThis->fState)
235 return VINF_SUCCESS;
236 return rtSemEventMultiWait(pThis, cMillies, true /* fInterruptible */);
237}
238RT_EXPORT_SYMBOL(RTSemEventMultiWaitNoResume);
239
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