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.
19 #include "ldap_pvt_thread.h"
21 #if defined( HAVE_LWP )
31 #include <ac/socket.h>
38 #include <lwp/stackdep.h>
40 #define MAX_STACK 51200
41 #define MAX_THREADS 20
44 * Initialize LWP by spinning of a schedular
47 ldap_pvt_thread_initialize( void )
53 if (( stack = get_stack( &stackno )) == NULL ) {
57 lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
63 stkalign_t *stk_stack;
66 static struct stackinfo *stacks;
68 stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
72 if ( stacks == NULL ) {
73 stacks = (struct stackinfo *) ch_calloc( 1, MAX_THREADS *
74 sizeof(struct stackinfo) );
77 for ( i = 0; i < MAX_THREADS; i++ ) {
78 if ( stacks[i].stk_inuse == 0 ) {
83 if ( i == MAX_THREADS ) {
84 Debug( LDAP_DEBUG_ANY,
85 "no more stacks (max %d) - increase MAX_THREADS for more",
90 if ( stacks[i].stk_stack == NULL ) {
91 stacks[i].stk_stack = (stkalign_t *) malloc(
92 (MAX_STACK / sizeof(stkalign_t) + 1 )
93 * sizeof(stkalign_t) );
97 stacks[i].stk_inuse = 1;
98 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
102 ldap_pvt_thread_free_stack( int stackno )
104 if ( stackno < 0 || stackno > MAX_THREADS ) {
105 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d",
109 stacks[stackno].stk_inuse = 0;
113 lwp_create_stack( void *(*func)(), void *arg, int stackno )
117 ldap_pvt_thread_free_stack( stackno );
121 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
123 void *(*start_routine)( void *),
129 if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
132 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
133 stack, 3, start_routine, arg, stackno ) );
137 ldap_pvt_thread_exit( void *retval )
143 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
150 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
156 ldap_pvt_thread_yield( void )
163 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
166 * lwp cv_create requires the monitor id be passed in
167 * when the cv is created, pthreads passes it when the
168 * condition is waited for. so, we fake the creation
169 * here and actually do it when the cv is waited for
173 cond->lcv_created = 0;
179 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
181 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
185 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
186 ldap_pvt_thread_mutex_t *mutex )
188 if ( ! cond->lcv_created ) {
189 cv_create( &cond->lcv_cv, *mutex );
190 cond->lcv_created = 1;
193 return( cv_wait( cond->lcv_cv ) );
197 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
199 return( mon_create( mutex ) );
203 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
205 return( mon_destroy( *mutex ) );
209 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
211 return( mon_enter( *mutex ) );
215 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
217 return( mon_exit( *mutex ) );
221 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
223 return( mon_cond_enter( *mp ) );
227 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
229 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
233 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
235 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
238 #endif /* HAVE_LWP */