File: threads_posix.cc

package info (click to toggle)
arts 1.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,436 kB
  • ctags: 9,848
  • sloc: ansic: 44,670; cpp: 33,776; sh: 10,486; perl: 3,470; makefile: 372; yacc: 347; lex: 160
file content (399 lines) | stat: -rw-r--r-- 8,508 bytes parent folder | download | duplicates (5)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
341
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
    /*

    Copyright (C) 2001 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* only compile this if we have libpthread available */
#ifdef HAVE_LIBPTHREAD

#include <gsl/gslconfig.h>

#include <sys/types.h>
#include <stddef.h>
#include <stdarg.h>
#include <pthread.h>
#include <semaphore.h>
#include <debug.h>
#include <string.h>

#include "thread.h"

/*
 * define this if you want to protect mutexes against being locked twice by
 * the same thread
 */
#undef PTHREAD_DEBUG

namespace Arts {

extern void *gslGlobalMutexTable;

namespace PosixThreads {

class ThreadCondition_impl;

class Mutex_impl : public Arts::Mutex_impl {
protected:
	friend class ThreadCondition_impl;
	pthread_mutex_t mutex;

#ifdef PTHREAD_DEBUG
	pthread_t owner;
#endif

public:
	Mutex_impl()
	{
		pthread_mutex_init(&mutex, 0);
#ifdef PTHREAD_DEBUG
		owner = 0;
#endif
	}
	void lock()
	{
#ifdef PTHREAD_DEBUG
		pthread_t self = pthread_self();
		arts_assert(owner != self);
#endif

		pthread_mutex_lock(&mutex);

#ifdef PTHREAD_DEBUG
		arts_assert(!owner);
		owner = self;
#endif
	}
	bool tryLock()
	{
#ifdef PTHREAD_DEBUG
		pthread_t self = pthread_self();
		arts_assert(owner != self);
#endif

		int result = pthread_mutex_trylock(&mutex);

#ifdef PTHREAD_DEBUG
		if(result == 0)
		{
			arts_assert(!owner);
			owner = self;
		}
#endif
		return(result == 0);
	}
	void unlock()
	{
#ifdef PTHREAD_DEBUG
		arts_assert(owner == pthread_self());
		owner = 0;
#endif

		pthread_mutex_unlock(&mutex);
	}
};

class RecMutex_impl : public Arts::Mutex_impl {
protected:
	friend class ThreadCondition_impl;
	pthread_mutex_t mutex;
	pthread_t owner;
	int count;

public:
	RecMutex_impl()
	{
		pthread_mutex_init(&mutex, 0);
		owner = 0;
		count = 0;
	}
	void lock()
	{
		pthread_t self = pthread_self();
		if(owner != self)
		{
			pthread_mutex_lock(&mutex);
#ifdef PTHREAD_DEBUG
			arts_assert(count == 0);
			arts_assert(!owner);
#endif
			owner = self;
		}
		count++;
	}
	bool tryLock()
	{
		pthread_t self = pthread_self();
		if(owner != self)
		{
			int result = pthread_mutex_trylock(&mutex);
			if(result != 0)
				return false;

#ifdef PTHREAD_DEBUG
			arts_assert(count == 0);
			arts_assert(!owner);
#endif
			owner = self;
		}
		count++;
		return true;
	}
	void unlock()
	{
#ifdef PTHREAD_DEBUG
		arts_assert(owner == pthread_self());
		arts_assert(count > 0);
#endif

		count--;
		if(count == 0)
		{
			owner = 0;
			pthread_mutex_unlock(&mutex);
		}
	}
};


class Thread_impl : public Arts::Thread_impl {
protected:
	friend class PosixThreads;
	pthread_t pthread;
	Thread *thread;

public:
	Thread_impl(Thread *thread) : thread(thread) {
	}
	void setPriority(int priority) {
		struct sched_param sp;
		sp.sched_priority = priority;
		if (pthread_setschedparam(pthread, SCHED_FIFO, &sp))
			arts_debug("Thread::setPriority: sched_setscheduler failed");
	}
	static pthread_key_t privateDataKey;
	static void *threadStartInternal(void *impl)
	{
		pthread_setspecific(privateDataKey, impl);

    	((Thread_impl *)impl)->thread->run();
    	return 0;
	}
	void start() {
		pthread_create(&pthread,0,threadStartInternal,this);
	}
	void waitDone() {
    	void *foo;
    	pthread_join(pthread,&foo);
	}
};

pthread_key_t Thread_impl::privateDataKey;

class ThreadCondition_impl : public Arts::ThreadCondition_impl {
protected:
	pthread_cond_t cond;

public:
	ThreadCondition_impl() {
		pthread_cond_init(&cond, 0);
	}
	~ThreadCondition_impl() {
		pthread_cond_destroy(&cond);
	}
	void wakeOne() {
		pthread_cond_signal(&cond);
	}
	void wakeAll() {
		pthread_cond_broadcast(&cond);
	}
	void wait(Arts::Mutex_impl *mutex) {
#ifdef PTHREAD_DEBUG
		pthread_t self = pthread_self();
		arts_assert(((Mutex_impl *)mutex)->owner == self);
		((Mutex_impl *)mutex)->owner = 0;
#endif

		pthread_cond_wait(&cond, &((Mutex_impl*)mutex)->mutex);

#ifdef PTHREAD_DEBUG
		arts_assert(((Mutex_impl *)mutex)->owner == 0);
		((Mutex_impl *)mutex)->owner = self;
#endif
	}
};

class Semaphore_impl : public Arts::Semaphore_impl
{
private:
	sem_t semaphore;

public:
	Semaphore_impl(int shared, int count) {
		sem_init(&semaphore, shared, count);
	}

