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