message-queue.h 3.48 KB
Newer Older
Geithner, Thomas's avatar
Geithner, Thomas committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * message-queue.h
 *
 *  Created on: 12.09.2017
 *      Author: geith
 */

#ifndef MESSAGE_QUEUE_H_
#define MESSAGE_QUEUE_H_

#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <exception>
16
#include <chrono>
Geithner, Thomas's avatar
Geithner, Thomas committed
17
18
19

namespace desk{

20
class MessageException: public std::exception{
Geithner, Thomas's avatar
Geithner, Thomas committed
21
public:
22
23
	MessageException(std::string msg) _GLIBCXX_USE_NOEXCEPT
			: m_msg(msg){ }
Geithner, Thomas's avatar
Geithner, Thomas committed
24

25
26
	virtual const char* what()
		const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT{
Geithner, Thomas's avatar
Geithner, Thomas committed
27

28
29
		return m_msg.c_str();
	}
Geithner, Thomas's avatar
Geithner, Thomas committed
30

31
32
33
protected:
	std::string m_msg;
};
Geithner, Thomas's avatar
Geithner, Thomas committed
34

35
36
37
class TimeoutException: public std::exception{
public:
	TimeoutException() _GLIBCXX_USE_NOEXCEPT { }
38

39
40
	virtual const char* what()
		const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT{
41

42
43
		return "timeout";
	}
44

45
46
protected:
};
47

48
49
50
template <typename T>
class MessageQueue{
public:
Geithner, Thomas's avatar
Geithner, Thomas committed
51
52
53
54
55
56
57
58
59
60
61
	MessageQueue(): m_closed(false) {}

	~MessageQueue() {
		std::unique_lock<std::mutex> lck(m_queueMtx);
		m_closed = true;

		m_queueCond.notify_all();
	}

	void addMessage(T message){
		if(m_closed){
62
			throw MessageException("queue closed");
Geithner, Thomas's avatar
Geithner, Thomas committed
63
64
65
66
67
68
69
70
71
72
73
		}

		std::unique_lock<std::mutex> lck(m_queueMtx);
		m_queue.push(message);

		m_queueCond.notify_all();
	}

	T& getMessage(){
		std::unique_lock<std::mutex> lck(m_queueMtx);

74
		while(m_queue.empty() && !m_closed){
Geithner, Thomas's avatar
Geithner, Thomas committed
75
76
77
			m_queueCond.wait(lck);
		}
		if(m_closed){
78
			throw MessageException("queue closed");
Geithner, Thomas's avatar
Geithner, Thomas committed
79
80
81
82
83
84
85
86
		}

		T& message = m_queue.front();
		m_queue.pop();

		return message;
	}

87
88
89
90
91
92
93
94
95
96
97
98
99
	template <class Rep, class Period>
	T& getMessage(const std::chrono::duration<Rep, Period>& timeout){
		std::unique_lock<std::mutex> lck(m_queueMtx);

		while(m_queue.empty() && !m_closed){
			//m_queueCond.wait(lck);
			auto st = m_queueCond.wait_for(lck, timeout);

			if(st == std::cv_status::timeout){
				throw TimeoutException();
			}
		}
		if(m_closed){
100
			throw MessageException("queue closed");
101
102
103
104
105
106
107
108
		}

		T& message = m_queue.front();
		m_queue.pop();

		return message;
	}

Geithner, Thomas's avatar
Geithner, Thomas committed
109
110
111
112
113
114
115
116
117
118
119
120
121
	bool empty(){
		return m_queue.empty();
	}


protected:
	std::queue<T> m_queue;
	std::mutex m_queueMtx;
	std::condition_variable m_queueCond;
	bool m_closed;

};

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
template <typename T>
class MessageVariable{
public:
	MessageVariable(): m_closed(false), m_value(0), m_isValid(false) {}

	~MessageVariable() {
		std::unique_lock<std::mutex> lck(m_queueMtx);
		m_closed = true;

		m_queueCond.notify_all();
	}

	void addMessage(T message){
		if(m_closed){
			throw MessageException("queue closed");
		}

		std::unique_lock<std::mutex> lck(m_queueMtx);
		//m_queue.push(message);
		m_value = message;
		m_isValid = true;

		m_queueCond.notify_all();
	}

	T& getMessage(){
		std::unique_lock<std::mutex> lck(m_queueMtx);

		while(!m_isValid && !m_closed){
			m_queueCond.wait(lck);
		}
		if(m_closed){
			throw MessageException("queue closed");
		}

		T& message = m_value;

		m_isValid = false;
		return message;
	}

	template <class Rep, class Period>
	T& getMessage(const std::chrono::duration<Rep, Period>& timeout){
		std::unique_lock<std::mutex> lck(m_queueMtx);

Geithner, Thomas's avatar
Geithner, Thomas committed
167
		while(!m_isValid && !m_closed){
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
			//m_queueCond.wait(lck);
			auto st = m_queueCond.wait_for(lck, timeout);

			if(st == std::cv_status::timeout){
				throw TimeoutException();
			}
		}
		if(m_closed){
			throw MessageException("queue closed");
		}

		T& message = m_value;
		m_isValid = false;

		return message;
	}

	bool empty(){
		return !m_isValid;
	}


protected:
	//std::queue<T> m_queue;
	//std::optional<T> m_queue;
	T m_value;
	bool m_isValid;
	std::mutex m_queueMtx;
	std::condition_variable m_queueCond;
	bool m_closed;

};
Geithner, Thomas's avatar
Geithner, Thomas committed
200
201
202
203

}

#endif /* MESSAGE_QUEUE_H_ */