ThreadUtils.cpp 6.1 KB
Newer Older
ahoms's avatar
ahoms committed
1
#include "ThreadUtils.h"
2
#include "Exceptions.h"
ahoms's avatar
ahoms committed
3
#include <errno.h>
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
4
#ifdef __unix
5
#include <sys/time.h>
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
6
7
8
#else
#include <time_compat.h>
#endif
ahoms's avatar
ahoms committed
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

using namespace lima;

MutexAttr::MutexAttr(Type type)
{
	if (pthread_mutexattr_init(&m_mutex_attr) != 0)
		throw LIMA_COM_EXC(Error, "Error initializing mutex attr");

	try {
		setType(type);
	} catch (...) {
		destroy();
		throw;
	}
}

MutexAttr::MutexAttr(const MutexAttr& mutex_attr)
	: m_mutex_attr(mutex_attr.m_mutex_attr)
{
}

MutexAttr::~MutexAttr()
{
	destroy();
}

void MutexAttr::setType(Type type)
{
	int kind;
	switch (type) {
	case Normal: 
		kind = PTHREAD_MUTEX_NORMAL;
		break;
	case Recursive:
		kind = PTHREAD_MUTEX_RECURSIVE;
		break;
	case ErrorCheck:
		kind = PTHREAD_MUTEX_ERRORCHECK;
		break;
	default:
		throw LIMA_COM_EXC(InvalidValue, "Invalid MutexAttr type");
	}

	if (pthread_mutexattr_settype(&m_mutex_attr, kind) != 0)
		throw LIMA_COM_EXC(Error, "Error setting mutex attr");
}

MutexAttr::Type MutexAttr::getType() const
{
	int kind;
	if (pthread_mutexattr_gettype(&m_mutex_attr, &kind) != 0)
		throw LIMA_COM_EXC(Error, "Error getting mutex attr");

	switch (kind) {
	case PTHREAD_MUTEX_NORMAL:
		return Normal;
	case PTHREAD_MUTEX_RECURSIVE:
		return Recursive;
	case PTHREAD_MUTEX_ERRORCHECK:
		return ErrorCheck;
	default:
		throw LIMA_COM_EXC(Error, "Invalid mutex attr kind");
	}
}

MutexAttr& MutexAttr::operator =(Type type)
{
	setType(type);
	return *this;
}

MutexAttr& MutexAttr::operator =(const MutexAttr& mutex_attr)
{
	m_mutex_attr = mutex_attr.m_mutex_attr;
	return *this;
}

void MutexAttr::destroy()
{
	pthread_mutexattr_destroy(&m_mutex_attr);
}


Mutex::Mutex(MutexAttr mutex_attr)
	: m_mutex_attr(mutex_attr)
{
	pthread_mutexattr_t& attr = m_mutex_attr.m_mutex_attr;
	if (pthread_mutex_init(&m_mutex, &attr) != 0)
		throw LIMA_COM_EXC(Error, "Error initializing mutex");
}

Mutex::~Mutex()
{
	pthread_mutex_destroy(&m_mutex);
}

void Mutex::lock()
{
	if (pthread_mutex_lock(&m_mutex) != 0)
		throw LIMA_COM_EXC(Error, "Error locking mutex");
}

void Mutex::unlock()
{
	if (pthread_mutex_unlock(&m_mutex) != 0)
		throw LIMA_COM_EXC(Error, "Error unlocking mutex");
}

bool Mutex::tryLock()
{
	
	switch (pthread_mutex_trylock(&m_mutex)) {
	case EBUSY:
		return false;
	case 0:
		return true;
	default:
		throw LIMA_COM_EXC(Error, "Error trying to lock mutex");
	}
}

MutexAttr Mutex::getAttr()
{
	return m_mutex_attr;
}


136
Cond::Cond() : m_mutex(MutexAttr::Normal)
ahoms's avatar
ahoms committed
137
138
139
140
141
142
143
144
145
146
{
	if (pthread_cond_init(&m_cond, NULL) != 0)
		throw LIMA_COM_EXC(Error, "Error initializing condition");
}

Cond::~Cond()
{
	pthread_cond_destroy(&m_cond);
}

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
/** @brief wait on cond variable
 *  @return 
 *  - true if ok 
 *  - false if timeout
 */
bool Cond::wait(double timeout)
{
  int retcode = 0;
  if(timeout >= 0.)
    {
      struct timeval now;
      struct timespec waitTimeout;
      int retcode = 0;
      gettimeofday(&now,NULL);
      waitTimeout.tv_sec = now.tv_sec + long(timeout);
      waitTimeout.tv_nsec = (now.tv_usec * 1000) + 
	long((timeout - long(timeout)) * 1e9);
      retcode = pthread_cond_timedwait(&m_cond,&m_mutex.m_mutex,&waitTimeout);
    }
  else
      retcode = pthread_cond_wait(&m_cond, &m_mutex.m_mutex);
	
  if(retcode && retcode != ETIMEDOUT)
    throw LIMA_COM_EXC(Error, "Error waiting for condition");
  return !retcode;
ahoms's avatar
ahoms committed
172
173
174
175
176
177
178
179
}

