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