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