1 /*
2  * pthreads wrapper functions.
3  */
4 
5 #include	"unp.h"
6 #include	"unpthread.h"
7 
8 void
Pthread_create(pthread_t * tid,const pthread_attr_t * attr,void * (* func)(void *),void * arg)9 Pthread_create(pthread_t *tid, const pthread_attr_t *attr,
10 			   void * (*func)(void *), void *arg)
11 {
12 	int		n;
13 
14 	if ( (n = pthread_create(tid, attr, func, arg)) == 0)
15 		return;
16 	errno = n;
17 	err_sys("pthread_create error");
18 }
19 
20 void
Pthread_join(pthread_t tid,void ** status)21 Pthread_join(pthread_t tid, void **status)
22 {
23 	int		n;
24 
25 	if ( (n = pthread_join(tid, status)) == 0)
26 		return;
27 	errno = n;
28 	err_sys("pthread_join error");
29 }
30 
31 void
Pthread_detach(pthread_t tid)32 Pthread_detach(pthread_t tid)
33 {
34 	int		n;
35 
36 	if ( (n = pthread_detach(tid)) == 0)
37 		return;
38 	errno = n;
39 	err_sys("pthread_detach error");
40 }
41 
42 void
Pthread_mutexattr_init(pthread_mutexattr_t * attr)43 Pthread_mutexattr_init(pthread_mutexattr_t *attr)
44 {
45 	int		n;
46 
47 	if ( (n = pthread_mutexattr_init(attr)) == 0)
48 		return;
49 	errno = n;
50 	err_sys("pthread_mutexattr_init error");
51 }
52 
53 #ifdef	_POSIX_THREAD_PROCESS_SHARED
54 void
Pthread_mutexattr_setpshared(pthread_mutexattr_t * attr,int flag)55 Pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int flag)
56 {
57 	int		n;
58 
59 	if ( (n = pthread_mutexattr_setpshared(attr, flag)) == 0)
60 		return;
61 	errno = n;
62 	err_sys("pthread_mutexattr_setpshared error");
63 }
64 #endif
65 
66 void
Pthread_mutex_init(pthread_mutex_t * mptr,pthread_mutexattr_t * attr)67 Pthread_mutex_init(pthread_mutex_t *mptr, pthread_mutexattr_t *attr)
68 {
69 	int		n;
70 
71 	if ( (n = pthread_mutex_init(mptr, attr)) == 0)
72 		return;
73 	errno = n;
74 	err_sys("pthread_mutex_init error");
75 }
76 
77 /* include Pthread_mutex_lock */
78 void
Pthread_mutex_lock(pthread_mutex_t * mptr)79 Pthread_mutex_lock(pthread_mutex_t *mptr)
80 {
81 	int		n;
82 
83 	if ( (n = pthread_mutex_lock(mptr)) == 0)
84 		return;
85 	errno = n;
86 	err_sys("pthread_mutex_lock error");
87 }
88 /* end Pthread_mutex_lock */
89 
90 void
Pthread_mutex_unlock(pthread_mutex_t * mptr)91 Pthread_mutex_unlock(pthread_mutex_t *mptr)
92 {
93 	int		n;
94 
95 	if ( (n = pthread_mutex_unlock(mptr)) == 0)
96 		return;
97 	errno = n;
98 	err_sys("pthread_mutex_unlock error");
99 }
100 
101 void
Pthread_cond_broadcast(pthread_cond_t * cptr)102 Pthread_cond_broadcast(pthread_cond_t *cptr)
103 {
104 	int		n;
105 
106 	if ( (n = pthread_cond_broadcast(cptr)) == 0)
107 		return;
108 	errno = n;
109 	err_sys("pthread_cond_broadcast error");
110 }
111 
112 void
Pthread_cond_signal(pthread_cond_t * cptr)113 Pthread_cond_signal(pthread_cond_t *cptr)
114 {
115 	int		n;
116 
117 	if ( (n = pthread_cond_signal(cptr)) == 0)
118 		return;
119 	errno = n;
120 	err_sys("pthread_cond_signal error");
121 }
122 
123 void
Pthread_cond_wait(pthread_cond_t * cptr,pthread_mutex_t * mptr)124 Pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr)
125 {
126 	int		n;
127 
128 	if ( (n = pthread_cond_wait(cptr, mptr)) == 0)
129 		return;
130 	errno = n;
131 	err_sys("pthread_cond_wait error");
132 }
133 
134 void
Pthread_cond_timedwait(pthread_cond_t * cptr,pthread_mutex_t * mptr,const struct timespec * tsptr)135 Pthread_cond_timedwait(pthread_cond_t *cptr, pthread_mutex_t *mptr,
136 					   const struct timespec *tsptr)
137 {
138 	int		n;
139 
140 	if ( (n = pthread_cond_timedwait(cptr, mptr, tsptr)) == 0)
141 		return;
142 	errno = n;
143 	err_sys("pthread_cond_timedwait error");
144 }
145 
146 void
Pthread_once(pthread_once_t * ptr,void (* func)(void))147 Pthread_once(pthread_once_t *ptr, void (*func)(void))
148 {
149 	int		n;
150 
151 	if ( (n = pthread_once(ptr, func)) == 0)
152 		return;
153 	errno = n;
154 	err_sys("pthread_once error");
155 }
156 
157 void
Pthread_key_create(pthread_key_t * key,void (* func)(void *))158 Pthread_key_create(pthread_key_t *key, void (*func)(void *))
159 {
160 	int		n;
161 
162 	if ( (n = pthread_key_create(key, func)) == 0)
163 		return;
164 	errno = n;
165 	err_sys("pthread_key_create error");
166 }
167 
168 void
Pthread_setspecific(pthread_key_t key,const void * value)169 Pthread_setspecific(pthread_key_t key, const void *value)
170 {
171 	int		n;
172 
173 	if ( (n = pthread_setspecific(key, value)) == 0)
174 		return;
175 	errno = n;
176 	err_sys("pthread_setspecific error");
177 }
178