]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Clarify error string, add comment
[openldap] / libraries / libldap_r / thr_posix.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 /* thr_posix.c - wrapper around posix and posixish thread implementations.
13  */
14
15 #include "portable.h"
16
17 #if defined( HAVE_PTHREADS )
18
19 #include <ac/errno.h>
20
21 #include "ldap_pvt_thread.h"
22
23
24 #if HAVE_PTHREADS_D4
25 #  define LDAP_PVT_THREAD_ATTR_DEFAULT          pthread_attr_default
26 #  define LDAP_PVT_THREAD_CONDATTR_DEFAULT      pthread_condattr_default
27 #  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT     pthread_mutexattr_default
28 #else
29 #  define LDAP_PVT_THREAD_ATTR_DEFAULT          NULL
30 #  define LDAP_PVT_THREAD_CONDATTR_DEFAULT      NULL
31 #  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT     NULL
32 #endif
33
34
35 int
36 ldap_pvt_thread_initialize( void )
37 {
38 #if defined( LDAP_THREAD_CONCURRENCY ) && HAVE_PTHREAD_SETCONCURRENCY
39         ldap_pvt_thread_set_concurrency( LDAP_THREAD_CONCURRENCY );
40 #endif
41         return 0;
42 }
43
44 int
45 ldap_pvt_thread_destroy( void )
46 {
47         return 0;
48 }
49
50 #ifdef HAVE_PTHREAD_SETCONCURRENCY
51 int
52 ldap_pvt_thread_set_concurrency(int n)
53 {
54 #ifdef HAVE_PTHREAD_SETCONCURRENCY
55         return pthread_setconcurrency( n );
56 #elif HAVE_THR_SETCONCURRENCY
57         return pthread_setconcurrency( n );
58 #else
59         return 0;
60 #endif
61 }
62 #endif
63
64 #ifdef HAVE_PTHREAD_GETCONCURRENCY
65 int
66 ldap_pvt_thread_get_concurrency(void)
67 {
68 #ifdef HAVE_PTHREAD_GETCONCURRENCY
69         return pthread_getconcurrency();
70 #elif HAVE_THR_GETCONCURRENCY
71         return pthread_getconcurrency();
72 #else
73         return 0;
74 #endif
75 }
76 #endif
77
78 int 
79 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
80         int detach,
81         void *(*start_routine)( void * ),
82         void *arg)
83 {
84         int rtn = pthread_create( thread, LDAP_PVT_THREAD_ATTR_DEFAULT,
85                                   start_routine, arg );
86
87         if( detach ) {
88 #ifdef HAVE_PTHREADS_FINAL
89                 pthread_detach( *thread );
90 #else
91                 pthread_detach( thread );
92 #endif
93         }
94         return rtn;
95 }
96
97 void 
98 ldap_pvt_thread_exit( void *retval )
99 {
100         pthread_exit( retval );
101 }
102
103 int 
104 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
105 {
106 #if !defined( HAVE_PTHREADS_FINAL )
107         void *dummy;
108         if (thread_return==NULL)
109           thread_return=&dummy;
110 #endif  
111         return pthread_join( thread, thread_return );
112 }
113
114 int 
115 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
116 {
117 #ifdef HAVE_PTHREAD_KILL
118         return pthread_kill( thread, signo );
119 #else
120         /* pthread package with DCE */
121         if (kill( getpid(), signo )<0)
122                 return errno;
123         return 0;
124 #endif
125 }
126
127 int 
128 ldap_pvt_thread_yield( void )
129 {
130 #ifdef _POSIX_THREAD_IS_GNU_PTH
131         sched_yield();
132         return 0;
133
134 #elif HAVE_SCHED_YIELD
135         return sched_yield();
136
137 #elif HAVE_PTHREAD_YIELD
138         pthread_yield();
139         return 0;
140
141 #elif HAVE_THR_YIELD
142         return thr_yield();
143
144 #else
145         return 0;
146 #endif   
147 }
148
149 int 
150 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
151 {
152         return pthread_cond_init( cond, LDAP_PVT_THREAD_CONDATTR_DEFAULT );
153 }
154
155 int 
156 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
157 {
158         return pthread_cond_destroy( cond );
159 }
160         
161 int 
162 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
163 {
164         return pthread_cond_signal( cond );
165 }
166
167 int
168 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
169 {
170         return pthread_cond_broadcast( cond );
171 }
172
173 int 
174 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
175                       ldap_pvt_thread_mutex_t *mutex )
176 {
177         return pthread_cond_wait( cond, mutex );
178 }
179
180 int 
181 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
182 {
183         return pthread_mutex_init( mutex, LDAP_PVT_THREAD_MUTEXATTR_DEFAULT );
184 }
185
186 int 
187 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
188 {
189         return pthread_mutex_destroy( mutex );
190 }
191
192 int 
193 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
194 {
195         return pthread_mutex_lock( mutex );
196 }
197
198 int 
199 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
200 {
201         return pthread_mutex_trylock( mutex );
202 }
203
204 int 
205 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
206 {
207         return pthread_mutex_unlock( mutex );
208 }
209
210 #endif /* HAVE_PTHREADS */
211