]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_lwp.c
Define env.variable SLAPD_DEBUG to override the default debug level
[openldap] / libraries / libldap_r / thr_lwp.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_lwp.c - wrappers around SunOS LWP threads */
12
13 /* BUGS:
14  * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
15  *   they can become static.
16  */
17
18 #include "portable.h"
19 #include "ldap_pvt_thread.h"
20
21 #if defined( HAVE_LWP )
22
23 /*************
24  *           *
25  * SunOS LWP *
26  *           *
27  *************/
28 #include <stdio.h>
29
30 #include <ac/time.h>
31 #include <ac/socket.h>
32
33 #include "lber.h"
34 #include "ldap.h"
35 #include "ldap_log.h"
36
37 #include <lwp/lwp.h>
38 #include <lwp/stackdep.h>
39
40 #define MAX_STACK       51200
41 #define MAX_THREADS     20
42
43 /*
44  * Initialize LWP by spinning of a schedular
45  */
46 int
47 ldap_pvt_thread_initialize( void )
48 {
49         thread_t                tid;
50         stkalign_t              *stack;
51         int                     stackno;
52
53         if (( stack = get_stack( &stackno )) == NULL ) {
54                 return -1;
55         }
56
57         lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
58         return 0;
59 }
60
61 struct stackinfo {
62         int             stk_inuse;
63         stkalign_t      *stk_stack;
64 };
65
66 static struct stackinfo *stacks;
67
68 stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
69 {
70         int     i;
71
72         if ( stacks == NULL ) {
73                 stacks = (struct stackinfo *) ch_calloc( 1, MAX_THREADS *
74                     sizeof(struct stackinfo) );
75         }
76
77         for ( i = 0; i < MAX_THREADS; i++ ) {
78                 if ( stacks[i].stk_inuse == 0 ) {
79                         break;
80                 }
81         }
82
83         if ( i == MAX_THREADS ) {
84                 Debug( LDAP_DEBUG_ANY,
85                     "no more stacks (max %d) - increase MAX_THREADS for more",
86                     MAX_THREADS, 0, 0 );
87                 return( NULL );
88         }
89
90         if ( stacks[i].stk_stack == NULL ) {
91                 stacks[i].stk_stack = (stkalign_t *) malloc(
92                     (MAX_STACK / sizeof(stkalign_t) + 1 )
93                     * sizeof(stkalign_t) );
94         }
95
96         *stacknop = i;
97         stacks[i].stk_inuse = 1;
98         return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
99 }
100
101 void
102 ldap_pvt_thread_free_stack( int stackno )
103 {
104         if ( stackno < 0 || stackno > MAX_THREADS ) {
105                 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d",
106                     stackno, 0, 0 );
107         }
108
109         stacks[stackno].stk_inuse = 0;
110 }
111
112 static void
113 lwp_create_stack( void *(*func)(), void *arg, int stackno )
114 {
115         (*func)( arg );
116
117         ldap_pvt_thread_free_stack( stackno );
118 }
119
120 int 
121 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
122         int detach,
123         void *(*start_routine)( void *),
124         void *arg)
125 {
126         stkalign_t      *stack;
127         int             stackno;
128
129         if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
130                 return( -1 );
131         }
132         return( lwp_create( thread, lwp_create_stack, MINPRIO, 0, 
133                            stack, 3, start_routine, arg, stackno ) );
134 }
135
136 void 
137 ldap_pvt_thread_exit( void *retval )
138 {
139         lwp_destroy( SELF );
140 }
141
142 int 
143 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
144 {
145         lwp_join( thread );
146         return 0;
147 }
148
149 int 
150 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
151 {
152         return 0;
153 }
154
155 int 
156 ldap_pvt_thread_yield( void )
157 {
158         lwp_yield( SELF );
159         return 0;
160 }
161
162 int 
163 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
164 {
165         /*
166          * lwp cv_create requires the monitor id be passed in
167          * when the cv is created, pthreads passes it when the
168          * condition is waited for.  so, we fake the creation
169          * here and actually do it when the cv is waited for
170          * later.
171          */
172
173         cond->lcv_created = 0;
174
175         return( 0 );
176 }
177
178 int 
179 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
180 {
181         return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
182 }
183
184 int 
185 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
186                       ldap_pvt_thread_mutex_t *mutex )
187 {
188         if ( ! cond->lcv_created ) {
189                 cv_create( &cond->lcv_cv, *mutex );
190                 cond->lcv_created = 1;
191         }
192
193         return( cv_wait( cond->lcv_cv ) );      
194 }
195
196 int 
197 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
198 {
199         return( mon_create( mutex ) );
200 }
201
202 int 
203 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
204 {
205         return( mon_destroy( *mutex ) );
206 }
207
208 int 
209 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
210 {
211         return( mon_enter( *mutex ) );
212 }
213
214 int 
215 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
216 {
217         return( mon_exit( *mutex ) );
218 }
219
220 int
221 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
222 {
223         return( mon_cond_enter( *mp ) );
224 }
225
226 int
227 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
228 {
229         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
230 }
231
232 int
233 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
234 {
235         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
236 }
237
238 #endif /* HAVE_LWP */