Files
pip/src_concurrent/test/testutil.h
Фоменко Степан Владимирович b313a9c9f2 Added inter thread communication lib
git-svn-id: svn://db.shs.com.ru/pip@858 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
2019-10-02 15:47:21 +00:00

61 lines
1.9 KiB
C++

//
// Created by fomenko on 27.09.2019.
//
#ifndef AWRCANFLASHER_TESTUTIL_H
#define AWRCANFLASHER_TESTUTIL_H
#include "pithread.h"
/**
* Minimum wait thread start, switch context or another interthread communication action time. Increase it if tests
* write "Start thread timeout reach!" message. You can reduce it if you want increase test performance.
*/
const int WAIT_THREAD_TIME_MS = 40;
const int THREAD_COUNT = 5;
class TestUtil: public PIObject {
PIOBJECT(TestUtil)
public:
double threadStartTime;
PIThread* thread = new PIThread();
StdFunctionThreadFuncAdapter* adapter = nullptr;
volatile bool isRunning;
std::function<void()> adapterFunctionDefault;
bool createThread(const std::function<void()>& fun = nullptr, PIThread* thread_ = nullptr) {
adapter = new StdFunctionThreadFuncAdapter(fun == nullptr ? adapterFunctionDefault : fun);
if (thread_ == nullptr) thread_ = thread;
adapter->registerToInvoke(thread_);
thread_->startOnce();
return waitThread(thread_);
}
bool waitThread(PIThread* thread_, bool runningStatus = true) {
PITimeMeasurer measurer;
bool isTimeout = !thread_->waitForStart(WAIT_THREAD_TIME_MS);
while (!isRunning) {
isTimeout = WAIT_THREAD_TIME_MS <= measurer.elapsed_m();
if (isTimeout) break;
piMSleep(1);
}
threadStartTime = measurer.elapsed_m();
if (isTimeout) piCout << "Start thread timeout reach!";
if (threadStartTime > 1) {
piCout << "Start time" << threadStartTime << "ms";
} else if (threadStartTime > 0.001) {
piCout << "Start time" << threadStartTime * 1000 << "mcs";
} else {
piCout << "Start time" << threadStartTime * 1000 * 1000 << "ns";
}
return !isTimeout;
}
};
#endif //AWRCANFLASHER_TESTUTIL_H