Added inter thread communication lib

git-svn-id: svn://db.shs.com.ru/pip@858 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
16 changed files with 1366 additions and 3 deletions

View File

@@ -0,0 +1,89 @@
//
// Created by fomenko on 20.09.2019.
//
#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
* managing the resources, including threads, consumed when executing a collection of tasks.
*/
class PIThreadPoolExecutor {
public:
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >(), PIThreadFactory* threadFactory = new PIThreadFactory());
virtual ~PIThreadPoolExecutor();
/**
* @brief Executes the given task sometime in the future. The task execute in an existing pooled thread. If the task
* cannot be submitted for execution, either because this executor has been shutdown or because its capacity has been
* reached.
*
* @param runnable not empty function for thread pool execution
*/
void execute(const std::function<void()>& runnable);
void shutdownNow();
/**
* @brief Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be
* 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.
*/
void shutdown() {
isShutdown_ = true;
}
volatile bool isShutdown() const;
bool awaitTermination(int timeoutMs);
private:
volatile bool isShutdown_;
PIBlockingDequeue<std::function<void()> >* taskQueue;
PIThreadFactory* threadFactory;
PIVector<AbstractThread*> threadPool;
};
#endif //PIP_TESTS_EXECUTOR_H

View File

@@ -0,0 +1,148 @@
//
// Created by fomenko on 20.09.2019.
//
#ifndef PIP_TESTS_PIBLOCKINGDEQUEUE_H
#define PIP_TESTS_PIBLOCKINGDEQUEUE_H
#include "pideque.h"
#include "piconditionvar.h"
/**
* @brief A Queue that supports operations that wait for the queue to become non-empty when retrieving an element, and
* wait for space to become available in the queue when storing an element.
*/
template <typename T>
class PIBlockingDequeue: private PIDeque<T> {
public:
explicit inline PIBlockingDequeue(size_t capacity = SIZE_MAX, PIConditionVariable* cond_var = new PIConditionVariable()) : condition_var(cond_var), max_size(capacity) { }
explicit inline PIBlockingDequeue(const PIDeque<T>& other) : condition_var(new PIConditionVariable()) {
max_size = SIZE_MAX;
PIDeque<T>::append(other);
}
inline PIBlockingDequeue(PIBlockingDequeue<T> & other) : condition_var(new PIConditionVariable()) {
other.mutex.lock();
max_size = other.max_size;
PIDeque<T>::append(static_cast<PIDeque<T>&>(other));
other.mutex.unlock();
}
virtual ~PIBlockingDequeue() {
delete condition_var;
}
/**
* @brief Inserts the specified element at the end of this queue if it is possible to do so immediately without
* exceeding the queue's capacity, returning true upon success and false if this queue is full.
*
* @param v the element to add
* @return true if the element was added to this queue, else false
*/
virtual bool offer(const T & v) {
mutex.lock();
if (PIDeque<T>::size() >= max_size) {
mutex.unlock();
return false;
}
PIDeque<T>::push_back(v);
mutex.unlock();
condition_var->notifyOne();
return true;
}
/**
* @brief Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
*
* @return the head of this queue
*/
virtual T take() {
T t;
mutex.lock();
condition_var->wait(mutex, [&]() { return !PIDeque<T>::isEmpty(); });
t = T(PIDeque<T>::take_front());
mutex.unlock();
return t;
}
/**
* @brief Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an
* element to become available.
*
* @param timeoutMs how long to wait before giving up, in milliseconds
* @param defaultVal value, which returns if the specified waiting time elapses before an element is available
* @return the head of this queue, or defaultVal if the specified waiting time elapses before an element is available
*/
virtual T poll(int timeoutMs, const T & defaultVal) {
T t;
mutex.lock();
bool isOk = condition_var->waitFor(mutex, timeoutMs, [&]() { return !PIDeque<T>::isEmpty(); });
t = isOk ? T(PIDeque<T>::take_front()) : defaultVal;
mutex.unlock();
return t;
}
virtual size_t capacity() {
size_t c;
mutex.lock();
c = max_size;
mutex.unlock();
return c;
}
/**
* @brief Returns the number of additional elements that this queue can ideally (in the absence of memory or resource
* constraints) accept. This is always equal to the initial capacity of this queue less the current size of this queue.
*
* @return the remaining capacity
*/
virtual size_t remainingCapacity() {
mutex.lock();
size_t c = max_size - PIDeque<T>::size();
mutex.unlock();
return c;
}
virtual size_t size() {
mutex.lock();
size_t s = PIDeque<T>::size();
mutex.unlock();
return s;
}
/**
* @brief Removes all available elements from this queue and adds them to other given queue.
*/
virtual size_t drainTo(PIDeque<T>& other, size_t maxCount = SIZE_MAX) {
mutex.lock();
size_t count = maxCount > PIDeque<T>::size() ? PIDeque<T>::size() : maxCount;
for (size_t i = 0; i < count; ++i) other.push_back(PIDeque<T>::take_front());
mutex.unlock();
return count;
}
/**
* @brief Removes all available elements from this queue and adds them to other given queue.
*/
virtual size_t drainTo(PIBlockingDequeue<T>& other, size_t maxCount = SIZE_MAX) {
mutex.lock();
other.mutex.lock();
size_t count = maxCount > PIDeque<T>::size() ? PIDeque<T>::size() : maxCount;
size_t otherRemainingCapacity = other.max_size - static_cast<PIDeque<T>>(other).size();
if (count > otherRemainingCapacity) count = otherRemainingCapacity;
for (size_t i = 0; i < count; ++i) other.push_back(PIDeque<T>::take_front());
other.mutex.unlock();
mutex.unlock();
return count;
}
PIConditionVariable *getConditionVar() const {
return condition_var;
}
private:
PIConditionLock mutex;
PIConditionVariable* condition_var;
size_t max_size;
};
#endif //PIP_TESTS_PIBLOCKINGDEQUEUE_H

View File

@@ -0,0 +1,26 @@
//
// Created by fomenko on 25.09.2019.
//
#ifndef AWRCANFLASHER_PICONDITIONLOCK_H
#define AWRCANFLASHER_PICONDITIONLOCK_H
#include <pimutex.h>
#include <piinit.h>
class PIP_EXPORT PIConditionLock {
public:
explicit PIConditionLock();
~PIConditionLock();
void lock();
void unlock();
bool tryLock();
void* handle();
private:
NO_COPY_CLASS(PIConditionLock)
PRIVATE_DECLARATION
};
#endif //AWRCANFLASHER_PICONDITIONLOCK_H

View File

@@ -0,0 +1,48 @@
//
// Created by fomenko on 20.09.2019.
//
#ifndef PIP_TESTS_PICONDITIONVAR_H
#define PIP_TESTS_PICONDITIONVAR_H
#include "piconditionlock.h"
#include <functional>
#include "piinit.h"
#define PICONDITION_RELIABLE true
class PIP_EXPORT PIConditionVariable {
public:
explicit PIConditionVariable();
virtual ~PIConditionVariable();
virtual void notifyOne();
virtual void notifyAll();
virtual void wait(PIConditionLock& lk);
virtual void wait(PIConditionLock& lk, const std::function<bool()>& condition);
virtual bool waitFor(PIConditionLock& lk, int timeoutMs);
virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function<bool()>& condition);
private:
NO_COPY_CLASS(PIConditionVariable)
PRIVATE_DECLARATION
};
class PIThread;
typedef void (*ThreadFunc)(void * );
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