File: MessageThread.h

package info (click to toggle)
libtgvoip 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 9,004 kB
  • sloc: cpp: 61,331; ansic: 24,725; sh: 4,150; makefile: 749; asm: 242
file content (52 lines) | stat: -rwxr-xr-x 947 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Created by Grishka on 17.06.2018.
//

#ifndef LIBTGVOIP_MESSAGETHREAD_H
#define LIBTGVOIP_MESSAGETHREAD_H

#include "threading.h"
#include "utils.h"
#include <vector>
#include <functional>

namespace tgvoip{
	class MessageThread : public Thread{
	public:
		TGVOIP_DISALLOW_COPY_AND_ASSIGN(MessageThread);
		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;
		};

		void Run();
		void InsertMessageInternal(Message& m);

		bool running=true;
		std::vector<Message> queue;
		Mutex queueMutex;
		uint32_t lastMessageID=1;
		bool cancelCurrent=false;

#ifdef _WIN32
		HANDLE event;
#else
		pthread_cond_t cond;
#endif
	};
}

#endif //LIBTGVOIP_MESSAGETHREAD_H