1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
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"
47 #include "OSGBaseThread.h"
51 struct condition_test_data
57 mCondVar
= OSG::CondVar::get(NULL
, false);
58 mOutputLock
= OSG::Lock ::get(NULL
, false);
61 OSG::CondVarRefPtr mCondVar
;
62 OSG::LockRefPtr mOutputLock
;
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) \
76 (data)->mOutputLock->release();
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
);
86 data
->mCondVar
->acquire();
87 #ifdef OSG_CONDVAR_DEBUG
88 OUTPUT_BEGIN(data
) << ": Locked condvar" << OUTPUT_END(data
);
93 while (!(data
->notified
> 0))
95 #ifdef OSG_CONDVAR_DEBUG
96 OUTPUT_BEGIN(data
) << ": waiting condvar notified: "
97 << data
->notified
<< OUTPUT_END(data
);
99 data
->mCondVar
->wait();
100 #ifdef OSG_CONDVAR_DEBUG
101 OUTPUT_BEGIN(data
) << ": waited condvar notified: "
102 << data
->notified
<< OUTPUT_END(data
);
106 #ifdef OSG_CONDVAR_DEBUG
107 OUTPUT_BEGIN(data
) << ": awoken: " << data
->awoken
<< OUTPUT_END(data
);
109 OUTPUT_BEGIN(data
) << ": releasing condvar" << OUTPUT_END(data
);
111 data
->mCondVar
->release();
112 #ifdef OSG_CONDVAR_DEBUG
113 OUTPUT_BEGIN(data
) << ": released condvar" << OUTPUT_END(data
);
118 #ifdef OSG_CONDVAR_DEBUG
119 OUTPUT_BEGIN(data
) << ": releasing condvar" << OUTPUT_END(data
);
121 data
->mCondVar
->release();
122 #ifdef OSG_CONDVAR_DEBUG
123 OUTPUT_BEGIN(data
) << ": released condvar" << OUTPUT_END(data
);
134 condition_test_data data
;
136 OSG::BaseThreadRefPtr thread
= OSG::BaseThread::get(NULL
, false);
137 thread
->runFunction(&condition_test_thread
, &data
);
140 #ifdef OSG_CONDVAR_DEBUG
141 OUTPUT_BEGIN(&data
) << ": Locking condvar" << OUTPUT_END(&data
);
143 data
.mCondVar
->acquire();
144 #ifdef OSG_CONDVAR_DEBUG
145 OUTPUT_BEGIN(&data
) << ": Locked condvar" << OUTPUT_END(&data
);
149 #ifdef OSG_CONDVAR_DEBUG
150 OUTPUT_BEGIN(&data
) << ": notified: " << data
.notified
<< OUTPUT_END(&data
);
152 OUTPUT_BEGIN(&data
) << ": signaling condvar" << OUTPUT_END(&data
);
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
);
160 data
.mCondVar
->release();
161 #ifdef OSG_CONDVAR_DEBUG
162 OUTPUT_BEGIN(&data
) << ": released condvar" << OUTPUT_END(&data
);
166 OSG::BaseThread::join(thread
);
167 CHECK_EQUAL(1, data
.awoken
);
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
);
185 #ifdef OSG_CONDVAR_DEBUG
186 OUTPUT_BEGIN(&data
) << ": Locking condvar" << OUTPUT_END(&data
);
188 data
.mCondVar
->acquire();
189 #ifdef OSG_CONDVAR_DEBUG
190 OUTPUT_BEGIN(&data
) << ": Locked condvar" << OUTPUT_END(&data
);
194 #ifdef OSG_CONDVAR_DEBUG
195 OUTPUT_BEGIN(&data
) << ": notified: " << data
.notified
<< OUTPUT_END(&data
);
197 OUTPUT_BEGIN(&data
) << ": broadcast condvar" << OUTPUT_END(&data
);
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
);
205 data
.mCondVar
->release();
206 #ifdef OSG_CONDVAR_DEBUG
207 OUTPUT_BEGIN(&data
) << ": released condvar" << OUTPUT_END(&data
);
211 for (int i
= 0; i
< NUMTHREADS
; ++i
)
213 OSG::BaseThread::join(threads
[i
]);
216 CHECK_EQUAL(NUMTHREADS
, data
.awoken
);