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