	~Semaphore_impl() {
		sem_destroy(&semaphore);
	}

	void wait() {
		sem_wait(&semaphore);
	}

	int tryWait() {
		return sem_trywait(&semaphore);
	}

	void post() {
		sem_post(&semaphore);
	}

	int getValue() {
		int retval;
		sem_getvalue(&semaphore, &retval);
		return retval;
	}
};

class PosixThreads : public SystemThreads {
private:
	pthread_t mainThread;
public:
	PosixThreads() {
		mainThread = pthread_self();
	}
	bool isMainThread() {
		return pthread_equal(pthread_self(), mainThread);
	}
	Arts::Mutex_impl *createMutex_impl() {
		return new Mutex_impl();
	}
	Arts::Mutex_impl *createRecMutex_impl() {
		return new RecMutex_impl();
	}
	Arts::Thread_impl *createThread_impl(Arts::Thread *thread) {
		return new Thread_impl(thread);
	}
	Arts::ThreadCondition_impl *createThreadCondition_impl() {
		return new ThreadCondition_impl();
	}
	Thread *getCurrentThread() {
		void *data = pthread_getspecific(Thread_impl::privateDataKey);

		if(data)
			return ((Thread_impl *)data)->thread;
		else
			return 0; /* main thread */
	}
	Arts::Semaphore_impl *createSemaphore_impl(int shared, int count) {
		return new Semaphore_impl(shared, count);
	}
};

// set posix threads on startup
static class SetSystemThreads {
private:
	PosixThreads posixThreads;

public:
	SetSystemThreads() {
		if(pthread_key_create(&Thread_impl::privateDataKey, 0))
			arts_debug("SystemThreads init: pthread_key_create failed");

		SystemThreads::init(&posixThreads);

	}
	~SetSystemThreads() {
		SystemThreads::init(0);

		if(pthread_key_delete(Thread_impl::privateDataKey))
			arts_debug("SystemThreads init: pthread_key_delete failed");
	}
} initOnStartup;

#include <gsl/gslcommon.h>
/* -fast- locking for gsl on platforms with unix98 support */
#if (GSL_HAVE_MUTEXATTR_SETTYPE > 0)
static void pth_mutex_init (GslMutex *mutex)
{
	/* need NULL attribute here, which is the fast mutex on glibc
	 * and cannot be chosen through the pthread_mutexattr_settype()
	 */
	pthread_mutex_init ((pthread_mutex_t*) mutex, NULL);
}
static void pth_rec_mutex_init (GslRecMutex *mutex)
{
	pthread_mutexattr_t attr;

	pthread_mutexattr_init (&attr);
	pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init ((pthread_mutex_t*) mutex, &attr);
	pthread_mutexattr_destroy (&attr);
}
static void pth_rec_cond_init (GslCond *cond)
{
	pthread_cond_init ((pthread_cond_t*) cond, NULL);
}
static void pth_rec_cond_wait_timed (GslCond *cond, GslMutex *mutex,
									 gulong abs_secs, gulong abs_usecs)
{
	struct timespec abstime;

	abstime.tv_sec = abs_secs;
	abstime.tv_nsec = abs_usecs * 1000;
	pthread_cond_timedwait ((pthread_cond_t*) cond, (pthread_mutex_t*) mutex, &abstime);
}
static GslMutexTable pth_mutex_table = {
	pth_mutex_init,
	(void (*) (GslMutex*)) pthread_mutex_lock,
	(int  (*) (GslMutex*)) pthread_mutex_trylock,
	(void (*) (GslMutex*)) pthread_mutex_unlock,
	(void (*) (GslMutex*)) pthread_mutex_destroy,
	pth_rec_mutex_init,
	(void (*) (GslRecMutex*)) pthread_mutex_lock,
	(int  (*) (GslRecMutex*)) pthread_mutex_trylock,
	(void (*) (GslRecMutex*)) pthread_mutex_unlock,
	(void (*) (GslRecMutex*)) pthread_mutex_destroy,
	pth_rec_cond_init,
	(void (*)            (GslCond*)) pthread_cond_signal,
	(void (*)            (GslCond*)) pthread_cond_broadcast,
	(void (*) (GslCond*, GslMutex*)) pthread_cond_wait,
	pth_rec_cond_wait_timed,
	(void (*)            (GslCond*)) pthread_cond_destroy,
};

static class SetGslMutexTable {
public:
	SetGslMutexTable()
	{
		gslGlobalMutexTable = &pth_mutex_table;
	}
} initGslMutexTable;
#endif

}

}

#endif