]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_posix.c
Posix Draft 4 (HAVE_PTHREADS_D4) fixes:
[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 #include <ac/errno.h>
17
18 #include "ldap_pvt_thread.h"
19
20 #if defined( HAVE_PTHREADS )
21
22 #if HAVE_PTHREADS_D4
23 #  define LDAP_PVT_THREAD_ATTR_DEFAULT          pthread_attr_default
24 #  define LDAP_PVT_THREAD_CONDATTR_DEFAULT      pthread_condattr_default
25 #  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT     pthread_mutexattr_default
26 #else
27 #  define LDAP_PVT_THREAD_ATTR_DEFAULT          NULL
28 #  define LDAP_PVT_THREAD_CONDATTR_DEFAULT      NULL
29 #  define LDAP_PVT_THREAD_MUTEXATTR_DEFAULT     NULL
30 #endif
31
32
33 int
34 ldap_pvt_thread_initialize( void )
35 {
36 #if defined( LDAP_THREAD_CONCURRENCY ) && HAVE_PTHREAD_SETCONCURRENCY
37         ldap_pvt_thread_set_concurrency( LDAP_THREAD_CONCURRENCY );
38 #endif
39         return 0;
40 }
41
42 #ifdef HAVE_PTHREAD_SETCONCURRENCY
43 int
44 ldap_pvt_thread_set_concurrency(int n)
45 {
46 #ifdef HAVE_PTHREAD_SETCONCURRENCY
47         return pthread_setconcurrency( n );
48 #elif HAVE_THR_SETCONCURRENCY
49         return pthread_setconcurrency( n );
50 #else
51         return 0;
52 #endif
53 }
54 #endif
55
56 #ifdef HAVE_PTHREAD_GETCONCURRENCY
57 int
58 ldap_pvt_thread_get_concurrency(void)
59 {
60 #ifdef HAVE_PTHREAD_GETCONCURRENCY
61         return pthread_getconcurrency();
62 #elif HAVE_THR_GETCONCURRENCY
63         return pthread_getconcurrency();
64 #else
65         return 0;
66 #endif
67 }
68 #endif
69
70 int 
71 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
72         int detach,
73         void *(*start_routine)( void * ),
74         void *arg)
75 {
76         int rtn = pthread_create( thread, LDAP_PVT_THREAD_ATTR_DEFAULT,
77                                   start_routine, arg );
78
79         if( detach ) {
80 #ifdef HAVE_PTHREADS_FINAL
81                 pthread_detach( *thread );
82 #else
83                 pthread_detach( thread );
84 #endif
85         }
86         return rtn;
87 }
88
89 void 
90 ldap_pvt_thread_exit( void *retval )
91 {
92         pthread_exit( retval );
93 }
94
95 int 
96 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
97 {
98 #if !defined( HAVE_PTHREADS_FINAL )
99         void *dummy;
100         if (thread_return==NULL)
101           thread_return=&dummy;
102 #endif  
103         return pthread_join( thread, thread_return );
104 }
105
106 int 
107 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
108 {
109 #ifdef HAVE_PTHREAD_KILL
110         return pthread_kill( thread, signo );
111 #else
112         /* pthread package with DCE */
113         if (kill( getpid(), signo )<0)
114                 return errno;
115         return 0;
116 #endif
117 }
118
119 int 
120 ldap_pvt_thread_yield( void )
121 {
122 #ifdef HAVE_SCHED_YIELD
123         return sched_yield();
124 #elif HAVE_PTHREAD_YIELD
125         pthread_yield();
126         return 0;
127 #elif HAVE_THR_YIELD
128         return thr_yield();
129 #else
130         return 0;
131 #endif   
132 }
133
134 int 
135 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
136 {
137         return pthread_cond_init( cond, LDAP_PVT_THREAD_CONDATTR_DEFAULT );
138 }
139
140 int 
141 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cond )
142 {
143         return pthread_cond_destroy( cond );
144 }
145         
146 int 
147 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
148 {
149         return pthread_cond_signal( cond );
150 }
151
152 int
153 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
154 {
155         return pthread_cond_broadcast( cond );
156 }
157
158 int 
159 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
160                       ldap_pvt_thread_mutex_t *mutex )
161 {
162         return pthread_cond_wait( cond, mutex );
163 }
164
165 int 
166 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
167 {
168         return pthread_mutex_init( mutex, LDAP_PVT_THREAD_MUTEXATTR_DEFAULT );
169 }
170
171 int 
172 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
173 {
174         return pthread_mutex_destroy( mutex );
175 }
176
177 int 
178 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
179 {
180         return pthread_mutex_lock( mutex );
181 }
182
183 int 
184 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
185 {
186         return pthread_mutex_unlock( mutex );
187 }
188
189 #endif /* HAVE_PTHREADS */
190