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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THREADINGUTIL_H_
#define THREADINGUTIL_H_
#include <vector>
#ifdef THREADED
#include "pthread.h"
#endif
// *****************************************************************************
// Threading primitives
// atomic post-increment; returns the previous value of the operand
int32_t atomic_post_incr(volatile int32_t* operand, int32_t incr);
// atomic fetch&store; returns the previous value of the operand
int32_t atomic_fetch_store(volatile int32_t *operand, int32_t value);
// a partial implementation of an atomic integer type
class AtomicInt{
public:
explicit AtomicInt(int32_t init=0):v_(init){}
AtomicInt(const AtomicInt& other):v_(other){}
// assigment
AtomicInt& operator=(const AtomicInt& lhs){
atomic_fetch_store(&v_,lhs);
return *this;
}
AtomicInt& operator=(int32_t i){
atomic_fetch_store(&v_,i);
return *this;
}
// pre-increment
AtomicInt& operator++() {
atomic_post_incr(&v_,1);
return *this;
}
// pre-decrement
AtomicInt& operator--() {
atomic_post_incr(&v_,-1);
return *this;
}
// post-increment
AtomicInt operator++(int){
return AtomicInt(atomic_post_incr(&v_,1));
}
// post-decrement
AtomicInt operator--(int){
return AtomicInt(atomic_post_incr(&v_,-1));
}
operator int() const{
return atomic_post_incr(&v_,0);
}
int get() const{
return atomic_post_incr(&v_,0);
}
private:
mutable int32_t v_;
};
#ifdef THREADED
// ****************************************************************************
#define VALIDATE_JOBS(jm) jm.validateJobs(__FILE__,__LINE__)
#define VALIDATE_JOB(j) j.validate(__FILE__,__LINE__)
class Mutex{
public:
Mutex();
~Mutex();
void acquire();
void release();
private:
Mutex(const Mutex&);
Mutex& operator=(const Mutex&);
struct Impl;
Impl* impl_;
};
class MTLock{
public:
MTLock(Mutex& m):m_(m){m.acquire();}
~MTLock(){m_.release();}
Mutex& m_;
};
#define synchronized(m) MTLock __lock(m)
// ****************************************************************************
class Latch {
public:
virtual ~Latch() {}
virtual void await() const =0;
virtual void signalAndWait() =0;
virtual void signal() =0;
};
class CountDownLatch: public Latch {
public:
CountDownLatch(int count):count_(count) {
pthread_cond_init(&cond_,0);
pthread_mutex_init(&mut_,0);
}
virtual ~CountDownLatch() {
pthread_mutex_lock(&mut_);
if(count_!=0) {
count_=0;
pthread_cond_broadcast(&cond_);
}
pthread_mutex_unlock(&mut_);
pthread_cond_destroy(&cond_);
pthread_mutex_destroy(&mut_);
}
virtual void await() const {
pthread_mutex_lock(&mut_);
awaitImpl();
pthread_mutex_unlock(&mut_);
}
virtual void signalAndWait() {
pthread_mutex_lock(&mut_);
signalImpl();
awaitImpl();
pthread_mutex_unlock(&mut_);
}
virtual void signal() {
pthread_mutex_lock(&mut_);
signalImpl();
pthread_mutex_unlock(&mut_);
}
private:
void awaitImpl() const{
while(count_!=0)
pthread_cond_wait(&cond_,&mut_);
}
void signalImpl() {
if(count_>0) {
count_--;
pthread_cond_broadcast(&cond_);
}
}
int count_;
mutable pthread_mutex_t mut_;
mutable pthread_cond_t cond_;
};
class TestJob {
public:
typedef long JobId;
TestJob():hasRun_(false),startLatch_(0),endLatch_(0) {}
virtual ~TestJob() {
join();
}
virtual TestJob* clone() const =0;
virtual void run() =0;
virtual void validate(const char* file, int line) const =0;
virtual void start(Latch* startLatch=0,Latch* endLatch=0) {
startLatch_=startLatch;endLatch_=endLatch;
hasRun_=true;
pthread_create(&thread_, 0, thread, this);
}
virtual JobId getJobId() const {
return (JobId)thread_;
}
virtual void join() {
if(!hasRun_)
return;
if(!pthread_equal(thread_,pthread_self()))
pthread_join(thread_,0);
else
pthread_detach(thread_);
}
private:
void awaitStart() {
if(startLatch_==0) return;
startLatch_->signalAndWait();
}
void signalFinished() {
if(endLatch_==0) return;
endLatch_->signal();
}
static void* thread(void* p) {
TestJob* j=(TestJob*)p;
j->awaitStart(); // wait for the start command
j->run();
j->signalFinished();
return 0;
}
bool hasRun_;
Latch* startLatch_;
Latch* endLatch_;
pthread_t thread_;
};
class TestJobManager {
typedef std::vector<TestJob*> JobList;
public:
TestJobManager(const TestJob& tj,int threadCount=1):
startLatch_(threadCount),endLatch_(threadCount)
{
for(int i=0;i<threadCount;++i)
jobs_.push_back(tj.clone());
}
virtual ~TestJobManager(){
for(unsigned i=0;i<jobs_.size();++i)
delete jobs_[i];
}
virtual void startAllJobs() {
for(unsigned i=0;i<jobs_.size();++i)
jobs_[i]->start(&startLatch_,&endLatch_);
}
virtual void startJobsImmediately() {
for(unsigned i=0;i<jobs_.size();++i)
jobs_[i]->start(0,&endLatch_);
}
virtual void wait() const {
endLatch_.await();
}
virtual void validateJobs(const char* file, int line) const{
for(unsigned i=0;i<jobs_.size();++i)
jobs_[i]->validate(file,line);
}
private:
JobList jobs_;
CountDownLatch startLatch_;
CountDownLatch endLatch_;
};
#else // THREADED
// single THREADED
class Mutex{
public:
void acquire(){}
void release(){}
};
#define synchronized(m)
#endif // THREADED
#endif /*THREADINGUTIL_H_*/
|