| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
#include <iostream> |
|---|
| 35 |
#include <stdio.h> |
|---|
| 36 |
#include <ulocks.h> |
|---|
| 37 |
#include <math.h> |
|---|
| 38 |
#include <time.h> |
|---|
| 39 |
#include <sys/time.h> |
|---|
| 40 |
|
|---|
| 41 |
#include <Threads/vjThread.h> |
|---|
| 42 |
#include <Sync/vjCond.h> |
|---|
| 43 |
#include <Kernel/vjDebug.h> |
|---|
| 44 |
|
|---|
| 45 |
class SyncIncrementer |
|---|
| 46 |
{ |
|---|
| 47 |
public: |
|---|
| 48 |
SyncIncrementer() : value(0), go(false) |
|---|
| 49 |
{;} |
|---|
| 50 |
|
|---|
| 51 |
int start(); |
|---|
| 52 |
void trigger(); |
|---|
| 53 |
void sync(); |
|---|
| 54 |
void main(void* nullParam); |
|---|
| 55 |
|
|---|
| 56 |
void incValue() |
|---|
| 57 |
{ value++;} |
|---|
| 58 |
void decValue() |
|---|
| 59 |
{ value--;} |
|---|
| 60 |
int getValue() |
|---|
| 61 |
{ return value; } |
|---|
| 62 |
|
|---|
| 63 |
private: |
|---|
| 64 |
vjCond syncCond; |
|---|
| 65 |
bool go; |
|---|
| 66 |
int value; |
|---|
| 67 |
}; |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
int main(void) |
|---|
| 74 |
{ |
|---|
| 75 |
SyncIncrementer syncer; |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
syncer.start(); |
|---|
| 79 |
|
|---|
| 80 |
while (1) |
|---|
| 81 |
{ |
|---|
| 82 |
std::cerr << std::setw(5) << vjThread::self() << "P1: Before Trigger" |
|---|
| 83 |
<< std::endl; |
|---|
| 84 |
|
|---|
| 85 |
vjDEBUG(vjDBG_ALL, 0) << "main: trigger\n" << vjDEBUG_FLUSH; |
|---|
| 86 |
syncer.trigger(); |
|---|
| 87 |
vjDEBUG(vjDBG_ALL, 3) << "main: trigger done\n" << vjDEBUG_FLUSH; |
|---|
| 88 |
|
|---|
| 89 |
std::cerr << std::setw(5) << vjThread::self() << "P1: Between" |
|---|
| 90 |
<< std::endl; |
|---|
| 91 |
|
|---|
| 92 |
vjDEBUG(vjDBG_ALL, 0) << "main: sync up\n" << vjDEBUG_FLUSH; |
|---|
| 93 |
syncer.sync(); |
|---|
| 94 |
vjDEBUG(vjDBG_ALL, 0) << "main: sync done\n" << vjDEBUG_FLUSH; |
|---|
| 95 |
|
|---|
| 96 |
syncer.decValue(); |
|---|
| 97 |
vjDEBUG(vjDBG_ALL, 0) << "VAL: " << syncer.getValue() << std::endl |
|---|
| 98 |
<< vjDEBUG_FLUSH; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
return 1; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
int SyncIncrementer::start() |
|---|
| 111 |
{ |
|---|
| 112 |
|
|---|
| 113 |
vjThreadMemberFunctor<SyncIncrementer>* memberFunctor = |
|---|
| 114 |
new vjThreadMemberFunctor<SyncIncrementer>(this, &SyncIncrementer::main, NULL); |
|---|
| 115 |
|
|---|
| 116 |
vjThread* control_thread = new vjThread(memberFunctor, 0); |
|---|
| 117 |
|
|---|
| 118 |
vjDEBUG(vjDBG_ALL, 0) << "SyncIncrementer::start: Just started main loop. " |
|---|
| 119 |
<< control_thread << std::endl << vjDEBUG_FLUSH; |
|---|
| 120 |
|
|---|
| 121 |
return 1; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
void SyncIncrementer::trigger() |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
syncCond.acquire(); |
|---|
| 130 |
{ |
|---|
| 131 |
std::cerr << std::setw(5) << vjThread::self() << " P1: Signal" |
|---|
| 132 |
<< std::endl; |
|---|
| 133 |
|
|---|
| 134 |
go = true; |
|---|
| 135 |
syncCond.signal(); |
|---|
| 136 |
vjDEBUG(vjDBG_ALL, 0) << "Trigger signaled\n" << vjDEBUG_FLUSH; |
|---|
| 137 |
|
|---|
| 138 |
} |
|---|
| 139 |
syncCond.release(); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
void SyncIncrementer::sync() |
|---|
| 144 |
{ |
|---|
| 145 |
syncCond.acquire(); |
|---|
| 146 |
{ |
|---|
| 147 |
while (go == true) |
|---|
| 148 |
{ |
|---|
| 149 |
std::cerr << std::setw(5) << vjThread::self() << " P1: Wait" |
|---|
| 150 |
<< std::endl; |
|---|
| 151 |
syncCond.wait(); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
vjDEBUG(vjDBG_ALL, 0) << "Sync: Completed. trigger == false\n" |
|---|
| 155 |
<< vjDEBUG_FLUSH; |
|---|
| 156 |
|
|---|
| 157 |
std::cerr << std::setw(5) << vjThread::self() << " P1: Exit wait" |
|---|
| 158 |
<< std::endl; |
|---|
| 159 |
} |
|---|
| 160 |
syncCond.release(); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
void SyncIncrementer::main(void* nullParam) { |
|---|
| 166 |
while (1) |
|---|
| 167 |
{ |
|---|
| 168 |
syncCond.acquire(); |
|---|
| 169 |
{ |
|---|
| 170 |
vjDEBUG(vjDBG_ALL, 0) << "Wait for trigger\n" << vjDEBUG_FLUSH; |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
while (go == false) |
|---|
| 174 |
{ |
|---|
| 175 |
std::cerr << std::setw(5) << vjThread::self() << " P2: Wait" |
|---|
| 176 |
<< std::endl; |
|---|
| 177 |
syncCond.wait(); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
std::cerr << std::setw(5) << vjThread::self() << " P2: Wait done" |
|---|
| 181 |
<< std::endl; |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
vjDEBUG(vjDBG_ALL, 0) << "Incrementing\n" << vjDEBUG_FLUSH; |
|---|
| 185 |
|
|---|
| 186 |
incValue(); |
|---|
| 187 |
|
|---|
| 188 |
vjDEBUG(vjDBG_ALL, 0) << "Var Incremented - Set trigger FALSE and SIGNAL\n" |
|---|
| 189 |
<< vjDEBUG_FLUSH; |
|---|
| 190 |
|
|---|
| 191 |
go = false; |
|---|
| 192 |
|
|---|
| 193 |
std::cerr << std::setw(5) << vjThread::self() << " P2: Signal" |
|---|
| 194 |
<< std::endl; |
|---|
| 195 |
syncCond.signal(); |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
} |
|---|
| 199 |
syncCond.release(); |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|