fix concurrent
git-svn-id: svn://db.shs.com.ru/pip@884 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
//
|
||||
// Created by fomenko on 23.09.2019.
|
||||
//
|
||||
|
||||
#include "executor.h"
|
||||
|
||||
PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeue<std::function<void()>> *taskQueue_,
|
||||
PIThreadFactory *threadFactory) : isShutdown_(false), taskQueue(taskQueue_), threadFactory(threadFactory) {
|
||||
|
||||
PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeue<std::function<void()>> *taskQueue_) : isShutdown_(false), taskQueue(taskQueue_) {
|
||||
for (size_t i = 0; i < corePoolSize; ++i) {
|
||||
AbstractThread* thread = threadFactory->newThread([&, i](){
|
||||
PIThread * thread = new PIThread([&, i](){
|
||||
auto runnable = taskQueue->poll(100, std::function<void()>());
|
||||
if (runnable) {
|
||||
runnable();
|
||||
@@ -19,6 +15,7 @@ PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeu
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PIThreadPoolExecutor::awaitTermination(int timeoutMs) {
|
||||
PITimeMeasurer measurer;
|
||||
for (size_t i = 0; i < threadPool.size(); ++i) {
|
||||
@@ -29,26 +26,30 @@ bool PIThreadPoolExecutor::awaitTermination(int timeoutMs) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void PIThreadPoolExecutor::shutdownNow() {
|
||||
isShutdown_ = true;
|
||||
for (size_t i = 0; i < threadPool.size(); ++i) threadPool[i]->stop();
|
||||
}
|
||||
|
||||
|
||||
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
||||
shutdownNow();
|
||||
while (threadPool.size() > 0) delete threadPool.take_back();
|
||||
delete threadFactory;
|
||||
delete taskQueue;
|
||||
}
|
||||
|
||||
|
||||
void PIThreadPoolExecutor::execute(const std::function<void()> &runnable) {
|
||||
if (!isShutdown_) taskQueue->offer(runnable);
|
||||
}
|
||||
|
||||
|
||||
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
||||
return isShutdown_;
|
||||
}
|
||||
|
||||
|
||||
void PIThreadPoolExecutor::shutdown() {
|
||||
isShutdown_ = true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//
|
||||
// Created by fomenko on 25.09.2019.
|
||||
//
|
||||
|
||||
#include "piconditionlock.h"
|
||||
#ifdef WINDOWS
|
||||
#include "synchapi.h"
|
||||
@@ -9,6 +5,7 @@
|
||||
#include "pthread.h"
|
||||
#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIConditionLock)
|
||||
#ifdef WINDOWS
|
||||
CRITICAL_SECTION
|
||||
@@ -18,33 +15,39 @@ PRIVATE_DEFINITION_START(PIConditionLock)
|
||||
nativeHandle;
|
||||
PRIVATE_DEFINITION_END(PIConditionLock)
|
||||
|
||||
|
||||
#ifdef WINDOWS
|
||||
PIConditionLock::PIConditionLock() {
|
||||
InitializeCriticalSection(&PRIVATE->nativeHandle);
|
||||
}
|
||||
|
||||
|
||||
PIConditionLock::~PIConditionLock() {
|
||||
DeleteCriticalSection(&PRIVATE->nativeHandle);
|
||||
}
|
||||
|
||||
|
||||
void PIConditionLock::lock() {
|
||||
EnterCriticalSection(&PRIVATE->nativeHandle);
|
||||
}
|
||||
|
||||
|
||||
void PIConditionLock::unlock() {
|
||||
LeaveCriticalSection(&PRIVATE->nativeHandle);
|
||||
}
|
||||
|
||||
|
||||
void *PIConditionLock::handle() {
|
||||
return &PRIVATE->nativeHandle;
|
||||
}
|
||||
|
||||
|
||||
bool PIConditionLock::tryLock() {
|
||||
return TryEnterCriticalSection(&PRIVATE->nativeHandle) != 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
|
||||
PIConditionLock::PIConditionLock() {
|
||||
pthread_mutexattr_t attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
@@ -55,24 +58,29 @@ PIConditionLock::PIConditionLock() {
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
}
|
||||
|
||||
|
||||
PIConditionLock::~PIConditionLock() {
|
||||
pthread_mutex_destroy(&(PRIVATE->nativeHandle));
|
||||
}
|
||||
|
||||
|
||||
void PIConditionLock::lock() {
|
||||
pthread_mutex_lock(&(PRIVATE->nativeHandle));
|
||||
}
|
||||
|
||||
|
||||
void PIConditionLock::unlock() {
|
||||
pthread_mutex_unlock(&(PRIVATE->nativeHandle));
|
||||
}
|
||||
|
||||
|
||||
void *PIConditionLock::handle() {
|
||||
return &PRIVATE->nativeHandle;
|
||||
}
|
||||
|
||||
|
||||
bool PIConditionLock::tryLock() {
|
||||
return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0);;
|
||||
return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
//
|
||||
// Created by fomenko on 20.09.2019.
|
||||
//
|
||||
|
||||
#include "piplatform.h"
|
||||
#include "piconditionvar.h"
|
||||
#include "pithread.h"
|
||||
#include "pitime.h"
|
||||
|
||||
#ifdef WINDOWS
|
||||
#define _WIN32_WINNT 0x0600
|
||||
@@ -11,9 +10,6 @@
|
||||
#include <winbase.h>
|
||||
#endif
|
||||
|
||||
#include "piconditionvar.h"
|
||||
#include "pithread.h"
|
||||
#include "pitime.h"
|
||||
|
||||
PRIVATE_DEFINITION_START(PIConditionVariable)
|
||||
#ifdef WINDOWS
|
||||
@@ -25,6 +21,7 @@ PRIVATE_DEFINITION_START(PIConditionVariable)
|
||||
bool isDestroying;
|
||||
PRIVATE_DEFINITION_END(PIConditionVariable)
|
||||
|
||||
|
||||
PIConditionVariable::PIConditionVariable() {
|
||||
#ifdef WINDOWS
|
||||
InitializeConditionVariable(&PRIVATE->nativeHandle);
|
||||
@@ -36,14 +33,15 @@ PIConditionVariable::PIConditionVariable() {
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
PIConditionVariable::~PIConditionVariable() {
|
||||
#ifdef WINDOWS
|
||||
#else
|
||||
pthread_cond_destroy(&PRIVATE->nativeHandle);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
void PIConditionVariable::wait(PIConditionLock& lk) {
|
||||
#ifdef WINDOWS
|
||||
SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), INFINITE);
|
||||
@@ -52,6 +50,7 @@ void PIConditionVariable::wait(PIConditionLock& lk) {
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>& condition) {
|
||||
bool isCondition;
|
||||
while (true) {
|
||||
@@ -66,6 +65,7 @@ void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>&
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
||||
bool isNotTimeout;
|
||||
#ifdef WINDOWS
|
||||
@@ -78,6 +78,7 @@ bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
||||
return isNotTimeout;
|
||||
}
|
||||
|
||||
|
||||
bool PIConditionVariable::waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()> &condition) {
|
||||
bool isCondition;
|
||||
PITimeMeasurer measurer;
|
||||
@@ -109,6 +110,8 @@ void PIConditionVariable::notifyOne() {
|
||||
pthread_cond_signal(&PRIVATE->nativeHandle);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIConditionVariable::notifyAll() {
|
||||
#ifdef WINDOWS
|
||||
WakeAllConditionVariable(&PRIVATE->nativeHandle);
|
||||
@@ -117,12 +120,3 @@ void PIConditionVariable::notifyAll() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void StdFunctionThreadFuncAdapter::threadFuncStdFunctionAdapter(void *it) {
|
||||
auto consumer = (StdFunctionThreadFuncAdapter*)it;
|
||||
consumer->fun();
|
||||
}
|
||||
|
||||
void StdFunctionThreadFuncAdapter::registerToInvoke(PIThread *thread) {
|
||||
thread->setData(data());
|
||||
thread->setSlot((ThreadFunc) threadFunc());
|
||||
}
|
||||
|
||||
@@ -5,46 +5,8 @@
|
||||
#ifndef PIP_TESTS_EXECUTOR_H
|
||||
#define PIP_TESTS_EXECUTOR_H
|
||||
|
||||
#include <pithread.h>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include "piblockingdequeue.h"
|
||||
|
||||
class AbstractThread {
|
||||
public:
|
||||
virtual bool start() = 0;
|
||||
virtual bool waitForStart(int timeout_msecs) = 0;
|
||||
virtual bool waitForFinish(int timeout_msecs) = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual ~AbstractThread() = default;
|
||||
};
|
||||
|
||||
class Thread : public AbstractThread {
|
||||
public:
|
||||
explicit Thread(const std::function<void()>& fun = [](){}) : adapter(fun) {
|
||||
adapter.registerToInvoke(&thread);
|
||||
}
|
||||
virtual ~Thread() = default;
|
||||
|
||||
inline bool start() override { return thread.start(); }
|
||||
inline bool waitForStart(int timeout_msecs) override { return thread.waitForStart(timeout_msecs); }
|
||||
inline bool waitForFinish(int timeout_msecs) override { return thread.waitForFinish(timeout_msecs); }
|
||||
inline void stop() override { thread.stop(); }
|
||||
|
||||
private:
|
||||
PIThread thread;
|
||||
StdFunctionThreadFuncAdapter adapter;
|
||||
};
|
||||
|
||||
class PIThreadFactory {
|
||||
public:
|
||||
inline virtual AbstractThread* newThread(const std::function<void()>& fun) {
|
||||
return new Thread(fun);
|
||||
}
|
||||
virtual ~PIThreadFactory() = default;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Thread pools address two different problems: they usually provide improved performance when executing large
|
||||
* numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and
|
||||
@@ -52,7 +14,7 @@ public:
|
||||
*/
|
||||
class PIThreadPoolExecutor {
|
||||
public:
|
||||
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >(), PIThreadFactory* threadFactory = new PIThreadFactory());
|
||||
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >());
|
||||
|
||||
virtual ~PIThreadPoolExecutor();
|
||||
|
||||
@@ -80,8 +42,7 @@ public:
|
||||
private:
|
||||
volatile bool isShutdown_;
|
||||
PIBlockingDequeue<std::function<void()> >* taskQueue;
|
||||
PIThreadFactory* threadFactory;
|
||||
PIVector<AbstractThread*> threadPool;
|
||||
PIVector<PIThread*> threadPool;
|
||||
};
|
||||
|
||||
#endif //PIP_TESTS_EXECUTOR_H
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
//
|
||||
// Created by fomenko on 25.09.2019.
|
||||
//
|
||||
|
||||
#ifndef AWRCANFLASHER_PICONDITIONLOCK_H
|
||||
#define AWRCANFLASHER_PICONDITIONLOCK_H
|
||||
|
||||
#include <pimutex.h>
|
||||
#include <piinit.h>
|
||||
#include "pimutex.h"
|
||||
|
||||
|
||||
/**
|
||||
* @brief Continued
|
||||
@@ -16,18 +12,17 @@ public:
|
||||
explicit PIConditionLock();
|
||||
virtual ~PIConditionLock();
|
||||
|
||||
/**
|
||||
* @brief lock
|
||||
*/
|
||||
//! \brief lock
|
||||
void lock();
|
||||
|
||||
/**
|
||||
* @brief unlock
|
||||
*/
|
||||
//! \brief unlock
|
||||
void unlock();
|
||||
|
||||
//! \brief tryLock
|
||||
bool tryLock();
|
||||
|
||||
void * handle();
|
||||
|
||||
private:
|
||||
NO_COPY_CLASS(PIConditionLock)
|
||||
PRIVATE_DECLARATION
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
//
|
||||
// Created by fomenko on 20.09.2019.
|
||||
//
|
||||
|
||||
#ifndef PIP_TESTS_PICONDITIONVAR_H
|
||||
#define PIP_TESTS_PICONDITIONVAR_H
|
||||
|
||||
@@ -9,6 +5,7 @@
|
||||
#include "pithread.h"
|
||||
#include "piinit.h"
|
||||
|
||||
|
||||
/**
|
||||
* @brief A condition variable is an object able to block the calling thread until notified to resume.
|
||||
*
|
||||
@@ -94,26 +91,11 @@ public:
|
||||
* @return false if timeout reached or true if wakeup condition is true
|
||||
*/
|
||||
virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()>& condition);
|
||||
|
||||
private:
|
||||
NO_COPY_CLASS(PIConditionVariable)
|
||||
|
||||
PRIVATE_DECLARATION
|
||||
};
|
||||
|
||||
|
||||
|
||||
// FIXME: remove that!
|
||||
class StdFunctionThreadFuncAdapter {
|
||||
public:
|
||||
static void threadFuncStdFunctionAdapter(void* it);
|
||||
|
||||
explicit StdFunctionThreadFuncAdapter(const std::function<void()>& fun_): fun(fun_) {}
|
||||
|
||||
void registerToInvoke(PIThread* thread);
|
||||
void* data() const { return (void*)this; }
|
||||
ThreadFunc threadFunc() const { return threadFuncStdFunctionAdapter; }
|
||||
private:
|
||||
std::function<void()> fun;
|
||||
};
|
||||
|
||||
#endif //PIP_TESTS_PICONDITIONVAR_H
|
||||
|
||||
Reference in New Issue
Block a user