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