1 /* thr_lwp.c - wrappers around SunOS LWP threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
19 * they can become static.
24 #if defined( HAVE_LWP )
32 /* This implementation NEEDS WORK. It currently does not compile */
37 #include <ac/socket.h>
41 #include "ldap_pvt_thread.h"
44 #include <lwp/stackdep.h>
46 #define MAX_STACK 51200
47 #define MAX_THREADS 20
50 * Initialize LWP by spinning of a schedular
53 ldap_int_thread_initialize( void )
59 if (( stack = get_stack( &stackno )) == NULL ) {
63 lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
68 ldap_int_thread_destroy( void )
70 /* need to destory lwp_scheduler thread and clean up private
77 stkalign_t *stk_stack;
80 static struct stackinfo *stacks;
82 static stkalign_t * ldap_int_thread_get_stack( int *stacknop )
86 if ( stacks == NULL ) {
87 stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
88 sizeof(struct stackinfo) );
90 if( stacks == NULL ) {
91 Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
97 for ( i = 0; i < MAX_THREADS; i++ ) {
98 if ( stacks[i].stk_inuse == 0 ) {
103 if ( i == MAX_THREADS ) {
104 Debug( LDAP_DEBUG_ANY,
105 "no more stacks (max %d) - increase MAX_THREADS for more",
110 if ( stacks[i].stk_stack == NULL ) {
111 stacks[i].stk_stack = (stkalign_t *) LDAP_MALLOC(
112 (MAX_STACK / sizeof(stkalign_t) + 1 )
113 * sizeof(stkalign_t) );
115 if( stacks[i].stk_stack == NULL ) {
116 Debug( LDAP_DEBUG_ANY, "stack allocation failed",
123 stacks[i].stk_inuse = 1;
124 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
128 ldap_int_thread_free_stack( int stackno )
130 if ( stackno < 0 || stackno > MAX_THREADS ) {
131 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
135 stacks[stackno].stk_inuse = 0;
139 lwp_create_stack( void *(*func)(), void *arg, int stackno )
143 ldap_int_thread_free_stack( stackno );
147 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
149 void *(*start_routine)( void *),
155 if ( (stack = ldap_int_thread_get_stack( &stackno )) == NULL ) {
158 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
159 stack, 3, start_routine, arg, stackno ) );
163 ldap_pvt_thread_exit( void *retval )
169 ldap_pvt_thread_sleep(
170 unsigned int interval
178 if ( lwp_self( &mylwp ) < 0 ) {
184 mon_enter( &sglob->tsl_mon );
186 if ( sglob->tsl_list != NULL ) {
187 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
188 if ( SAMETHREAD( t->tl_tid, mylwp )) {
189 /* We're already sleeping? */
190 t->tl_wake = now + interval;
191 mon_exit( &sglob->tsl_mon );
192 lwp_suspend( mylwp );
198 nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
200 if( nt == NULL ) return -1;
202 nt->tl_next = sglob->tsl_list;
203 nt->tl_wake = now + interval;
205 sglob->tsl_list = nt;
207 mon_exit( &sglob->tsl_mon );
209 lwp_suspend( mylwp );
214 * The lwp_scheduler thread periodically checks to see if any threads
215 * are due to be resumed. If there are, it resumes them. Otherwise,
216 * it computes the lesser of ( 1 second ) or ( the minimum time until
217 * a thread need to be resumed ) and puts itself to sleep for that amount
226 struct timeval interval;
229 while ( !sglob->slurpd_shutdown ) {
230 mon_enter( &sglob->tsl_mon );
234 if ( sglob->tsl_list != NULL ) {
235 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
236 if (( t->tl_wake > 0L ) && ( t->tl_wake < now )) {
237 lwp_resume( t->tl_tid );
241 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
247 mon_exit( &sglob->tsl_mon );
249 interval.tv_usec = 0L;
251 interval.tv_sec = 1L;
253 interval.tv_sec = min;
256 lwp_sleep( &interval );
259 mon_enter( &sglob->tsl_mon );
261 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
262 lwp_resume( t->tl_tid );
265 mon_exit( &sglob->tsl_mon );
267 free_stack( stackno );
271 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
278 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
284 ldap_pvt_thread_yield( void )
291 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
294 * lwp cv_create requires the monitor id be passed in
295 * when the cv is created, pthreads passes it when the
296 * condition is waited for. so, we fake the creation
297 * here and actually do it when the cv is waited for
301 cond->lcv_created = 0;
307 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
309 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
313 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
314 ldap_int_thread_mutex_t *mutex )
316 if ( ! cond->lcv_created ) {
317 cv_create( &cond->lcv_cv, *mutex );
318 cond->lcv_created = 1;
321 return( cv_wait( cond->lcv_cv ) );
325 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
327 return( mon_create( mutex ) );
331 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
333 return( mon_destroy( *mutex ) );
337 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
339 return( mon_enter( *mutex ) );
343 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
345 return( mon_exit( *mutex ) );
349 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
351 return( mon_cond_enter( *mp ) );
355 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
357 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
361 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
363 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
367 ldap_pvt_thread_self( void )
376 #endif /* HAVE_LWP */