]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_lwp.c
Don't implicitly inherit the default SSL_CTX, tls.c:alloc_handle will
[openldap] / libraries / libldap_r / thr_lwp.c
1 /* thr_lwp.c - wrappers around SunOS LWP threads */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 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 /* BUGS:
18  * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
19  *   they can become static.
20  */
21
22 #include "portable.h"
23
24 #if defined( HAVE_LWP )
25
26 /*************
27  *           *
28  * SunOS LWP *
29  *           *
30  *************/
31
32 /* This implementation NEEDS WORK.   It currently does not compile */
33
34 #include <stdio.h>
35
36 #include <ac/time.h>
37 #include <ac/socket.h>
38
39 #include "ldap-int.h"
40
41 #include "ldap_pvt_thread.h" /* Get the thread interface */
42 #define LDAP_THREAD_IMPLEMENTATION
43 #include "ldap_thr_debug.h"      /* May rename the symbols defined below */
44
45 #include <lwp/lwp.h>
46 #include <lwp/stackdep.h>
47
48 #define MAX_STACK       51200
49 #define MAX_THREADS     20
50
51 /*
52  * Initialize LWP by spinning of a schedular
53  */
54 int
55 ldap_int_thread_initialize( void )
56 {
57         thread_t                tid;
58         stkalign_t              *stack;
59         int                     stackno;
60
61         if (( stack = get_stack( &stackno )) == NULL ) {
62                 return -1;
63         }
64
65         lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
66         return 0;
67 }
68
69 int
70 ldap_int_thread_destroy( void )
71 {
72         /* need to destroy lwp_scheduler thread and clean up private
73                 variables */
74         return 0;
75 }
76
77 struct stackinfo {
78         int             stk_inuse;
79         stkalign_t      *stk_stack;
80 };
81
82 static struct stackinfo *stacks;
83
84 static stkalign_t * ldap_int_thread_get_stack( int *stacknop )
85 {
86         int     i;
87
88         if ( stacks == NULL ) {
89                 stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
90                     sizeof(struct stackinfo) );
91
92                 if( stacks == NULL ) {
93                         Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
94                                 0, 0, 0 );
95                         return NULL;
96                 }
97         }
98
99         for ( i = 0; i < MAX_THREADS; i++ ) {
100                 if ( stacks[i].stk_inuse == 0 ) {
101                         break;
102                 }
103         }
104
105         if ( i == MAX_THREADS ) {
106                 Debug( LDAP_DEBUG_ANY,
107                     "no more stacks (max %d) - increase MAX_THREADS for more",
108                     MAX_THREADS, 0, 0 );
109                 return( NULL );
110         }
111
112         if ( stacks[i].stk_stack == NULL ) {
113                 stacks[i].stk_stack = (stkalign_t *) LDAP_MALLOC(
114                     (MAX_STACK / sizeof(stkalign_t) + 1 )
115                     * sizeof(stkalign_t) );
116
117                 if( stacks[i].stk_stack == NULL ) {
118                         Debug( LDAP_DEBUG_ANY, "stack allocation failed",
119                                 0, 0, 0 );
120                         return( NULL );
121                 }
122         }
123
124         *stacknop = i;
125         stacks[i].stk_inuse = 1;
126         return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
127 }
128
129 static void
130 ldap_int_thread_free_stack( int stackno )
131 {
132         if ( stackno < 0 || stackno > MAX_THREADS ) {
133                 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
134                     stackno, 0, 0 );
135         }
136
137         stacks[stackno].stk_inuse = 0;
138 }
139
140 static void
141 lwp_create_stack( void *(*func)(), void *arg, int stackno )
142 {
143         (*func)( arg );
144
145         ldap_int_thread_free_stack( stackno );
146 }
147
148 int 
149 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
150         int detach,
151         void *(*start_routine)( void *),
152         void *arg)
153 {
154         stkalign_t      *stack;
155         int             stackno;
156
157         if ( (stack = ldap_int_thread_get_stack( &stackno )) == NULL ) {
158                 return( -1 );
159         }
160         return( lwp_create( thread, lwp_create_stack, MINPRIO, 0, 
161                            stack, 3, start_routine, arg, stackno ) );
162 }
163
164 void 
165 ldap_pvt_thread_exit( void *retval )
166 {
167         lwp_destroy( SELF );
168 }
169
170 unsigned int
171 ldap_pvt_thread_sleep(
172         unsigned int interval
173 )
174 {
175         thread_t                mylwp;
176         tl_t            *t, *nt;
177         time_t          now;
178
179
180         if ( lwp_self( &mylwp ) < 0 ) {
181                 return -1;
182         }
183
184         time( &now );
185
186         mon_enter( &sglob->tsl_mon );
187
188         if ( sglob->tsl_list != NULL ) {
189                 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
190                         if ( SAMETHREAD( t->tl_tid, mylwp )) {
191                                 /* We're already sleeping? */
192                                 t->tl_wake = now + interval;
193                                 mon_exit( &sglob->tsl_mon );
194                                 lwp_suspend( mylwp );
195                                 return 0;
196                         }
197                 }
198         }
199
200         nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
201
202         if( nt == NULL ) return -1;
203
204         nt->tl_next = sglob->tsl_list;
205         nt->tl_wake = now + interval;
206         nt->tl_tid = mylwp;
207         sglob->tsl_list = nt;
208
209         mon_exit( &sglob->tsl_mon );
210
211         lwp_suspend( mylwp );
212         return 0;
213 }
214
215 /*
216  * The lwp_scheduler thread periodically checks to see if any threads
217  * are due to be resumed.  If there are, it resumes them.  Otherwise,
218  * it computes the lesser of ( 1 second ) or ( the minimum time until
219  * a thread need to be resumed ) and puts itself to sleep for that amount
220  * of time.
221  */
222 static void
223 lwp_scheduler(
224         int             stackno
225 )
226 {
227         time_t                  now, min;
228         struct timeval          interval;
229         tl_t                    *t;
230
231         while ( !sglob->slurpd_shutdown ) {
232                 mon_enter( &sglob->tsl_mon );
233
234                 time( &now );
235                 min = 0L;
236                 if ( sglob->tsl_list != NULL ) {
237                         for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
238                                 if (( t->tl_wake  > 0L ) && ( t->tl_wake < now )) {
239                                         lwp_resume( t->tl_tid );
240                                         t->tl_wake = 0L;
241                                 }
242
243                                 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
244                                         min =  t->tl_wake;
245                                 }
246                         }
247                 }
248
249                 mon_exit( &sglob->tsl_mon );
250
251                 interval.tv_usec = 0L;
252                 if ( min == 0L ) {
253                         interval.tv_sec = 1L;
254                 } else {
255                         interval.tv_sec = min;
256                 }
257
258                 lwp_sleep( &interval );
259         }
260
261         mon_enter( &sglob->tsl_mon );
262
263         for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
264                 lwp_resume( t->tl_tid );
265         }
266
267         mon_exit( &sglob->tsl_mon );
268
269         free_stack( stackno );
270 }
271
272 int 
273 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
274 {
275         lwp_join( thread );
276         return 0;
277 }
278
279 int 
280 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
281 {
282         return 0;
283 }
284
285 int 
286 ldap_pvt_thread_yield( void )
287 {
288         lwp_yield( SELF );
289         return 0;
290 }
291
292 int 
293 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
294 {
295         /*
296          * lwp cv_create requires the monitor id be passed in
297          * when the cv is created, pthreads passes it when the
298          * condition is waited for.  so, we fake the creation
299          * here and actually do it when the cv is waited for
300          * later.
301          */
302
303         cond->lcv_created = 0;
304
305         return( 0 );
306 }
307
308 int 
309 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
310 {
311         return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
312 }
313
314 int 
315 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
316         ldap_pvt_thread_mutex_t *mutex )
317 {
318         if ( ! cond->lcv_created ) {
319                 cv_create( &cond->lcv_cv, *mutex );
320                 cond->lcv_created = 1;
321         }
322
323         return( cv_wait( cond->lcv_cv ) );      
324 }
325
326 int 
327 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
328 {
329         return( mon_create( mutex ) );
330 }
331
332 int 
333 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
334 {
335         return( mon_destroy( *mutex ) );
336 }
337
338 int 
339 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
340 {
341         return( mon_enter( *mutex ) );
342 }
343
344 int 
345 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
346 {
347         return( mon_exit( *mutex ) );
348 }
349
350 int
351 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
352 {
353         return( mon_cond_enter( *mp ) );
354 }
355
356 int
357 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
358 {
359         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
360 }
361
362 int
363 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
364 {
365         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
366 }
367
368 ldap_pvt_thread_t
369 ldap_pvt_thread_self( void )
370 {
371         thread_t                mylwp;
372
373         lwp_self( &mylwp );
374
375         return mylwp;
376 }
377
378 #endif /* HAVE_LWP */