]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Use getpassphrase() instead of getpass() if available.
[openldap] / libraries / libldap_r / thr_posix.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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;
85 #if defined(HAVE_PTHREADS_FINAL) && defined(PTHREAD_CREATE_UNDETACHED)
86         pthread_attr_t attr;
87
88         pthread_attr_init(&attr);
89         if (!detach)
90                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
91
92         rtn = pthread_create( thread, &attr, start_routine, arg );
93 #else
94         rtn = pthread_create( thread, LDAP_PVT_THREAD_ATTR_DEFAULT,
95                                   start_routine, arg );
96 #endif
97
98         if( detach ) {
99 #ifdef HAVE_PTHREADS_FINAL
100                 pthread_detach( *thread );
101 #else
102                 pthread_detach( thread );
103 #endif
104         }
105         return rtn;
106 }
107
108 void 
109 ldap_pvt_thread_exit( void *retval )
110 {
111         pthread_exit( retval );
112 }
113
114 int 
115 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
116 {
117 #if !defined( HAVE_PTHREADS_FINAL )
118         void *dummy;
119         if (thread_return==NULL)
120           thread_return=&dummy;
121 #endif  
122         return pthread_join( thread, thread_return );
123 }
124
125 int 
126 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
127 {
128 #ifdef HAVE_PTHREAD_KILL
129         return pthread_kill( thread, signo );
130 #else
131         /* pthread package with DCE */
132         if (kill( getpid(), signo )<0)
133                 return errno;
134         return 0;
135 #endif
136 }
137
138 int 
139 ldap_pvt_thread_yield( void )
140 {
141 #ifdef _POSIX_THREAD_IS_GNU_PTH
142         sched_yield();
143         return 0;
144
145 #elif HAVE_SCHED_YIELD
146         return sched_yield();
147
148 #elif HAVE_PTHREAD_YIELD
149         pthread_yield();
150         return 0;
151
152 #elif HAVE_THR_YIELD
153         return thr_yield();
154
155 #else
156         return 0;
157 #endif   
158 }
159
160 int 
161 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
162 {
163         return pthread_cond_init( cond, LDAP_PVT_THREAD_CONDATTR_DEFAULT );
164 }
165
166 int 
167 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
168 {
169         return pthread_cond_destroy( cond );
170 }
171         
172 int 
173 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
174 {
175         return pthread_cond_signal( cond );
176 }
177
178 int
179 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
180 {
181         return pthread_cond_broadcast( cond );
182 }
183
184 int 
185 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
186                       ldap_pvt_thread_mutex_t *mutex )
187 {
188         return pthread_cond_wait( cond, mutex );
189 }
190
191 int 
192 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
193 {
194         return pthread_mutex_init( mutex, LDAP_PVT_THREAD_MUTEXATTR_DEFAULT );
195 }
196
197 int 
198 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
199 {
200         return pthread_mutex_destroy( mutex );
201 }
202
203 int 
204 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
205 {
206         return pthread_mutex_lock( mutex );
207 }
208
209 int 
210 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
211 {
212         return pthread_mutex_trylock( mutex );
213 }
214
215 int 
216 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
217 {
218         return pthread_mutex_unlock( mutex );
219 }
220
221 #endif /* HAVE_PTHREADS */
222