// // Created by fomenko on 20.09.2019. // #ifndef PIP_TESTS_PICONDITIONVAR_H #define PIP_TESTS_PICONDITIONVAR_H #include "piconditionlock.h" #include #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& condition); virtual bool waitFor(PIConditionLock& lk, int timeoutMs); virtual bool waitFor(PIConditionLock& lk, int timeoutMs, const std::function& 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& fun_): fun(fun_) {} void registerToInvoke(PIThread* thread); void* data() const { return (void*)this; } ThreadFunc threadFunc() const { return threadFuncStdFunctionAdapter; } private: std::function fun; }; #endif //PIP_TESTS_PICONDITIONVAR_H