VirtualBox

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

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

Runtime: s/TASK_COMM_LEN/sizeof(current->comm)/ (fix debug builds on older Linux kernels)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/* $Id: semevent-r0drv-linux.c 29648 2010-05-18 16:15:42Z 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#ifdef IPRT_DEBUG_SEMS
145 snprintf(current->comm, sizeof(current->comm), "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pThis));
146#endif
147 for (;;)
148 {
149 /* make everything thru schedule_timeout() atomic scheduling wise. */
150 prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
151
152 /* check the condition. */
153 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
154 break;
155
156 /* check for pending signals. */
157 if (fInterruptible && signal_pending(current))
158 {
159 rc = VERR_INTERRUPTED;
160 break;
161 }
162
163 /* wait */
164 lTimeout = schedule_timeout(lTimeout);
165
166 after_wait(&Wait);
167
168 /* Check if someone destroyed the semaphore while we were waiting. */
169 if (pThis->u32Magic != RTSEMEVENT_MAGIC)
170 {
171 rc = VERR_SEM_DESTROYED;
172 break;
173 }
174
175 /* check for timeout. */
176 if (!lTimeout)
177 {
178 rc = VERR_TIMEOUT;
179 break;
180 }
181 }
182
183 finish_wait(&pThis->Head, &Wait);
184#ifdef IPRT_DEBUG_SEMS
185 snprintf(current->comm, sizeof(current->comm), "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
186#endif
187 return rc;
188}
189
190
191RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
192{
193 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
194 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
195 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
196
197 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
198 return VINF_SUCCESS;
199 return rtSemEventWait(pThis, cMillies, false /* fInterruptible */);
200}
201RT_EXPORT_SYMBOL(RTSemEventWait);
202
203
204RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
205{
206 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
207 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
208 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
209
210 if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
211 return VINF_SUCCESS;
212 return rtSemEventWait(pThis, cMillies, true /* fInterruptible */);
213}
214RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
215
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