Files
pip/src_main/concurrent/piconditionvar.h
Фоменко Степан Владимирович a6599a8ed1 Documentation for concurrent and small improvements
git-svn-id: svn://db.shs.com.ru/pip@868 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
2019-12-30 16:53:09 +00:00

63 lines
2.0 KiB
C++

//
// 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"
/**
* @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 {
public:
explicit 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();
/**
* @brief Unblocks all threads currently waiting for this condition. If no threads are waiting, the function does
* nothing.
*/
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