1 /*
2 ** ui_thr.h - Pthreads over UI Threads sort-of-compatibility header
3 **
4 ** Copyright (c) 1997-2000 Peter Eriksson <pen@lysator.liu.se>
5 **
6 ** This program is free software; you can redistribute it and/or
7 ** modify it as you wish - as long as you don't claim that you wrote
8 ** it.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 */
14 
15 #ifndef PLIB_UI_THR_H
16 #define PLIB_UI_THR_H
17 
18 #include <thread.h>
19 #include <errno.h>
20 
21 
22 #define pthread_mutex_t			mutex_t
23 #define pthread_mutex_init(mp,ap)	mutex_init(mp,0,NULL)
24 #define pthread_mutex_lock(mp)		mutex_lock(mp)
25 #define pthread_mutex_unlock(mp)	mutex_unlock(mp)
26 #define pthread_mutex_destroy(mp)	mutex_destroy(mp)
27 
28 #define pthread_cond_t			cond_t
29 #define pthread_cond_init(mp,ap)	cond_init(mp,0,NULL)
30 #define pthread_cond_wait(cp,mp)	cond_wait(cp,mp)
31 #define pthread_cond_timedwait(cp,mp,t)	cond_timedwait(cp,mp,t)
32 #define pthread_cond_signal(cp)		cond_signal(cp)
33 #define pthread_cond_broadcast(cp)	cond_broadcast(cp)
34 #define pthread_cond_destroy(cp)	cond_destroy(cp)
35 
36 #define pthread_attr_t			int
37 #define pthread_attr_init(ap)		(*(ap) = 0)
38 
39 #define pthread_t			thread_t
40 
41 #define PTHREAD_CREATE_DETACHED		THR_DETACHED
42 #define PTHREAD_CREATE_JOINABLE		0
43 
44 #define pthread_attr_setdetachstate(ap,state)	((*(ap)) = state)
45 
46 #define pthread_create(tidp,attrp,func,arg)	\
47     	thr_create(NULL, 0, (func), (arg), ((attrp) ? *((int *) attrp) : 0), (tidp))
48 
49 #define pthread_join(tid, statusp)	thr_join(tid, NULL, statusp)
50 
51 
52 typedef struct
53 {
54     mutex_t lock;
55     int f;
56 } my_pthread_once_t;
57 #define pthread_once_t my_pthread_once_t
58 
59 #define PTHREAD_ONCE_INIT		{ DEFAULTMUTEX, 0 }
60 
61 #define pthread_once(ov,fun)	\
62 { \
63     mutex_lock(&(ov)->lock); \
64     if ((ov)->f == 0) \
65     { \
66 	(ov)->f = 1; \
67 	fun(); \
68     } \
69     mutex_unlock(&(ov)->lock); \
70 }
71 
72 #endif
73