1 /* thr_lwp.c - wrappers around SunOS LWP threads */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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" /* Get the thread interface */
42 #define LDAP_THREAD_IMPLEMENTATION
43 #include "ldap_thr_debug.h" /* May rename the symbols defined below */
46 #include <lwp/stackdep.h>
48 #define MAX_STACK 51200
49 #define MAX_THREADS 20
52 * Initialize LWP by spinning of a schedular
55 ldap_int_thread_initialize( void )
61 if (( stack = get_stack( &stackno )) == NULL ) {
65 lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
70 ldap_int_thread_destroy( void )
72 /* need to destroy lwp_scheduler thread and clean up private
79 stkalign_t *stk_stack;
82 static struct stackinfo *stacks;
84 static stkalign_t * ldap_int_thread_get_stack( int *stacknop )
88 if ( stacks == NULL ) {
89 stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
90 sizeof(struct stackinfo) );
92 if( stacks == NULL ) {
93 Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
99 for ( i = 0; i < MAX_THREADS; i++ ) {
100 if ( stacks[i].stk_inuse == 0 ) {
105 if ( i == MAX_THREADS ) {
106 Debug( LDAP_DEBUG_ANY,
107 "no more stacks (max %d) - increase MAX_THREADS for more",
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) );
117 if( stacks[i].stk_stack == NULL ) {
118 Debug( LDAP_DEBUG_ANY, "stack allocation failed",
125 stacks[i].stk_inuse = 1;
126 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
130 ldap_int_thread_free_stack( int stackno )
132 if ( stackno < 0 || stackno > MAX_THREADS ) {
133 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
137 stacks[stackno].stk_inuse = 0;
141 lwp_create_stack( void *(*func)(), void *arg, int stackno )
145 ldap_int_thread_free_stack( stackno );
149 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
151 void *(*start_routine)( void *),
157 if ( (stack = ldap_int_thread_get_stack( &stackno )) == NULL ) {
160 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
161 stack, 3, start_routine, arg, stackno ) );
165 ldap_pvt_thread_exit( void *retval )
171 ldap_pvt_thread_sleep(
172 unsigned int interval
180 if ( lwp_self( &mylwp ) < 0 ) {
186 mon_enter( &sglob->tsl_mon );
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 );
200 nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
202 if( nt == NULL ) return -1;
204 nt->tl_next = sglob->tsl_list;
205 nt->tl_wake = now + interval;
207 sglob->tsl_list = nt;
209 mon_exit( &sglob->tsl_mon );
211 lwp_suspend( mylwp );
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
228 struct timeval interval;
231 while ( !sglob->slurpd_shutdown ) {
232 mon_enter( &sglob->tsl_mon );
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 );
243 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
249 mon_exit( &sglob->tsl_mon );
251 interval.tv_usec = 0L;
253 interval.tv_sec = 1L;
255 interval.tv_sec = min;
258 lwp_sleep( &interval );
261 mon_enter( &sglob->tsl_mon );
263 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
264 lwp_resume( t->tl_tid );
267 mon_exit( &sglob->tsl_mon );
269 free_stack( stackno );
273 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
280 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
286 ldap_pvt_thread_yield( void )
293 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
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
303 cond->lcv_created = 0;
309 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
311 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
315 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
316 ldap_pvt_thread_mutex_t *mutex )
318 if ( ! cond->lcv_created ) {
319 cv_create( &cond->lcv_cv, *mutex );
320 cond->lcv_created = 1;
323 return( cv_wait( cond->lcv_cv ) );
327 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
329 return( mon_create( mutex ) );
333 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
335 return( mon_destroy( *mutex ) );
339 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
341 return( mon_enter( *mutex ) );
345 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
347 return( mon_exit( *mutex ) );
351 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
353 return( mon_cond_enter( *mp ) );
357 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
359 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
363 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
365 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
369 ldap_pvt_thread_self( void )
378 #endif /* HAVE_LWP */