VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* $Id: semevent-r0drv-nt.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Single Release Event Semaphores, Ring-0 Driver, NT.
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/*******************************************************************************
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 phEventSem)
58{
59 return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
60}
61
62
63RTDECL(int) RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
64{
65 AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
66 AssertCompile(sizeof(RTSEMEVENTINTERNAL) > sizeof(void *));
67
68 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
69 if (!pThis)
70 return VERR_NO_MEMORY;
71
72 pThis->u32Magic = RTSEMEVENT_MAGIC;
73 KeInitializeEvent(&pThis->Event, SynchronizationEvent, FALSE);
74
75 *phEventSem = pThis;
76 return VINF_SUCCESS;
77}
78
79
80RTDECL(int) RTSemEventDestroy(RTSEMEVENT hEventSem)
81{
82 /*
83 * Validate input.
84 */
85 PRTSEMEVENTINTERNAL pThis = hEventSem;
86 if (pThis == NIL_RTSEMEVENT)
87 return VINF_SUCCESS;
88 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
89 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
90
91 /*
92 * Invalidate it and signal the object just in case.
93 */
94 ASMAtomicIncU32(&pThis->u32Magic);
95 KeSetEvent(&pThis->Event, 0xfff, FALSE);
96 RTMemFree(pThis);
97 return VINF_SUCCESS;
98}
99
100
101RTDECL(int) RTSemEventSignal(RTSEMEVENT hEventSem)
102{
103 /*
104 * Validate input.
105 */
106 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
107 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
108 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
109
110 /*
111 * Signal the event object.
112 */
113 KeSetEvent(&pThis->Event, 1, FALSE);
114 return VINF_SUCCESS;
115}
116
117
118static int rtSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies, bool fInterruptible)
119{
120 /*
121 * Validate input.
122 */
123 PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
124 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
125 AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
126
127 /*
128 * Wait for it.
129 * We're assuming interruptible waits should happen at UserMode level.
130 */
131 NTSTATUS rcNt;
132 KPROCESSOR_MODE WaitMode = fInterruptible ? UserMode : KernelMode;
133 if (cMillies == RT_INDEFINITE_WAIT)
134 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, NULL);
135 else
136 {
137 LARGE_INTEGER Timeout;
138 Timeout.QuadPart = -(int64_t)cMillies * 10000;
139 rcNt = KeWaitForSingleObject(&pThis->Event, Executive, WaitMode, fInterruptible, &Timeout);
140 }
141 switch (rcNt)
142 {
143 case STATUS_SUCCESS:
144 if (pThis->u32Magic == RTSEMEVENT_MAGIC)
145 return VINF_SUCCESS;
146 return VERR_SEM_DESTROYED;
147 case STATUS_ALERTED:
148 return VERR_INTERRUPTED;
149 case STATUS_USER_APC:
150 return VERR_INTERRUPTED;
151 case STATUS_TIMEOUT:
152 return VERR_TIMEOUT;
153 default:
154 AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
155 pThis->u32Magic, pThis, (long)rcNt));
156 return VERR_INTERNAL_ERROR;
157 }
158}
159
160
161RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
162{
163 return rtSemEventWait(hEventSem, cMillies, false /* fInterruptible */);
164}
165
166
167RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, RTMSINTERVAL cMillies)
168{
169 return rtSemEventWait(hEventSem, cMillies, true /* fInterruptible */);
170}
171
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