void Cond::signal()
{
	if (pthread_cond_signal(&m_cond) != 0)
		throw LIMA_COM_EXC(Error, "Error signaling condition");
}

180
181
182
183
184
185
186
187
188
189
void Cond::acquire()
{
	m_mutex.lock();
}

void Cond::release()
{
	m_mutex.unlock();
}

190
191
192
193
194
void Cond::broadcast()
{
  if (pthread_cond_broadcast(&m_cond) != 0)
    throw LIMA_COM_EXC(Error, "Error broadcast condition");
}
ahoms's avatar
ahoms committed
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

Thread::Thread()
{
	m_started = m_finished = false;
}

Thread::~Thread()
{
	if (m_started) {
		abort();
		pthread_join(m_thread, NULL);
	}
}

void Thread::start()
{
	if (m_started)
		throw LIMA_COM_EXC(Error, "Thread already started");

	if (pthread_create(&m_thread, NULL, staticThreadFunction, this) != 0)
		throw LIMA_HW_EXC(Error, "Error creating thread");

	m_started = true;
}

void Thread::abort()
{
	if (!m_started)
		throw LIMA_COM_EXC(Error, "Thread has not been started");

	if (!m_finished)
		pthread_cancel(m_thread);
}

bool Thread::hasStarted()
{
	return m_started;
}

bool Thread::hasFinished()
{
	return m_finished;
}

void *Thread::staticThreadFunction(void *data)
{
	Thread *thread = (Thread *) data;

	try {
		thread->threadFunction();
	} catch (...) {

	}
	
	thread->m_finished = true;
	return NULL;
}


CmdThread::AuxThread::AuxThread(CmdThread& master)
255
	: m_master(&master)
ahoms's avatar
ahoms committed
256
257
258
259
260
261
262
263
264
{
}

CmdThread::AuxThread::~AuxThread()
{
}

void CmdThread::AuxThread::threadFunction()
{
265
	m_master->cmdLoop();
ahoms's avatar
ahoms committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}

CmdThread::CmdThread()
	: m_thread(*this)
{
	m_status = InInit;
	m_cmd = None;
}

CmdThread::~CmdThread()
{
	abort();
	waitStatus(Finished);
}

AutoMutex CmdThread::lock()
{
283
  return AutoMutex(m_cond.mutex(), AutoMutex::Locked);
ahoms's avatar
ahoms committed
284
285
286
287
}

AutoMutex CmdThread::tryLock()
{
288
  return AutoMutex(m_cond.mutex(), AutoMutex::TryLocked);
ahoms's avatar
ahoms committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
}

int CmdThread::getStatus()
{
	AutoMutex l = lock();
	return m_status;
}

void CmdThread::setStatus(int status)
{
	AutoMutex l = lock();
	m_status = status;
	m_cond.signal();
}

void CmdThread::waitStatus(int status)
{
	AutoMutex l = lock();
	while (m_status != status)
308
		m_cond.wait();
ahoms's avatar
ahoms committed
309
310
311
312
313
314
}

int CmdThread::waitNotStatus(int status)
{
	AutoMutex l = lock();
	while (m_status == status)
315
		m_cond.wait();
ahoms's avatar
ahoms committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
	return m_status;
}

void CmdThread::sendCmd(int cmd)
{
	AutoMutex l = lock();
	m_cmd = cmd;
	m_cond.signal();
}

void CmdThread::start()
{
	if (m_thread.hasStarted())
		throw LIMA_COM_EXC(InvalidValue, "Thread already started");

	m_cmd = Init;
	m_thread.start();
}

void CmdThread::abort()
{
	if (getStatus() != Finished)
		sendCmd(Abort);
}

int CmdThread::waitNextCmd()
{
	AutoMutex l = lock();

	while (m_cmd == None)
346
	  m_cond.wait();
ahoms's avatar
ahoms committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

	int cmd = m_cmd;
	m_cmd = None;
	return cmd;
}

void CmdThread::cmdLoop()
{
	while (getStatus() != Finished) {
		int cmd = waitNextCmd();
		switch (cmd) {
		case None:
			throw LIMA_COM_EXC(InvalidValue, "Invalid None cmd");
		case Init:
			init();
			break;
		case Stop:
			setStatus(Stopped);
			break;
		case Abort:
			setStatus(Finished);
			break;
		default:
			execCmd(cmd);
		}
	}
}