1
/* $Id: tstSemEvent.cpp $ */
3
* IPRT Testcase - Event Semaphore Test.
7
* Copyright (C) 2009 Sun Microsystems, Inc.
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.
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.
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.
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.
31
/*******************************************************************************
33
*******************************************************************************/
34
#include <iprt/semaphore.h>
35
#include <iprt/string.h>
36
#include <iprt/thread.h>
37
#include <iprt/stream.h>
38
#include <iprt/time.h>
39
#include <iprt/initterm.h>
40
#include <iprt/rand.h>
42
#include <iprt/assert.h>
45
/*******************************************************************************
47
*******************************************************************************/
48
static RTSEMEVENTMULTI g_hSemEM = NIL_RTSEMEVENTMULTI;
49
static uint32_t volatile g_cErrors;
52
int PrintError(const char *pszFormat, ...)
54
ASMAtomicIncU32(&g_cErrors);
56
RTPrintf("tstSemEvent: FAILURE - ");
58
va_start(va, pszFormat);
59
RTPrintfV(pszFormat, va);
66
int ThreadTest1(RTTHREAD ThreadSelf, void *pvUser)
69
rc = RTSemEventMultiWait(g_hSemEM, 1000);
70
if (rc != VERR_TIMEOUT)
72
PrintError("Thread 1: unexpected result of first RTSemEventMultiWait %Rrc\n", rc);
76
rc = RTSemEventMultiWait(g_hSemEM, 1000);
79
PrintError("Thread 1: unexpected result of second RTSemEventMultiWait %Rrc\n", rc);
83
RTPrintf("tstSemEvent: Thread 1 normal exit...\n");
88
int ThreadTest2(RTTHREAD ThreadSelf, void *pvUser)
91
rc = RTSemEventMultiWait(g_hSemEM, RT_INDEFINITE_WAIT);
94
PrintError("Thread 2: unexpected result of RTSemEventMultiWait %Rrc\n", rc);
98
RTPrintf("tstSemEvent: Thread 2 normal exit...\n");
106
RTTHREAD Thread1, Thread2;
108
rc = RTSemEventMultiCreate(&g_hSemEM);
110
return PrintError("RTSemEventMultiCreate failed (rc=%Rrc)\n", rc);
113
* Create the threads and let them block on the event multi semaphore.
115
rc = RTSemEventMultiReset(g_hSemEM);
117
return PrintError("RTSemEventMultiReset failed (rc=%Rrc)\n", rc);
119
rc = RTThreadCreate(&Thread2, ThreadTest2, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2");
121
return PrintError("RTThreadCreate failed for thread 2 (rc=%Rrc)\n", rc);
124
rc = RTThreadCreate(&Thread1, ThreadTest1, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1");
126
return PrintError("RTThreadCreate failed for thread 1 (rc=%Rrc)\n", rc);
128
/* Force first thread (which has a timeout of 1 second) to timeout in the
129
* first wait, and the second wait will succeed. */
131
rc = RTSemEventMultiSignal(g_hSemEM);
133
PrintError("RTSemEventMultiSignal failed (rc=%Rrc)\n", rc);
135
rc = RTThreadWait(Thread1, 1000, NULL);
137
PrintError("RTThreadWait failed for thread 1 (rc=%Rrc)\n", rc);
139
rc = RTThreadWait(Thread2, 1000, NULL);
141
PrintError("RTThreadWait failed for thread 2 (rc=%Rrc)\n", rc);
143
rc = RTSemEventMultiDestroy(g_hSemEM);
145
PrintError("RTSemEventMultiDestroy failed - %Rrc\n", rc);
146
g_hSemEM = NIL_RTSEMEVENTMULTI;
153
int main(int argc, char **argv)
158
RTPrintf("tstSemEvent: RTR3Init failed (rc=%Rrc)\n", rc);
161
RTPrintf("tstSemEvent: TESTING...\n");
165
RTPrintf("tstSemEvent: SUCCESS\n");
167
RTPrintf("tstSemEvent: FAILURE - %u errors\n", g_cErrors);
168
return g_cErrors != 0;