Documentation for concurrent and small improvements
git-svn-id: svn://db.shs.com.ru/pip@868 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -11,7 +11,8 @@ PIThreadPoolExecutor::PIThreadPoolExecutor(size_t corePoolSize, PIBlockingDequeu
|
|||||||
auto runnable = taskQueue->poll(100, std::function<void()>());
|
auto runnable = taskQueue->poll(100, std::function<void()>());
|
||||||
if (runnable) {
|
if (runnable) {
|
||||||
runnable();
|
runnable();
|
||||||
} else if (isShutdown_) threadPool[i]->stop();
|
}
|
||||||
|
if (isShutdown_ && taskQueue->size() == 0) threadPool[i]->stop();
|
||||||
});
|
});
|
||||||
threadPool.push_back(thread);
|
threadPool.push_back(thread);
|
||||||
thread->start();
|
thread->start();
|
||||||
@@ -35,7 +36,6 @@ void PIThreadPoolExecutor::shutdownNow() {
|
|||||||
|
|
||||||
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
PIThreadPoolExecutor::~PIThreadPoolExecutor() {
|
||||||
shutdownNow();
|
shutdownNow();
|
||||||
taskQueue->getConditionVar()->notifyAll();
|
|
||||||
while (threadPool.size() > 0) delete threadPool.take_back();
|
while (threadPool.size() > 0) delete threadPool.take_back();
|
||||||
delete threadFactory;
|
delete threadFactory;
|
||||||
delete taskQueue;
|
delete taskQueue;
|
||||||
@@ -48,3 +48,7 @@ void PIThreadPoolExecutor::execute(const std::function<void()> &runnable) {
|
|||||||
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
volatile bool PIThreadPoolExecutor::isShutdown() const {
|
||||||
return isShutdown_;
|
return isShutdown_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PIThreadPoolExecutor::shutdown() {
|
||||||
|
isShutdown_ = true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,13 +3,12 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "piplatform.h"
|
#include "piplatform.h"
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
#define _WIN32_WINNT 0x0600
|
#define _WIN32_WINNT 0x0600
|
||||||
#include "synchapi.h"
|
#include "synchapi.h"
|
||||||
#include <windef.h>
|
#include <windef.h>
|
||||||
#include <winbase.h>
|
#include <winbase.h>
|
||||||
#else
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "piconditionvar.h"
|
#include "piconditionvar.h"
|
||||||
@@ -68,15 +67,15 @@ void PIConditionVariable::wait(PIConditionLock& lk, const std::function<bool()>&
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
bool PIConditionVariable::waitFor(PIConditionLock &lk, int timeoutMs) {
|
||||||
bool isTimeout;
|
bool isNotTimeout;
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
isTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
|
isNotTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
|
||||||
#else
|
#else
|
||||||
timespec abstime = {.tv_sec = timeoutMs / 1000, .tv_nsec = timeoutMs * 1000 * 1000};
|
timespec abstime = {.tv_sec = timeoutMs / 1000, .tv_nsec = timeoutMs * 1000 * 1000};
|
||||||
isTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &abstime) == 0;
|
isNotTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &abstime) == 0;
|
||||||
#endif
|
#endif
|
||||||
if (PRIVATE->isDestroying) return false;
|
if (PRIVATE->isDestroying) return false;
|
||||||
return isTimeout;
|
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) {
|
||||||
|
|||||||
@@ -31,3 +31,28 @@ TEST(ExcutorIntegrationTest, execute_is_not_execute_after_shutdown) {
|
|||||||
piMSleep(WAIT_THREAD_TIME_MS);
|
piMSleep(WAIT_THREAD_TIME_MS);
|
||||||
ASSERT_FALSE(isRunnableInvoke);
|
ASSERT_FALSE(isRunnableInvoke);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ExcutorIntegrationTest, execute_is_execute_before_shutdown) {
|
||||||
|
bool isRunnableInvoke = false;
|
||||||
|
PIThreadPoolExecutor executorService(1);
|
||||||
|
executorService.execute([&]() {
|
||||||
|
piMSleep(WAIT_THREAD_TIME_MS);
|
||||||
|
isRunnableInvoke = true;
|
||||||
|
});
|
||||||
|
executorService.shutdown();
|
||||||
|
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||||
|
ASSERT_TRUE(isRunnableInvoke);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ExcutorIntegrationTest, execute_is_awaitTermination_wait) {
|
||||||
|
PIThreadPoolExecutor executorService(1);
|
||||||
|
executorService.execute([&]() {
|
||||||
|
piMSleep(2 * WAIT_THREAD_TIME_MS);
|
||||||
|
});
|
||||||
|
executorService.shutdown();
|
||||||
|
PITimeMeasurer measurer;
|
||||||
|
ASSERT_TRUE(executorService.awaitTermination(4 * WAIT_THREAD_TIME_MS));
|
||||||
|
double waitTime = measurer.elapsed_m();
|
||||||
|
ASSERT_GE(waitTime, WAIT_THREAD_TIME_MS);
|
||||||
|
ASSERT_LE(waitTime, 3 * WAIT_THREAD_TIME_MS);
|
||||||
|
}
|
||||||
@@ -72,9 +72,7 @@ public:
|
|||||||
* accepted. Invocation has no additional effect if already shut down. This method does not wait for previously
|
* accepted. Invocation has no additional effect if already shut down. This method does not wait for previously
|
||||||
* submitted tasks to complete execution. Use awaitTermination to do that.
|
* submitted tasks to complete execution. Use awaitTermination to do that.
|
||||||
*/
|
*/
|
||||||
void shutdown() {
|
void shutdown();
|
||||||
isShutdown_ = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
volatile bool isShutdown() const;
|
volatile bool isShutdown() const;
|
||||||
|
|
||||||
|
|||||||
@@ -15,19 +15,38 @@
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class PIBlockingDequeue: private PIDeque<T> {
|
class PIBlockingDequeue: private PIDeque<T> {
|
||||||
public:
|
public:
|
||||||
explicit inline PIBlockingDequeue(size_t capacity = SIZE_MAX, PIConditionVariable* cond_var = new PIConditionVariable()) : condition_var(cond_var), max_size(capacity) { }
|
explicit inline PIBlockingDequeue(size_t capacity = SIZE_MAX,
|
||||||
explicit inline PIBlockingDequeue(const PIDeque<T>& other) : condition_var(new PIConditionVariable()) {
|
PIConditionVariable* cond_var_add = new PIConditionVariable(),
|
||||||
|
PIConditionVariable* cond_var_rem = new PIConditionVariable())
|
||||||
|
: cond_var_add(cond_var_add), cond_var_rem(cond_var_rem), max_size(capacity) { }
|
||||||
|
explicit inline PIBlockingDequeue(const PIDeque<T>& other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
|
||||||
max_size = SIZE_MAX;
|
max_size = SIZE_MAX;
|
||||||
PIDeque<T>::append(other);
|
PIDeque<T>::append(other);
|
||||||
}
|
}
|
||||||
inline PIBlockingDequeue(PIBlockingDequeue<T> & other) : condition_var(new PIConditionVariable()) {
|
inline PIBlockingDequeue(PIBlockingDequeue<T> & other) : cond_var_add(new PIConditionVariable()), cond_var_rem(new PIConditionVariable()) {
|
||||||
other.mutex.lock();
|
other.mutex.lock();
|
||||||
max_size = other.max_size;
|
max_size = other.max_size;
|
||||||
PIDeque<T>::append(static_cast<PIDeque<T>&>(other));
|
PIDeque<T>::append(static_cast<PIDeque<T>&>(other));
|
||||||
other.mutex.unlock();
|
other.mutex.unlock();
|
||||||
}
|
}
|
||||||
virtual ~PIBlockingDequeue() {
|
virtual ~PIBlockingDequeue() {
|
||||||
delete condition_var;
|
delete cond_var_add;
|
||||||
|
delete cond_var_rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Inserts the specified element into this queue, waiting if necessary for space to become available.
|
||||||
|
*
|
||||||
|
* @todo write tests
|
||||||
|
*
|
||||||
|
* @param v the element to add
|
||||||
|
*/
|
||||||
|
virtual void put(const T & v) {
|
||||||
|
mutex.lock();
|
||||||
|
cond_var_rem->wait(mutex, [&]() { return PIDeque<T>::size() < max_size; });
|
||||||
|
PIDeque<T>::push_back(v);
|
||||||
|
mutex.unlock();
|
||||||
|
cond_var_add->notifyOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +64,7 @@ public:
|
|||||||
}
|
}
|
||||||
PIDeque<T>::push_back(v);
|
PIDeque<T>::push_back(v);
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
condition_var->notifyOne();
|
cond_var_add->notifyOne();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,9 +76,10 @@ public:
|
|||||||
virtual T take() {
|
virtual T take() {
|
||||||
T t;
|
T t;
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
condition_var->wait(mutex, [&]() { return !PIDeque<T>::isEmpty(); });
|
cond_var_add->wait(mutex, [&]() { return !PIDeque<T>::isEmpty(); });
|
||||||
t = T(PIDeque<T>::take_front());
|
t = T(PIDeque<T>::take_front());
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
cond_var_rem->notifyOne();
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,9 +94,10 @@ public:
|
|||||||
virtual T poll(int timeoutMs, const T & defaultVal) {
|
virtual T poll(int timeoutMs, const T & defaultVal) {
|
||||||
T t;
|
T t;
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
bool isOk = condition_var->waitFor(mutex, timeoutMs, [&]() { return !PIDeque<T>::isEmpty(); });
|
bool isOk = cond_var_add->waitFor(mutex, timeoutMs, [&]() { return !PIDeque<T>::isEmpty(); });
|
||||||
t = isOk ? T(PIDeque<T>::take_front()) : defaultVal;
|
t = isOk ? T(PIDeque<T>::take_front()) : defaultVal;
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
if (isOk) cond_var_rem->notifyOne();
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,13 +155,10 @@ public:
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
PIConditionVariable *getConditionVar() const {
|
|
||||||
return condition_var;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PIConditionLock mutex;
|
PIConditionLock mutex;
|
||||||
PIConditionVariable* condition_var;
|
PIConditionVariable* cond_var_add;
|
||||||
|
PIConditionVariable* cond_var_rem;
|
||||||
size_t max_size;
|
size_t max_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,13 @@
|
|||||||
#include <pimutex.h>
|
#include <pimutex.h>
|
||||||
#include <piinit.h>
|
#include <piinit.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Continued
|
||||||
|
*/
|
||||||
class PIP_EXPORT PIConditionLock {
|
class PIP_EXPORT PIConditionLock {
|
||||||
public:
|
public:
|
||||||
explicit PIConditionLock();
|
explicit PIConditionLock();
|
||||||
~PIConditionLock();
|
virtual ~PIConditionLock();
|
||||||
|
|
||||||
void lock();
|
void lock();
|
||||||
void unlock();
|
void unlock();
|
||||||
|
|||||||
@@ -9,15 +9,29 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include "piinit.h"
|
#include "piinit.h"
|
||||||
|
|
||||||
#define PICONDITION_RELIABLE true
|
/**
|
||||||
|
* @brief A condition variable is an object able to block the calling thread until notified to resume.
|
||||||
|
*
|
||||||
|
* It uses a PIConditionLock to lock the thread when one of its wait functions is called. The thread remains
|
||||||
|
* blocked until woken up by another thread that calls a notification function on the same PIConditionVariable object.
|
||||||
|
*/
|
||||||
class PIP_EXPORT PIConditionVariable {
|
class PIP_EXPORT PIConditionVariable {
|
||||||
public:
|
public:
|
||||||
explicit PIConditionVariable();
|
explicit PIConditionVariable();
|
||||||
virtual ~PIConditionVariable();
|
virtual ~PIConditionVariable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Unblocks one of the threads currently waiting for this condition. If no threads are waiting, the function
|
||||||
|
* does nothing. If more than one, it is unspecified which of the threads is selected.
|
||||||
|
*/
|
||||||
virtual void notifyOne();
|
virtual void notifyOne();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Unblocks all threads currently waiting for this condition. If no threads are waiting, the function does
|
||||||
|
* nothing.
|
||||||
|
*/
|
||||||
virtual void notifyAll();
|
virtual void notifyAll();
|
||||||
|
|
||||||
virtual void wait(PIConditionLock& lk);
|
virtual void wait(PIConditionLock& lk);
|
||||||
virtual void wait(PIConditionLock& lk, const std::function<bool()>& condition);
|
virtual void wait(PIConditionLock& lk, const std::function<bool()>& condition);
|
||||||
virtual bool waitFor(PIConditionLock& lk, int timeoutMs);
|
virtual bool waitFor(PIConditionLock& lk, int timeoutMs);
|
||||||
|
|||||||
Reference in New Issue
Block a user