]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_pth.c
ITS#5490
[openldap] / libraries / libldap_r / thr_pth.c
1 /* thr_pth.c - wrappers around GNU Pth */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #if defined( HAVE_GNU_PTH )
20
21 #include "ldap_pvt_thread.h" /* Get the thread interface */
22 #define LDAP_THREAD_IMPLEMENTATION
23 #define LDAP_THREAD_RDWR_IMPLEMENTATION
24 #include "ldap_thr_debug.h"      /* May rename the symbols defined below */
25
26 #include <errno.h>
27
28 /*******************
29  *                 *
30  * GNU Pth Threads *
31  *                 *
32  *******************/
33
34 static pth_attr_t detach_attr;
35 static pth_attr_t joined_attr;
36
37 int
38 ldap_int_thread_initialize( void )
39 {
40         if( !pth_init() ) {
41                 return -1;
42         }
43         detach_attr = pth_attr_new();
44         joined_attr = pth_attr_new();
45 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
46         pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
47         pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
48 #endif
49         return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
50 }
51
52 int
53 ldap_int_thread_destroy( void )
54 {
55         pth_attr_destroy(detach_attr);
56         pth_kill();
57         return 0;
58 }
59
60 int 
61 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
62         int detach,
63         void *(*start_routine)( void *),
64         void *arg)
65 {
66         *thread = pth_spawn( detach ? detach_attr : joined_attr,
67                 start_routine, arg );
68
69         return *thread == NULL ? errno : 0;
70 }
71
72 void 
73 ldap_pvt_thread_exit( void *retval )
74 {
75         pth_exit( retval );
76 }
77
78 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
79 {
80         return pth_join( thread, thread_return ) ? 0 : errno;
81 }
82
83 int 
84 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
85 {
86         return pth_raise( thread, signo ) ? 0 : errno;
87 }
88         
89 int 
90 ldap_pvt_thread_yield( void )
91 {
92         return pth_yield(NULL) ? 0 : errno;
93 }
94
95 int 
96 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
97 {
98         return( pth_cond_init( cond ) ? 0 : errno );
99 }
100
101 int 
102 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
103 {
104         return( pth_cond_notify( cond, 0 ) ? 0 : errno );
105 }
106
107 int
108 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
109 {
110         return( pth_cond_notify( cond, 1 ) ? 0 : errno );
111 }
112
113 int 
114 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
115         ldap_pvt_thread_mutex_t *mutex )
116 {
117         return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
118 }
119
120 int
121 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
122 {
123         return 0;
124 }
125
126 int 
127 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
128 {
129         return( pth_mutex_init( mutex ) ? 0 : errno );
130 }
131
132 int 
133 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
134 {
135         return 0;
136 }
137
138 int 
139 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
140 {
141         return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
142 }
143
144 int 
145 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
146 {
147         return( pth_mutex_release( mutex ) ? 0 : errno );
148 }
149
150 int
151 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
152 {
153         return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
154 }
155
156 ldap_pvt_thread_t
157 ldap_pvt_thread_self( void )
158 {
159         return pth_self();
160 }
161
162 #ifdef LDAP_THREAD_HAVE_RDWR
163 int 
164 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
165 {
166         return pth_rwlock_init( rw ) ? 0 : errno;
167 }
168
169 int 
170 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
171 {
172         return 0;
173 }
174
175 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
176 {
177         return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
178 }
179
180 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
181 {
182         return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
183 }
184
185 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
186 {
187         return pth_rwlock_release( rw ) ? 0 : errno;
188 }
189
190 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
191 {
192         return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
193 }
194
195 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
196 {
197         return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
198 }
199
200 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
201 {
202         return pth_rwlock_release( rw ) ? 0 : errno;
203 }
204
205 #endif /* LDAP_THREAD_HAVE_RDWR */
206 #endif /* HAVE_GNU_PTH */