changed: gcc8 base update
[opensg.git] / Source / Base / Threading / OSGCondVarTest.cpp
blobf7b4a16c92638aebed17db54eccd734cf23871d3
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
8 * *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11 * License *
12 * *
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
16 * *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 * *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
28 * Changes *
29 * *
30 * *
31 * *
32 * *
33 * *
34 * *
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 // Unit tests for vec classes
41 #include "OSGConfig.h"
42 #include "OSGBaseFunctions.h"
43 #include "OSGBaseInitFunctions.h"
44 #include "OSGCondVar.h"
45 #include "OSGLock.h"
46 #include "OSGLog.h"
47 #include "OSGBaseThread.h"
49 OSG_USING_NAMESPACE
51 struct condition_test_data
53 condition_test_data()
54 : notified(0)
55 , awoken(0)
57 mCondVar = OSG::CondVar::get(NULL, false);
58 mOutputLock = OSG::Lock ::get(NULL, false);
61 OSG::CondVarRefPtr mCondVar;
62 OSG::LockRefPtr mOutputLock;
63 int notified;
64 int awoken;
67 //#define OSG_CONDVAR_DEBUG
69 #ifdef OSG_CONDVAR_DEBUG
70 # define OUTPUT_BEGIN(data) \
71 (data)->mOutputLock->acquire(); \
72 PWARNING << OSG::BaseThread::getCurrent()->getCName()
74 # define OUTPUT_END(data) \
75 std::endl; \
76 (data)->mOutputLock->release();
77 #endif
79 void condition_test_thread(void* param)
81 condition_test_data* data = static_cast<condition_test_data*>(param);
83 #ifdef OSG_CONDVAR_DEBUG
84 OUTPUT_BEGIN(data) << ": Locking condvar" << OUTPUT_END(data);
85 #endif
86 data->mCondVar->acquire();
87 #ifdef OSG_CONDVAR_DEBUG
88 OUTPUT_BEGIN(data) << ": Locked condvar" << OUTPUT_END(data);
89 #endif
91 try
93 while (!(data->notified > 0))
95 #ifdef OSG_CONDVAR_DEBUG
96 OUTPUT_BEGIN(data) << ": waiting condvar notified: "
97 << data->notified << OUTPUT_END(data);
98 #endif
99 data->mCondVar->wait();
100 #ifdef OSG_CONDVAR_DEBUG
101 OUTPUT_BEGIN(data) << ": waited condvar notified: "
102 << data->notified << OUTPUT_END(data);
103 #endif
105 data->awoken++;
106 #ifdef OSG_CONDVAR_DEBUG
107 OUTPUT_BEGIN(data) << ": awoken: " << data->awoken << OUTPUT_END(data);
109 OUTPUT_BEGIN(data) << ": releasing condvar" << OUTPUT_END(data);
110 #endif
111 data->mCondVar->release();
112 #ifdef OSG_CONDVAR_DEBUG
113 OUTPUT_BEGIN(data) << ": released condvar" << OUTPUT_END(data);
114 #endif
116 catch(...)
118 #ifdef OSG_CONDVAR_DEBUG
119 OUTPUT_BEGIN(data) << ": releasing condvar" << OUTPUT_END(data);
120 #endif
121 data->mCondVar->release();
122 #ifdef OSG_CONDVAR_DEBUG
123 OUTPUT_BEGIN(data) << ": released condvar" << OUTPUT_END(data);
124 #endif
125 throw;
129 SUITE(CondVarTests)
132 TEST(TestSignal)
134 condition_test_data data;
136 OSG::BaseThreadRefPtr thread = OSG::BaseThread::get(NULL, false);
137 thread->runFunction(&condition_test_thread, &data);
138 OSG::osgSleep(250);
140 #ifdef OSG_CONDVAR_DEBUG
141 OUTPUT_BEGIN(&data) << ": Locking condvar" << OUTPUT_END(&data);
142 #endif
143 data.mCondVar->acquire();
144 #ifdef OSG_CONDVAR_DEBUG
145 OUTPUT_BEGIN(&data) << ": Locked condvar" << OUTPUT_END(&data);
146 #endif
148 data.notified++;
149 #ifdef OSG_CONDVAR_DEBUG
150 OUTPUT_BEGIN(&data) << ": notified: " << data.notified << OUTPUT_END(&data);
152 OUTPUT_BEGIN(&data) << ": signaling condvar" << OUTPUT_END(&data);
153 #endif
154 data.mCondVar->signal();
155 #ifdef OSG_CONDVAR_DEBUG
156 OUTPUT_BEGIN(&data) << ": signaled condvar" << OUTPUT_END(&data);
158 OUTPUT_BEGIN(&data) << ": releasing condvar" << OUTPUT_END(&data);
159 #endif
160 data.mCondVar->release();
161 #ifdef OSG_CONDVAR_DEBUG
162 OUTPUT_BEGIN(&data) << ": released condvar" << OUTPUT_END(&data);
163 #endif
166 OSG::BaseThread::join(thread);
167 CHECK_EQUAL(1, data.awoken);
170 TEST(TestBroadcast)
172 const int NUMTHREADS = 5;
173 std::vector<OSG::BaseThreadRefPtr> threads;
174 condition_test_data data;
176 for (int i = 0; i < NUMTHREADS; ++i)
178 OSG::BaseThreadRefPtr thread = OSG::BaseThread::get(NULL, false);
179 thread->runFunction(&condition_test_thread, &data);
180 threads.push_back(thread);
183 OSG::osgSleep(250);
185 #ifdef OSG_CONDVAR_DEBUG
186 OUTPUT_BEGIN(&data) << ": Locking condvar" << OUTPUT_END(&data);
187 #endif
188 data.mCondVar->acquire();
189 #ifdef OSG_CONDVAR_DEBUG
190 OUTPUT_BEGIN(&data) << ": Locked condvar" << OUTPUT_END(&data);
191 #endif
193 data.notified++;
194 #ifdef OSG_CONDVAR_DEBUG
195 OUTPUT_BEGIN(&data) << ": notified: " << data.notified << OUTPUT_END(&data);
197 OUTPUT_BEGIN(&data) << ": broadcast condvar" << OUTPUT_END(&data);
198 #endif
199 data.mCondVar->broadcast();
200 #ifdef OSG_CONDVAR_DEBUG
201 OUTPUT_BEGIN(&data) << ": broadcast condvar done" << OUTPUT_END(&data);
203 OUTPUT_BEGIN(&data) << ": releasing condvar" << OUTPUT_END(&data);
204 #endif
205 data.mCondVar->release();
206 #ifdef OSG_CONDVAR_DEBUG
207 OUTPUT_BEGIN(&data) << ": released condvar" << OUTPUT_END(&data);
208 #endif
211 for (int i = 0; i < NUMTHREADS; ++i)
213 OSG::BaseThread::join(threads[i]);
216 CHECK_EQUAL(NUMTHREADS, data.awoken);
219 } // SUITE