summaryrefslogtreecommitdiff
path: root/lib/asyncqueue.hpp
blob: 61ff412e02ce5c02998fcf21d8dff6e7bc8312ad (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
167
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
200
/*
	Copyright (C) 2014  Intel Corporation

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <glib.h>

#include <abstractpropertytype.h>
#include "listplusplus.h"

#include <mutex>
#include <condition_variable>
#include <unordered_set>
#include <vector>

namespace amb
{

template <typename T, class Pred = std::equal_to<T> >
class Queue
{
public:
	Queue(bool unique = false, bool blocking = false)
		:mUnique(unique), mBlocking(blocking)
	{

	}

	virtual ~Queue()
	{

	}

	int count()
	{
		std::lock_guard<std::mutex> lock(mutex);

		return mQueue.size();
	}

	T pop()
	{
		std::unique_lock<std::mutex> lock(mutex);

		if(mBlocking)
		{
			if(!mQueue.size())
			{
				cond.wait(lock);
			}
		}

		if(!mQueue.size())
		{
			throw std::runtime_error("nothing in queue");
		}

		auto itr = mQueue.begin();

		T item = *itr;

		mQueue.erase(itr);

		return item;
	}

	virtual void append(T item)
	{
		{
			std::lock_guard<std::mutex> lock(mutex);

			if(contains(mQueue, item))
			{
				mQueue.erase(std::find(mQueue.begin(), mQueue.end(), item));
			}
			mQueue.push_back(item);
		}

		if(mBlocking)
		{
			cond.notify_all();
		}
	}

	void remove(T item)
	{
		std::lock_guard<std::mutex> lock(mutex);
		removeOne(&mQueue, item);
	}

private:
	bool mBlocking;
	bool mUnique;
	std::mutex mutex;
	std::condition_variable cond;
	std::vector<T> mQueue;
};

template <typename T, class Pred = std::equal_to<T> >
struct AsyncQueueSource{
	GSource source;
	Queue<T, Pred>* queue;
	int minQueueSize;
};

template <typename T, class Pred = std::equal_to<T> >
class AsyncQueueWatcher
{
public:
	typedef function<void (Queue<T, Pred> *)> AsyncQueueWatcherCallback;
	AsyncQueueWatcher(Queue<T, Pred> * queue, AsyncQueueWatcherCallback cb, int queueSize = 0, AbstractPropertyType::Priority priority = AbstractPropertyType::Normal)
		: callback(cb), mMaxQueueSize(queueSize)
	{

		static GSourceFuncs funcs = {prepare, check, dispatch, finalize};
		GSource* source = (GSource *) g_source_new(&funcs, sizeof(AsyncQueueSource<T, Pred>));

		AsyncQueueSource<T, Pred>* watch = (AsyncQueueSource<T, Pred>*)source;
		watch->queue = queue;
		watch->minQueueSize = queueSize;

		gint p = G_PRIORITY_DEFAULT;

		if(priority == AbstractPropertyType::Normal)
			p = G_PRIORITY_DEFAULT;
		else if(priority == AbstractPropertyType::High)
			p = G_PRIORITY_HIGH;
		else if(priority == AbstractPropertyType::Low)
			p = G_PRIORITY_LOW;

		g_source_set_priority(source, p);
		g_source_set_callback(source, nullptr, this, nullptr);

		g_source_attach(source, nullptr);
		g_source_unref(source);
	}

	AsyncQueueWatcherCallback callback;


protected:
	AsyncQueueWatcher(){}

	int mMaxQueueSize;

private:

	static gboolean prepare(GSource *source, gint *timeout)
	{
		AsyncQueueSource<T, Pred>* s = (AsyncQueueSource<T, Pred>*)source;
		*timeout = -1;

		if (!s)
			return false;

		return s->queue->count() > s->minQueueSize;
	}

	static gboolean check(GSource *source)
	{
		AsyncQueueSource<T, Pred>* s = (AsyncQueueSource<T, Pred>*)source;

		if (!s)
			return false;

		return s->queue->count() > s->minQueueSize;
	}

	static gboolean dispatch(GSource *source, GSourceFunc callback, gpointer userData)
	{
		AsyncQueueSource<T, Pred>* s = (AsyncQueueSource<T, Pred>*)source;

		if (!s)
			return false;

		AsyncQueueWatcher<T, Pred>* watcher = static_cast<AsyncQueueWatcher<T, Pred>*>(userData);

		watcher->callback(s->queue);
		return true;
	}

	static void finalize(GSource* source)
	{

	}
};
}  // namespace amb