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) );
87 for ( i = 0; i < MAX_THREADS; i++ ) {
88 if ( stacks[i].stk_inuse == 0 ) {
93 if ( i == MAX_THREADS ) {
94 Debug( LDAP_DEBUG_ANY,
95 "no more stacks (max %d) - increase MAX_THREADS for more",
100 if ( stacks[i].stk_stack == NULL ) {
101 stacks[i].stk_stack = (stkalign_t *) malloc(
102 (MAX_STACK / sizeof(stkalign_t) + 1 )
103 * sizeof(stkalign_t) );
107 stacks[i].stk_inuse = 1;
108 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
112 ldap_pvt_thread_free_stack( int stackno )
114 if ( stackno < 0 || stackno > MAX_THREADS ) {
115 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
119 stacks[stackno].stk_inuse = 0;
123 lwp_create_stack( void *(*func)(), void *arg, int stackno )
127 ldap_pvt_thread_free_stack( stackno );
131 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
133 void *(*start_routine)( void *),
139 if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
142 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
143 stack, 3, start_routine, arg, stackno ) );
147 ldap_pvt_thread_exit( void *retval )
153 ldap_pvt_thread_sleep(
154 unsigned int interval
162 if ( lwp_self( &mylwp ) < 0 ) {
168 mon_enter( &sglob->tsl_mon );
170 if ( sglob->tsl_list != NULL ) {
171 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
172 if ( SAMETHREAD( t->tl_tid, mylwp )) {
173 /* We're already sleeping? */
174 t->tl_wake = now + interval;
175 mon_exit( &sglob->tsl_mon );
176 lwp_suspend( mylwp );
182 nt = (tl_t *) malloc( sizeof( tl_t ));
184 nt->tl_next = sglob->tsl_list;
185 nt->tl_wake = now + interval;
187 sglob->tsl_list = nt;
189 mon_exit( &sglob->tsl_mon );
191 lwp_suspend( mylwp );
196 * The lwp_scheduler thread periodically checks to see if any threads
197 * are due to be resumed. If there are, it resumes them. Otherwise,
198 * it computes the lesser of ( 1 second ) or ( the minimum time until
199 * a thread need to be resumed ) and puts itself to sleep for that amount
208 struct timeval interval;
211 while ( !sglob->slurpd_shutdown ) {
212 mon_enter( &sglob->tsl_mon );
216 if ( sglob->tsl_list != NULL ) {
217 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
218 if (( t->tl_wake > 0L ) && ( t->tl_wake < now )) {
219 lwp_resume( t->tl_tid );
223 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
229 mon_exit( &sglob->tsl_mon );
231 interval.tv_usec = 0L;
233 interval.tv_sec = 1L;
235 interval.tv_sec = min;
238 lwp_sleep( &interval );
241 mon_enter( &sglob->tsl_mon );
243 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
244 lwp_resume( t->tl_tid );
247 mon_exit( &sglob->tsl_mon );
249 free_stack( stackno );
253 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
260 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
266 ldap_pvt_thread_yield( void )
273 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
276 * lwp cv_create requires the monitor id be passed in
277 * when the cv is created, pthreads passes it when the
278 * condition is waited for. so, we fake the creation
279 * here and actually do it when the cv is waited for
283 cond->lcv_created = 0;
289 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
291 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
295 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
296 ldap_pvt_thread_mutex_t *mutex )
298 if ( ! cond->lcv_created ) {
299 cv_create( &cond->lcv_cv, *mutex );
300 cond->lcv_created = 1;
303 return( cv_wait( cond->lcv_cv ) );
307 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
309 return( mon_create( mutex ) );
313 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
315 return( mon_destroy( *mutex ) );
319 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
321 return( mon_enter( *mutex ) );
325 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
327 return( mon_exit( *mutex ) );
331 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
333 return( mon_cond_enter( *mp ) );
337 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
339 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
343 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
345 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
348 #endif /* HAVE_LWP */