Play-/Source/MailBox.h

47 lines
933 B
C
Raw Normal View History

#pragma once
#include <functional>
#include <deque>
#include <mutex>
#include <condition_variable>
class CMailBox
{
public:
2018-04-30 21:01:23 +01:00
virtual ~CMailBox() = default;
2018-04-30 21:01:23 +01:00
typedef std::function<void()> FunctionType;
2018-04-30 21:01:23 +01:00
void SendCall(const FunctionType&, bool = false);
void SendCall(FunctionType&&);
void FlushCalls();
2018-04-30 21:01:23 +01:00
bool IsPending() const;
void ReceiveCall();
void WaitForCall();
void WaitForCall(unsigned int);
private:
struct MESSAGE
{
2017-03-25 21:31:59 -04:00
MESSAGE() = default;
MESSAGE(MESSAGE&&) = default;
MESSAGE(const MESSAGE&) = delete;
2018-04-30 21:01:23 +01:00
MESSAGE& operator=(MESSAGE&&) = default;
MESSAGE& operator=(const MESSAGE&) = delete;
2017-03-25 21:31:59 -04:00
2018-04-30 21:01:23 +01:00
FunctionType function;
2023-06-21 12:07:08 -04:00
bool sync = false;
};
typedef std::deque<MESSAGE> FunctionCallQueue;
2018-04-30 21:01:23 +01:00
FunctionCallQueue m_calls;
std::mutex m_callMutex;
std::condition_variable m_callFinished;
std::condition_variable m_waitCondition;
2023-06-21 12:07:08 -04:00
bool m_callDone = false;
};