ThreadUtils.cpp 6.02 KB
Newer Older
ahoms's avatar
ahoms committed
1
2
#include "ThreadUtils.h"
#include <errno.h>
3
#include <sys/time.h>
ahoms's avatar
ahoms committed
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

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;
}


131
Cond::Cond() : m_mutex(MutexAttr::Normal)
ahoms's avatar
ahoms committed
132
133
134
135
136
137
138
139
140
141
{
	if (pthread_cond_init(&m_cond, NULL) != 0)
		throw LIMA_COM_EXC(Error, "Error initializing condition");
}

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

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
/** @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
167
168
169
170
171
172
173
174
}

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

175
176
177
178
179
180
181
182
183
184
void Cond::acquire()
{
	m_mutex.lock();
}

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

185
186
187
188
189
void Cond::broadcast()
{
  if (pthread_cond_broadcast(&m_cond) != 0)
    throw LIMA_COM_EXC(Error, "Error broadcast condition");
}
ahoms's avatar
ahoms committed
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

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)
250
	: m_master(&master)
ahoms's avatar
ahoms committed
251
252
253
254
255
256
257
258
259
{
}

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

void CmdThread::AuxThread::threadFunction()
{
260
	m_master->cmdLoop();
ahoms's avatar
ahoms committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
}

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

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

AutoMutex CmdThread::lock()
{
278
  return AutoMutex(m_cond.mutex(), AutoMutex::Locked);
ahoms's avatar
ahoms committed
279
280
281
282
}

AutoMutex CmdThread::tryLock()
{
283
  return AutoMutex(m_cond.mutex(), AutoMutex::TryLocked);
ahoms's avatar
ahoms committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
}

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)
303
		m_cond.wait();
ahoms's avatar
ahoms committed
304
305
306
307
308
309
}

int CmdThread::waitNotStatus(int status)
{
	AutoMutex l = lock();
	while (m_status == status)
310
		m_cond.wait();
ahoms's avatar
ahoms committed
311
312
313
314
315
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
	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)
341
	  m_cond.wait();
ahoms's avatar
ahoms committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

	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);
		}
	}
}