2018-07-17 18:48:21 +02:00
|
|
|
//
|
|
|
|
// Created by Grishka on 17.06.2018.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_MESSAGETHREAD_H
|
|
|
|
#define LIBTGVOIP_MESSAGETHREAD_H
|
|
|
|
|
|
|
|
#include "threading.h"
|
2018-11-09 16:44:01 +01:00
|
|
|
#include "utils.h"
|
2018-07-17 18:48:21 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
2019-04-15 01:43:10 +02:00
|
|
|
#include <atomic>
|
2018-07-17 18:48:21 +02:00
|
|
|
|
|
|
|
namespace tgvoip{
|
|
|
|
class MessageThread : public Thread{
|
|
|
|
public:
|
2018-11-09 16:44:01 +01:00
|
|
|
TGVOIP_DISALLOW_COPY_AND_ASSIGN(MessageThread);
|
2018-07-17 18:48:21 +02:00
|
|
|
MessageThread();
|
|
|
|
virtual ~MessageThread();
|
|
|
|
uint32_t Post(std::function<void()> func, double delay=0, double interval=0);
|
|
|
|
void Cancel(uint32_t id);
|
|
|
|
void CancelSelf();
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
enum{
|
|
|
|
INVALID_ID=0
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
struct Message{
|
|
|
|
uint32_t id;
|
|
|
|
double deliverAt;
|
|
|
|
double interval;
|
|
|
|
std::function<void()> func;
|
|
|
|
};
|
|
|
|
|
2018-11-09 16:44:01 +01:00
|
|
|
void Run();
|
2018-07-17 18:48:21 +02:00
|
|
|
void InsertMessageInternal(Message& m);
|
|
|
|
|
2019-04-15 01:43:10 +02:00
|
|
|
std::atomic<bool> running;
|
2018-07-17 18:48:21 +02:00
|
|
|
std::vector<Message> queue;
|
|
|
|
Mutex queueMutex;
|
2020-03-05 11:04:22 +01:00
|
|
|
Mutex queueAccessMutex;
|
2018-07-17 18:48:21 +02:00
|
|
|
uint32_t lastMessageID=1;
|
|
|
|
bool cancelCurrent=false;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE event;
|
|
|
|
#else
|
|
|
|
pthread_cond_t cond;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //LIBTGVOIP_MESSAGETHREAD_H
|