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"
|
#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) {
|
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()>());
|
auto runnable = taskQueue->poll(100, std::function<void()>());
|
||||||
if (runnable) {
|
if (runnable) {
|
||||||
runnable();
|
runnable();
|
||||||
@@ -19,6 +15,7 @@ PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIThreadPoolExecutor::awaitTermination(int timeoutMs) {
|
bool PIThreadPoolExecutor::awaitTermination(int timeoutMs) {
|
||||||
PITimeMeasurer measurer;
|
PITimeMeasurer measurer;
|
||||||
for (size_t i = 0; i < threadPool.size(); ++i) {
|
for (size_t i = 0; i < threadPool.size(); ++i) {
|
||||||
@@ -29,26 +26,30 @@ bool PIThreadPoolExecutor::awaitTermination(int timeoutMs) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIThreadPoolExecutor::shutdownNow() {
|
void PIThreadPoolExecutor::shutdownNow() {
|
||||||
isShutdown_ = true;
|
isShutdown_ = true;
|
||||||
for (size_t i = 0; i < threadPool.size(); ++i) threadPool[i]->stop();
|
for (size_t i = 0; i < threadPool.size(); ++i) threadPool[i]->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
||||||
shutdownNow();
|
shutdownNow();
|
||||||
while (threadPool.size() > 0) delete threadPool.take_back();
|
while (threadPool.size() > 0) delete threadPool.take_back();
|
||||||
delete threadFactory;
|
|
||||||
delete taskQueue;
|
delete taskQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIThreadPoolExecutor::execute(const std::function<void()> &runnable) {
|
void PIThreadPoolExecutor::execute(const std::function<void()> &runnable) {
|
||||||
if (!isShutdown_) taskQueue->offer(runnable);
|
if (!isShutdown_) taskQueue->offer(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
||||||
return isShutdown_;
|
return isShutdown_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIThreadPoolExecutor::shutdown() {
|
void PIThreadPoolExecutor::shutdown() {
|
||||||
isShutdown_ = true;
|
isShutdown_ = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
//
|
|
||||||
// Created by fomenko on 25.09.2019.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "piconditionlock.h"
|
#include "piconditionlock.h"
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
#include "synchapi.h"
|
#include "synchapi.h"
|
||||||
@@ -9,6 +5,7 @@
|
|||||||
#include "pthread.h"
|
#include "pthread.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
PRIVATE_DEFINITION_START(PIConditionLock)
|
PRIVATE_DEFINITION_START(PIConditionLock)
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
CRITICAL_SECTION
|
CRITICAL_SECTION
|
||||||
@@ -18,33 +15,39 @@ PRIVATE_DEFINITION_START(PIConditionLock)
|
|||||||
nativeHandle;
|
nativeHandle;
|
||||||
PRIVATE_DEFINITION_END(PIConditionLock)
|
PRIVATE_DEFINITION_END(PIConditionLock)
|
||||||
|
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
PIConditionLock::PIConditionLock() {
|
PIConditionLock::PIConditionLock() {
|
||||||
InitializeCriticalSection(&PRIVATE->nativeHandle);
|
InitializeCriticalSection(&PRIVATE->nativeHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIConditionLock::~PIConditionLock() {
|
PIConditionLock::~PIConditionLock() {
|
||||||
DeleteCriticalSection(&PRIVATE->nativeHandle);
|
DeleteCriticalSection(&PRIVATE->nativeHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionLock::lock() {
|
void PIConditionLock::lock() {
|
||||||
EnterCriticalSection(&PRIVATE->nativeHandle);
|
EnterCriticalSection(&PRIVATE->nativeHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionLock::unlock() {
|
void PIConditionLock::unlock() {
|
||||||
LeaveCriticalSection(&PRIVATE->nativeHandle);
|
LeaveCriticalSection(&PRIVATE->nativeHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *PIConditionLock::handle() {
|
void *PIConditionLock::handle() {
|
||||||
return &PRIVATE->nativeHandle;
|
return &PRIVATE->nativeHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIConditionLock::tryLock() {
|
bool PIConditionLock::tryLock() {
|
||||||
return TryEnterCriticalSection(&PRIVATE->nativeHandle) != 0;
|
return TryEnterCriticalSection(&PRIVATE->nativeHandle) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
|
||||||
PIConditionLock::PIConditionLock() {
|
PIConditionLock::PIConditionLock() {
|
||||||
pthread_mutexattr_t attr;
|
pthread_mutexattr_t attr;
|
||||||
memset(&attr, 0, sizeof(attr));
|
memset(&attr, 0, sizeof(attr));
|
||||||
@@ -55,24 +58,29 @@ PIConditionLock::PIConditionLock() {
|
|||||||
pthread_mutexattr_destroy(&attr);
|
pthread_mutexattr_destroy(&attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIConditionLock::~PIConditionLock() {
|
PIConditionLock::~PIConditionLock() {
|
||||||
pthread_mutex_destroy(&(PRIVATE->nativeHandle));
|
pthread_mutex_destroy(&(PRIVATE->nativeHandle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionLock::lock() {
|
void PIConditionLock::lock() {
|
||||||
pthread_mutex_lock(&(PRIVATE->nativeHandle));
|
pthread_mutex_lock(&(PRIVATE->nativeHandle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionLock::unlock() {
|
void PIConditionLock::unlock() {
|
||||||
pthread_mutex_unlock(&(PRIVATE->nativeHandle));
|
pthread_mutex_unlock(&(PRIVATE->nativeHandle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *PIConditionLock::handle() {
|
void *PIConditionLock::handle() {
|
||||||
return &PRIVATE->nativeHandle;
|
return &PRIVATE->nativeHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIConditionLock::tryLock() {
|
bool PIConditionLock::tryLock() {
|
||||||
return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0);;
|
return (pthread_mutex_trylock(&(PRIVATE->nativeHandle)) == 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
//
|
|
||||||
// Created by fomenko on 20.09.2019.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "piplatform.h"
|
#include "piplatform.h"
|
||||||
|
#include "piconditionvar.h"
|
||||||
|
#include "pithread.h"
|
||||||
|
#include "pitime.h"
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
#define _WIN32_WINNT 0x0600
|
#define _WIN32_WINNT 0x0600
|
||||||
@@ -11,9 +10,6 @@
|
|||||||
#include <winbase.h>
|
#include <winbase.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "piconditionvar.h"
|
|
||||||
#include "pithread.h"
|
|
||||||
#include "pitime.h"
|
|
||||||
|
|
||||||
PRIVATE_DEFINITION_START(PIConditionVariable)
|
PRIVATE_DEFINITION_START(PIConditionVariable)
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
@@ -25,6 +21,7 @@ PRIVATE_DEFINITION_START(PIConditionVariable)
|
|||||||
bool isDestroying;
|
bool isDestroying;
|
||||||
PRIVATE_DEFINITION_END(PIConditionVariable)
|
PRIVATE_DEFINITION_END(PIConditionVariable)
|
||||||
|
|
||||||
|
|
||||||
PIConditionVariable::PIConditionVariable() {
|
PIConditionVariable::PIConditionVariable() {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
InitializeConditionVariable(&PRIVATE->nativeHandle);
|
InitializeConditionVariable(&PRIVATE->nativeHandle);
|
||||||
@@ -36,14 +33,15 @@ PIConditionVariable::PIConditionVariable() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIConditionVariable::~PIConditionVariable() {
|
PIConditionVariable::~PIConditionVariable() {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
#else
|
#else
|
||||||
pthread_cond_destroy(&PRIVATE->nativeHandle);
|
pthread_cond_destroy(&PRIVATE->nativeHandle);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionVariable::wait(PIConditionLock& lk) {
|
void PIConditionVariable::wait(PIConditionLock& lk) {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), INFINITE);
|
SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), INFINITE);
|
||||||
@@ -52,6 +50,7 @@ void PIConditionVariable::wait(PIConditionLock& lk) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>& condition) {
|
void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>& condition) {
|
||||||
bool isCondition;
|
bool isCondition;
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -66,6 +65,7 @@ void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>&
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
||||||
bool isNotTimeout;
|
bool isNotTimeout;
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
@@ -78,6 +78,7 @@ bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
|||||||
return isNotTimeout;
|
return isNotTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PIConditionVariable::waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()> &condition) {
|
bool PIConditionVariable::waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()> &condition) {
|
||||||
bool isCondition;
|
bool isCondition;
|
||||||
PITimeMeasurer measurer;
|
PITimeMeasurer measurer;
|
||||||
@@ -109,6 +110,8 @@ void PIConditionVariable::notifyOne() {
|
|||||||
pthread_cond_signal(&PRIVATE->nativeHandle);
|
pthread_cond_signal(&PRIVATE->nativeHandle);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIConditionVariable::notifyAll() {
|
void PIConditionVariable::notifyAll() {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
WakeAllConditionVariable(&PRIVATE->nativeHandle);
|
WakeAllConditionVariable(&PRIVATE->nativeHandle);
|
||||||
@@ -117,12 +120,3 @@ void PIConditionVariable::notifyAll() {
|
|||||||
#endif
|
#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
|
#ifndef PIP_TESTS_EXECUTOR_H
|
||||||
#define PIP_TESTS_EXECUTOR_H
|
#define PIP_TESTS_EXECUTOR_H
|
||||||
|
|
||||||
#include <pithread.h>
|
|
||||||
#include <functional>
|
|
||||||
#include <utility>
|
|
||||||
#include "piblockingdequeue.h"
|
#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
|
* @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
|
* 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 {
|
class PIThreadPoolExecutor {
|
||||||
public:
|
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();
|
virtual ~PIThreadPoolExecutor();
|
||||||
|
|
||||||
@@ -80,8 +42,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
volatile bool isShutdown_;
|
volatile bool isShutdown_;
|
||||||
PIBlockingDequeue<std::function<void()> >* taskQueue;
|
PIBlockingDequeue<std::function<void()> >* taskQueue;
|
||||||
PIThreadFactory* threadFactory;
|
PIVector<PIThread*> threadPool;
|
||||||
PIVector<AbstractThread*> threadPool;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PIP_TESTS_EXECUTOR_H
|
#endif //PIP_TESTS_EXECUTOR_H
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
//
|
|
||||||
// Created by fomenko on 25.09.2019.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef AWRCANFLASHER_PICONDITIONLOCK_H
|
#ifndef AWRCANFLASHER_PICONDITIONLOCK_H
|
||||||
#define AWRCANFLASHER_PICONDITIONLOCK_H
|
#define AWRCANFLASHER_PICONDITIONLOCK_H
|
||||||
|
|
||||||
#include <pimutex.h>
|
#include "pimutex.h"
|
||||||
#include <piinit.h>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Continued
|
* @brief Continued
|
||||||
@@ -16,18 +12,17 @@ public:
|
|||||||
explicit PIConditionLock();
|
explicit PIConditionLock();
|
||||||
virtual ~PIConditionLock();
|
virtual ~PIConditionLock();
|
||||||
|
|
||||||
/**
|
//! \brief lock
|
||||||
* @brief lock
|
|
||||||
*/
|
|
||||||
void lock();
|
void lock();
|
||||||
|
|
||||||
/**
|
//! \brief unlock
|
||||||
* @brief unlock
|
|
||||||
*/
|
|
||||||
void unlock();
|
void unlock();
|
||||||
|
|
||||||
|
//! \brief tryLock
|
||||||
bool tryLock();
|
bool tryLock();
|
||||||
|
|
||||||
void * handle();
|
void * handle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NO_COPY_CLASS(PIConditionLock)
|
NO_COPY_CLASS(PIConditionLock)
|
||||||
PRIVATE_DECLARATION
|
PRIVATE_DECLARATION
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
//
|
|
||||||
// Created by fomenko on 20.09.2019.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef PIP_TESTS_PICONDITIONVAR_H
|
#ifndef PIP_TESTS_PICONDITIONVAR_H
|
||||||
#define PIP_TESTS_PICONDITIONVAR_H
|
#define PIP_TESTS_PICONDITIONVAR_H
|
||||||
|
|
||||||
@@ -9,6 +5,7 @@
|
|||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
#include "piinit.h"
|
#include "piinit.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief A condition variable is an object able to block the calling thread until notified to resume.
|
* @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
|
* @return false if timeout reached or true if wakeup condition is true
|
||||||
*/
|
*/
|
||||||
virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()>& condition);
|
virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()>& condition);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NO_COPY_CLASS(PIConditionVariable)
|
NO_COPY_CLASS(PIConditionVariable)
|
||||||
|
|
||||||
PRIVATE_DECLARATION
|
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
|
#endif //PIP_TESTS_PICONDITIONVAR_H
|
||||||
|
|||||||
Reference in New Issue
Block a user