00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _VPR_CondVarGeneric_h_
00043 #define _VPR_CondVarGeneric_h_
00044
00045 #include <vpr/vprConfig.h>
00046
00047 #include <iostream>
00048 #include <iomanip>
00049
00050 #ifdef VPR_OS_Win32
00051 # include <process.h>
00052 #endif
00053
00054 #include <sys/types.h>
00055
00056 #ifdef HAVE_UNISTD_H
00057 # include <unistd.h>
00058 #endif
00059
00060 #include <vpr/Sync/Semaphore.h>
00061 #include <vpr/Sync/Mutex.h>
00062 #include <vpr/Util/Debug.h>
00063 #include <vpr/Util/Interval.h>
00064
00065
00066 namespace vpr
00067 {
00068
00082 class VPR_CLASS_API CondVarGeneric
00083 {
00084 public:
00091 CondVarGeneric(Mutex* mutex = NULL)
00092 : mWaiters(0)
00093 , mCondMutex(NULL)
00094 {
00095 if ( mutex == NULL )
00096 {
00097 mutex = &mDefaultMutex;
00098 }
00099
00100 mCondMutex = mutex;
00101
00102 std::cerr << "------------------------------------\n"
00103 << " vpr::CondVarGeneric: DOES NOT WORK\n"
00104 << "------------------------------------\n";
00105 }
00106
00113 vpr::ReturnStatus wait(vpr::Interval time_to_wait = vpr::Interval::NoTimeout);
00114
00120 vpr::ReturnStatus signal()
00121 {
00122 std::cerr << std::setw(5) << getpid() << " Signal" << std::endl;
00123
00124 if ( mCondMutex->test() == 0 )
00125 {
00126 std::cerr << " vpr::CondVarGeneric::signal: Mutex was not locked when signal called!!!" << std::endl;
00127 }
00128
00129 if ( mWaiters > 0 )
00130 {
00131 return mSema.release();
00132 }
00133 else
00134 {
00135 return vpr::ReturnStatus();
00136 }
00137 }
00138
00143 vpr::ReturnStatus broadcast()
00144 {
00145
00146 if ( mCondMutex->test() == 0 )
00147 {
00148 std::cerr << " vpr::CondVarGeneric::broadcast: Mutex was not locked when broadcase called!!!" << std::endl;
00149 }
00150
00151 for ( int i = mWaiters;i>0;i-- )
00152 {
00153 mSema.release();
00154 }
00155
00156 return vpr::ReturnStatus();
00157 }
00158
00160 vpr::ReturnStatus acquire()
00161 {
00162 return mCondMutex->acquire();
00163 }
00164
00166 vpr::ReturnStatus tryAcquire()
00167 {
00168 return mCondMutex->tryAcquire();
00169 }
00170
00172 vpr::ReturnStatus release()
00173 {
00174 return mCondMutex->release();
00175 }
00176
00182 void setMutex(Mutex* mutex)
00183 {
00184 mutex->release();
00185 mCondMutex = mutex;
00186 }
00187
00189 int test()
00190 {
00191 return mCondMutex->test();
00192 }
00193
00194 void dump() const
00195 {
00196 vprDEBUG_BEGIN(vprDBG_ALL,0)
00197 << "------------- vpr::CondVarGeneric::Dump ---------\n"
00198 << vprDEBUG_FLUSH;
00199 vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL) << "mWaiters: "
00200 << mWaiters << std::endl
00201 << vprDEBUG_FLUSH;
00202 mCondMutex->dump();
00203 vprDEBUG_END(vprDBG_ALL,0) << "-----------------------------------\n"
00204 << vprDEBUG_FLUSH;
00205 }
00206
00207 private:
00208
00209 Semaphore mSema;
00210 long mWaiters;
00212 Mutex* mCondMutex;
00213 Mutex mDefaultMutex;
00215
00216 void operator= (const CondVarGeneric&)
00217 {
00218 ;
00219 }
00220
00221 CondVarGeneric(const CondVarGeneric& c)
00222 {
00223 ;
00224 }
00225 };
00226
00227 }
00228
00229
00230 #endif