2 * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
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.
11 /* thr_lwp.c - wrappers around SunOS LWP threads */
14 * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
15 * they can become static.
20 #if defined( HAVE_LWP )
28 /* This implementation NEEDS WORK. It currently does not compile */
33 #include <ac/socket.h>
37 #include "ldap_pvt_thread.h"
40 #include <lwp/stackdep.h>
42 #define MAX_STACK 51200
43 #define MAX_THREADS 20
46 * Initialize LWP by spinning of a schedular
49 ldap_pvt_thread_initialize( void )
55 if (( stack = get_stack( &stackno )) == NULL ) {
59 lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
64 ldap_pvt_thread_destroy( void )
66 /* need to destory lwp_scheduler thread and clean up private
73 stkalign_t *stk_stack;
76 static struct stackinfo *stacks;
78 static stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
82 if ( stacks == NULL ) {
83 stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
84 sizeof(struct stackinfo) );
86 if( stacks == NULL ) {
87 Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
93 for ( i = 0; i < MAX_THREADS; i++ ) {
94 if ( stacks[i].stk_inuse == 0 ) {
99 if ( i == MAX_THREADS ) {
100 Debug( LDAP_DEBUG_ANY,
101 "no more stacks (max %d) - increase MAX_THREADS for more",
106 if ( stacks[i].stk_stack == NULL ) {
107 stacks[i].stk_stack = (stkalign_t *) LDAP_MALLOC(
108 (MAX_STACK / sizeof(stkalign_t) + 1 )
109 * sizeof(stkalign_t) );
111 if( stacks[i].stk_stack == NULL ) {
112 Debug( LDAP_DEBUG_ANY, "stack allocation failed",
119 stacks[i].stk_inuse = 1;
120 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
124 ldap_pvt_thread_free_stack( int stackno )
126 if ( stackno < 0 || stackno > MAX_THREADS ) {
127 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
131 stacks[stackno].stk_inuse = 0;
135 lwp_create_stack( void *(*func)(), void *arg, int stackno )
139 ldap_pvt_thread_free_stack( stackno );
143 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
145 void *(*start_routine)( void *),
151 if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
154 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
155 stack, 3, start_routine, arg, stackno ) );
159 ldap_pvt_thread_exit( void *retval )
165 ldap_pvt_thread_sleep(
166 unsigned int interval
174 if ( lwp_self( &mylwp ) < 0 ) {
180 mon_enter( &sglob->tsl_mon );
182 if ( sglob->tsl_list != NULL ) {
183 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
184 if ( SAMETHREAD( t->tl_tid, mylwp )) {
185 /* We're already sleeping? */
186 t->tl_wake = now + interval;
187 mon_exit( &sglob->tsl_mon );
188 lwp_suspend( mylwp );
194 nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
196 if( nt == NULL ) return -1;
198 nt->tl_next = sglob->tsl_list;
199 nt->tl_wake = now + interval;
201 sglob->tsl_list = nt;
203 mon_exit( &sglob->tsl_mon );
205 lwp_suspend( mylwp );
210 * The lwp_scheduler thread periodically checks to see if any threads
211 * are due to be resumed. If there are, it resumes them. Otherwise,
212 * it computes the lesser of ( 1 second ) or ( the minimum time until
213 * a thread need to be resumed ) and puts itself to sleep for that amount
222 struct timeval interval;
225 while ( !sglob->slurpd_shutdown ) {
226 mon_enter( &sglob->tsl_mon );
230 if ( sglob->tsl_list != NULL ) {
231 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
232 if (( t->tl_wake > 0L ) && ( t->tl_wake < now )) {
233 lwp_resume( t->tl_tid );
237 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
243 mon_exit( &sglob->tsl_mon );
245 interval.tv_usec = 0L;
247 interval.tv_sec = 1L;
249 interval.tv_sec = min;
252 lwp_sleep( &interval );
255 mon_enter( &sglob->tsl_mon );
257 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
258 lwp_resume( t->tl_tid );
261 mon_exit( &sglob->tsl_mon );
263 free_stack( stackno );
267 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
274 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
280 ldap_pvt_thread_yield( void )
287 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
290 * lwp cv_create requires the monitor id be passed in
291 * when the cv is created, pthreads passes it when the
292 * condition is waited for. so, we fake the creation
293 * here and actually do it when the cv is waited for
297 cond->lcv_created = 0;
303 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
305 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
309 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
310 ldap_pvt_thread_mutex_t *mutex )
312 if ( ! cond->lcv_created ) {
313 cv_create( &cond->lcv_cv, *mutex );
314 cond->lcv_created = 1;
317 return( cv_wait( cond->lcv_cv ) );
321 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
323 return( mon_create( mutex ) );
327 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
329 return( mon_destroy( *mutex ) );
333 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
335 return( mon_enter( *mutex ) );
339 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
341 return( mon_exit( *mutex ) );
345 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
347 return( mon_cond_enter( *mp ) );
351 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
353 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
357 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
359 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
362 #endif /* HAVE_LWP */