1 | /* $Id: semevent-win.cpp 25381 2009-12-14 23:52:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Event Sempahore, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 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 | #define LOG_GROUP RTLOGGROUP_SEMAPHORE
|
---|
36 | #include <Windows.h>
|
---|
37 |
|
---|
38 | #include <iprt/semaphore.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/err.h>
|
---|
42 | #include "internal/strict.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *******************************************************************************/
|
---|
48 | /** Converts semaphore to win32 handle. */
|
---|
49 | #define SEM2HND(Sem) ((HANDLE)(uintptr_t)Sem)
|
---|
50 |
|
---|
51 |
|
---|
52 | /* Undefine debug mappings. */
|
---|
53 | #undef RTSemEventWait
|
---|
54 | #undef RTSemEventWaitNoResume
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(int) RTSemEventCreate(PRTSEMEVENT pEventSem)
|
---|
58 | {
|
---|
59 | /*
|
---|
60 | * Create the semaphore.
|
---|
61 | * (Auto reset, not signaled, private event object.)
|
---|
62 | */
|
---|
63 | HANDLE hev = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
64 | if (hev)
|
---|
65 | {
|
---|
66 | *pEventSem = (RTSEMEVENT)(void *)hev;
|
---|
67 | Assert(*pEventSem != NIL_RTSEMEVENT);
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 | return RTErrConvertFromWin32(GetLastError());
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(int) RTSemEventDestroy(RTSEMEVENT EventSem)
|
---|
75 | {
|
---|
76 | if (EventSem == NIL_RTSEMEVENT) /* don't bitch */
|
---|
77 | return VERR_INVALID_HANDLE;
|
---|
78 |
|
---|
79 | /*
|
---|
80 | * Close semaphore handle.
|
---|
81 | */
|
---|
82 | if (CloseHandle(SEM2HND(EventSem)))
|
---|
83 | return VINF_SUCCESS;
|
---|
84 | AssertMsgFailed(("Destroy EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
|
---|
85 | return RTErrConvertFromWin32(GetLastError());
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
|
---|
90 | {
|
---|
91 | /*
|
---|
92 | * Wait for condition.
|
---|
93 | */
|
---|
94 | int rc = WaitForSingleObjectEx(SEM2HND(EventSem), cMillies == RT_INDEFINITE_WAIT ? INFINITE : cMillies, TRUE);
|
---|
95 | switch (rc)
|
---|
96 | {
|
---|
97 | case WAIT_OBJECT_0: return VINF_SUCCESS;
|
---|
98 | case WAIT_TIMEOUT: return VERR_TIMEOUT;
|
---|
99 | case WAIT_IO_COMPLETION: return VERR_INTERRUPTED;
|
---|
100 | case WAIT_ABANDONED: return VERR_SEM_OWNER_DIED;
|
---|
101 | default:
|
---|
102 | {
|
---|
103 | AssertMsgFailed(("Wait on EventSem %p failed, rc=%d lasterr=%d\n", EventSem, rc, GetLastError()));
|
---|
104 | int rc2 = RTErrConvertFromWin32(GetLastError());
|
---|
105 | if (rc2)
|
---|
106 | return rc2;
|
---|
107 |
|
---|
108 | AssertMsgFailed(("WaitForSingleObject(event) -> rc=%d while converted lasterr=%d\n", rc, rc2));
|
---|
109 | return VERR_INTERNAL_ERROR;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | RTDECL(int) RTSemEventSignal(RTSEMEVENT EventSem)
|
---|
116 | {
|
---|
117 | /*
|
---|
118 | * Signal the object.
|
---|
119 | */
|
---|
120 | if (SetEvent(SEM2HND(EventSem)))
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | AssertMsgFailed(("Signaling EventSem %p failed, lasterr=%d\n", EventSem, GetLastError()));
|
---|
123 | return RTErrConvertFromWin32(GetLastError());
|
---|
124 | }
|
---|
125 |
|
---|