VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/semevent-r0drv-nt.cpp@ 7331

Last change on this file since 7331 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.4 KB
Line 
1/* $Id: semevent-r0drv-nt.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Single Release Event Semaphores, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#include "the-nt-kernel.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 * NT event semaphore.
47 */
48typedef struct RTSEMEVENTINTERNAL
49{
50 /** Magic value (RTSEMEVENT_MAGIC). */
51 uint32_t volatile u32Magic;
52 /** The NT Event object. */
53 KEVENT Event;
54} RTSEMEVENTINTERNAL, *PRTSEMEVENTINTERNAL;
55
56
57RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
58{
59 Assert(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
60 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
61 if (pEventInt)
62 {
63 pEventInt->u32Magic = RTSEMEVENT_MAGIC;
64 KeInitializeEvent(&pEventInt->Event, SynchronizationEvent, FALSE);
65 *pEventSem = pEventInt;
66 return VINF_SUCCESS;
67 }
68 return VERR_NO_MEMORY;
69}
70
71
72RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
73{
74 /*
75 * Validate input.
76 */
77 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
78 if (!pEventInt)
79 return VERR_INVALID_PARAMETER;
80 if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
81 {
82 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
83 return VERR_INVALID_PARAMETER;
84 }
85
86 /*
87 * Invalidate it and signal the object just in case.
88 */
89 ASMAtomicIncU32(&pEventInt->u32Magic);
90 KeSetEvent(&pEventInt->Event, 0xfff, FALSE);
91 RTMemFree(pEventInt);
92 return VINF_SUCCESS;
93}
94
95
96RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
97{
98 /*
99 * Validate input.
100 */
101 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
102 if (!pEventInt)
103 return VERR_INVALID_PARAMETER;
104 if ( !pEventInt
105 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
106 {
107 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
108 return VERR_INVALID_PARAMETER;
109 }
110
111 /*
112 * Signal the event object.
113 */
114 KeSetEvent(&pEventInt->Event, 1, FALSE);
115 return VINF_SUCCESS;
116}
117
118
119static int rtSemEventWait(RTSEMEVENT EventSem, unsigned cMillies, bool fInterruptible)
120{
121 /*
122 * Validate input.
123 */
124 PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
125 if (!pEventInt)
126 return VERR_INVALID_PARAMETER;
127 if ( !pEventInt
128 || pEventInt->u32Magic != RTSEMEVENT_MAGIC)
129 {
130 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
131 return VERR_INVALID_PARAMETER;
132 }
133
134 /*
135 * Wait for it.
136 */
137 NTSTATUS rcNt;
138 if (cMillies == RT_INDEFINITE_WAIT)
139 rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, NULL);
140 else
141 {
142 LARGE_INTEGER Timeout;
143 Timeout.QuadPart = -(int64_t)cMillies * 10000;
144 rcNt = KeWaitForSingleObject(&pEventInt->Event, Executive, KernelMode, fInterruptible, &Timeout);
145 }
146 switch (rcNt)
147 {
148 case STATUS_SUCCESS:
149 if (pEventInt->u32Magic == RTSEMEVENT_MAGIC)
150 return VINF_SUCCESS;
151 return VERR_SEM_DESTROYED;
152 case STATUS_ALERTED:
153 return VERR_INTERRUPTED;
154 case STATUS_USER_APC:
155 return VERR_INTERRUPTED;
156 case STATUS_TIMEOUT:
157 return VERR_TIMEOUT;
158 default:
159 AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p: wait returned %lx!\n",
160 pEventInt->u32Magic, pEventInt, (long)rcNt));
161 return VERR_INTERNAL_ERROR;
162 }
163}
164
165
166RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
167{
168 return rtSemEventWait(EventSem, cMillies, false /* fInterruptible */);
169}
170
171
172RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
173{
174 return rtSemEventWait(EventSem, cMillies, true /* fInterruptible */);
175}
176
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