SlsDetectorDoubleBuffer.h 6.35 KB
Newer Older
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################

#ifndef __SLS_DETECTOR_DOUBLE_BUFFER_H
#define __SLS_DETECTOR_DOUBLE_BUFFER_H

#include "lima/Debug.h"
#include "lima/Exceptions.h"
#include "lima/ThreadUtils.h"

#include <array>
#include <functional>

namespace lima 
{

namespace SlsDetector
{

template <class T> class DoubleBufferWriter;
template <class T> class DoubleBufferReader;

/*
 * DoubleBuffer
 */

template <class T>
class DoubleBuffer {
	DEB_CLASS_NAMESPC(DebModCamera, "DoubleBuffer", "SlsDetector");

 public:
	using Counter = std::size_t;

	template <class ... Args>
	DoubleBuffer(Args ... args)
		: m_base(std::forward<Args>(args)...),
		m_buf{{{0, m_base[0]}, {1, m_base[1]}}} {
		DEB_CONSTRUCTOR();
		DEB_TRACE() << DEB_VAR2(&m_buf[0].buffer, &m_buf[1].buffer);
	}

	~DoubleBuffer() {
		DEB_DESTRUCTOR();
		stop();
	}

	auto size() const { return m_base.size(); }

	auto begin() { return m_base.begin(); }
	auto begin() const { return m_base.begin(); }
	auto end() { return m_base.end(); }
	auto end() const { return m_base.end(); }

	auto operator[](std::size_t i) { return m_base[i]; }
	auto operator[](std::size_t i) const { return m_base[i]; }
	
	void reset() {
		DEB_MEMBER_FUNCT();
		stop();
		AutoMutex l = lock();
		m_buf[0].reset();
		m_buf[1].reset();
		m_stopped = false;
	}

	void stop() {
		DEB_MEMBER_FUNCT();
		AutoMutex l = lock();
		m_stopped = true;
		m_cond.broadcast();
		while (m_buf[0].busy() || m_buf[1].busy() || m_waiting)
			m_cond.wait();
	}

 private:
	friend class DoubleBufferWriter<T>;
	friend class DoubleBufferReader<T>;

	enum State {Empty, Writing, Ready, Reading};
	
	static bool valid(Counter c) { return (c != Counter(-1)); }

	struct BufferData {
		int idx;
		T& buffer;
		State state;
		Counter counter;

		BufferData(int i, T& b) : idx(i), buffer(b) { reset(); }
		void reset() { state = Empty, counter = -1; }
		bool writing() { return (state == Writing); }
		bool reading() { return (state == Reading); }
		bool busy() { return writing() || reading(); }
		bool newerThan(Counter c) {
			return (valid(counter) && (!valid(c) || (c < counter)));
		}
		bool newerThan(const BufferData& b) {
			return newerThan(b.counter);
		}
		bool canRead(Counter c) { return !busy() && newerThan(c); }
	};

	using BufferRef = std::unique_ptr<BufferData,
					  std::function<void(BufferData *)>>;

	AutoMutex lock() { return m_cond.mutex(); };

	BufferRef getWriter() {
		DEB_MEMBER_FUNCT();
		AutoMutex wl(m_write_mutex);
		AutoMutex l = lock();
		BufferData *buffer = &m_buf[0];
		if (m_buf[0].reading() ||
		    (!m_buf[1].busy() && m_buf[0].newerThan(m_buf[1])))
			buffer = &m_buf[1];
		buffer->state = Writing;
		DEB_RETURN() << DEB_VAR2(buffer->idx, &buffer->buffer);
		wl.leaveLocked();
		auto deleter = [&](auto b) { putWriter(b); };
		return {buffer, deleter};
	}

	void putWriter(BufferData *buffer) {
		DEB_MEMBER_FUNCT();
		DEB_PARAM() << DEB_VAR2(buffer->idx, &buffer->buffer);
		AutoMutex wl(m_write_mutex, AutoMutex::PrevLocked);
		AutoMutex l = lock();
		buffer->state = valid(buffer->counter) ? Ready : Empty;
		m_cond.broadcast();
	}

	BufferRef getReader(Counter c) {
		DEB_MEMBER_FUNCT();
		DEB_PARAM() << DEB_VAR1(c);
		AutoMutex rl(m_read_mutex);
		AutoMutex l = lock();
		BufferData *buffer;
		while (true) {
			bool ok0 = m_buf[0].canRead(c);
			bool ok1 = m_buf[1].canRead(c);
			if (ok0 && (!ok1 || m_buf[0].newerThan(m_buf[1]))) {
				buffer = &m_buf[0];
				break;
			} else if (ok1) {
				buffer = &m_buf[1];
				break;
			} else if (!valid(c)) {
				buffer = &m_buf[0];
				break;
			}
			DEB_TRACE() << "Start waiting";
			m_waiting = true;
			m_cond.wait();
			m_waiting = false;
			DEB_TRACE() << "Finished waiting";
			if (m_stopped) {
				m_cond.broadcast();
				return {};
			}
		}
		buffer->state = Reading;
		DEB_RETURN() << DEB_VAR2(buffer->idx, &buffer->buffer);
		rl.leaveLocked();
		auto deleter = [&](auto b) { putReader(b); };
		return {buffer, deleter};
	}

	void putReader(BufferData *buffer) {
		DEB_MEMBER_FUNCT();
		DEB_PARAM() << DEB_VAR2(buffer->idx, &buffer->buffer);
		AutoMutex rl(m_read_mutex, AutoMutex::PrevLocked);
		AutoMutex l = lock();
		buffer->state = Ready;
		m_cond.broadcast();
	}

	Cond m_cond;
	Mutex m_write_mutex, m_read_mutex;
	std::array<T, 2> m_base;
	std::array<BufferData, 2> m_buf;
	bool m_waiting{false};
	bool m_stopped{false};
};


/*
 * DoubleBufferWriter
 */

template <class T>
class DoubleBufferWriter {
	DEB_CLASS_NAMESPC(DebModCamera, "DoubleBufferWriter", "SlsDetector");

 public:
	using Counter = typename DoubleBuffer<T>::Counter;

        DoubleBufferWriter(DoubleBuffer<T>& db) : m_buf(db.getWriter()) {}

	T& getBuffer() { return m_buf->buffer; }

	void setCounter(Counter c) { m_buf->counter = c; }

 private:
	using Buffer = typename DoubleBuffer<T>::BufferRef;

	Buffer m_buf;
};


/*
 * DoubleBufferReader
 */

template <class T>
class DoubleBufferReader {
	DEB_CLASS_NAMESPC(DebModCamera, "DoubleBufferReader", "SlsDetector");

 public:
	using Counter = typename DoubleBuffer<T>::Counter;

	DoubleBufferReader(DoubleBuffer<T>& db, Counter c = -1)
		: m_buf(db.getReader(c)) {}

	operator bool() { return m_buf; }

	T& getBuffer() { return validBuffer()->buffer; }

	Counter getCounter() { return validBuffer()->counter;	}

 private:
	using BufferRef = typename DoubleBuffer<T>::BufferRef;

	auto validBuffer() {
		DEB_MEMBER_FUNCT();
		if (!m_buf)
			THROW_HW_ERROR(Error) << "Using aborted Reader";
		return m_buf.get();
	}

	BufferRef m_buf;
};

} // namespace SlsDetector

} // namespace lima



#endif // __SLS_DETECTOR_DOUBLE_BUFFER_H