]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_pth.c
Additional thread set stack size patch from HEAD
[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-2005 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"
22 #include <errno.h>
23
24 /*******************
25  *                 *
26  * GNU Pth Threads *
27  *                 *
28  *******************/
29
30 static pth_attr_t detach_attr;
31 static pth_attr_t joined_attr;
32
33 int
34 ldap_int_thread_initialize( void )
35 {
36         if( !pth_init() ) {
37                 return -1;
38         }
39         detach_attr = pth_attr_new();
40         joined_attr = pth_attr_new();
41 #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE
42         pth_attr_set( joined_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
43         pth_attr_set( detach_attr, PTH_ATTR_STACK_SIZE, LDAP_PVT_THREAD_STACK_SIZE );
44 #endif
45         return pth_attr_set( detach_attr, PTH_ATTR_JOINABLE, FALSE );
46 }
47
48 int
49 ldap_int_thread_destroy( void )
50 {
51         pth_attr_destroy(detach_attr);
52         pth_kill();
53         return 0;
54 }
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         *thread = pth_spawn( detach ? detach_attr : joined_attr,
63                 start_routine, arg );
64
65         return *thread == NULL ? errno : 0;
66 }
67
68 void 
69 ldap_pvt_thread_exit( void *retval )
70 {
71         pth_exit( retval );
72 }
73
74 int ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
75 {
76         return pth_join( thread, thread_return ) ? 0 : errno;
77 }
78
79 int 
80 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
81 {
82         return pth_raise( thread, signo ) ? 0 : errno;
83 }
84         
85 int 
86 ldap_pvt_thread_yield( void )
87 {
88         return pth_yield(NULL) ? 0 : errno;
89 }
90
91 int 
92 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
93 {
94         return( pth_cond_init( cond ) ? 0 : errno );
95 }
96
97 int 
98 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
99 {
100         return( pth_cond_notify( cond, 0 ) ? 0 : errno );
101 }
102
103 int
104 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cond )
105 {
106         return( pth_cond_notify( cond, 1 ) ? 0 : errno );
107 }
108
109 int 
110 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
111         ldap_pvt_thread_mutex_t *mutex )
112 {
113         return( pth_cond_await( cond, mutex, NULL ) ? 0 : errno );
114 }
115
116 int
117 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
118 {
119         return 0;
120 }
121
122 int 
123 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
124 {
125         return( pth_mutex_init( mutex ) ? 0 : errno );
126 }
127
128 int 
129 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
130 {
131         return 0;
132 }
133
134 int 
135 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
136 {
137         return( pth_mutex_acquire( mutex, 0, NULL ) ? 0 : errno );
138 }
139
140 int 
141 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
142 {
143         return( pth_mutex_release( mutex ) ? 0 : errno );
144 }
145
146 int
147 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mutex )
148 {
149         return( pth_mutex_acquire( mutex, 1, NULL ) ? 0 : errno );
150 }
151
152 ldap_pvt_thread_t
153 ldap_pvt_thread_self( void )
154 {
155         return pth_self();
156 }
157
158 #ifdef LDAP_THREAD_HAVE_RDWR
159 int 
160 ldap_pvt_thread_rdwr_init( ldap_pvt_thread_rdwr_t *rw )
161 {
162         return pth_rwlock_init( rw ) ? 0 : errno;
163 }
164
165 int 
166 ldap_pvt_thread_rdwr_destroy( ldap_pvt_thread_rdwr_t *rw )
167 {
168         return 0;
169 }
170
171 int ldap_pvt_thread_rdwr_rlock( ldap_pvt_thread_rdwr_t *rw )
172 {
173         return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 0, NULL ) ? 0 : errno;
174 }
175
176 int ldap_pvt_thread_rdwr_rtrylock( ldap_pvt_thread_rdwr_t *rw )
177 {
178         return pth_rwlock_acquire( rw, PTH_RWLOCK_RD, 1, NULL ) ? 0 : errno;
179 }
180
181 int ldap_pvt_thread_rdwr_runlock( ldap_pvt_thread_rdwr_t *rw )
182 {
183         return pth_rwlock_release( rw ) ? 0 : errno;
184 }
185
186 int ldap_pvt_thread_rdwr_wlock( ldap_pvt_thread_rdwr_t *rw )
187 {
188         return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 0, NULL ) ? 0 : errno;
189 }
190
191 int ldap_pvt_thread_rdwr_wtrylock( ldap_pvt_thread_rdwr_t *rw )
192 {
193         return pth_rwlock_acquire( rw, PTH_RWLOCK_RW, 1, NULL ) ? 0 : errno;
194 }
195
196 int ldap_pvt_thread_rdwr_wunlock( ldap_pvt_thread_rdwr_t *rw )
197 {
198         return pth_rwlock_release( rw ) ? 0 : errno;
199 }
200
201 #endif /* LDAP_THREAD_HAVE_RDWR */
202 #endif /* HAVE_GNU_PTH */