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
45 stkalign_t *stk_stack;
48 static struct stackinfo *stacks;
50 stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
54 if ( stacks == NULL ) {
55 stacks = (struct stackinfo *) ch_calloc( 1, MAX_THREADS *
56 sizeof(struct stackinfo) );
59 for ( i = 0; i < MAX_THREADS; i++ ) {
60 if ( stacks[i].stk_inuse == 0 ) {
65 if ( i == MAX_THREADS ) {
66 Debug( LDAP_DEBUG_ANY,
67 "no more stacks (max %d) - increase MAX_THREADS for more",
72 if ( stacks[i].stk_stack == NULL ) {
73 stacks[i].stk_stack = (stkalign_t *) malloc(
74 (MAX_STACK / sizeof(stkalign_t) + 1 )
75 * sizeof(stkalign_t) );
79 stacks[i].stk_inuse = 1;
80 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
84 ldap_pvt_thread_free_stack( int stackno )
86 if ( stackno < 0 || stackno > MAX_THREADS ) {
87 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d",
91 stacks[stackno].stk_inuse = 0;
95 lwp_create_stack( void *(*func)(), void *arg, int stackno )
99 ldap_pvt_thread_free_stack( stackno );
103 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
104 ldap_pvt_thread_attr_t *attr,
105 void *(*start_routine)( void *), void *arg)
110 if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
113 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
114 stack, 3, start_routine, arg, stackno ) );
118 ldap_pvt_thread_exit( void *retval )
124 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
131 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
137 ldap_pvt_thread_yield( void )
144 ldap_pvt_thread_attr_init( ldap_pvt_thread_attr_t *attr )
151 ldap_pvt_thread_attr_destroy( ldap_pvt_thread_attr_t *attr )
157 ldap_pvt_thread_attr_setdetachstate( ldap_pvt_thread_attr_t *attr, int dstate )
164 ldap_pvt_thread_attr_getdetachstate( pthread_attr_t *attr, int *detachstate )
166 *detachstate = *attr;
171 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond,
172 ldap_pvt_thread_condattr_t *attr )
175 * lwp cv_create requires the monitor id be passed in
176 * when the cv is created, pthreads passes it when the
177 * condition is waited for. so, we fake the creation
178 * here and actually do it when the cv is waited for
182 cond->lcv_created = 0;
188 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
190 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
194 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond,
195 ldap_pvt_thread_mutex_t *mutex )
197 if ( ! cond->lcv_created ) {
198 cv_create( &cond->lcv_cv, *mutex );
199 cond->lcv_created = 1;
202 return( cv_wait( cond->lcv_cv ) );
206 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex,
207 ldap_pvt_thread_mutexattr_t *attr )
209 return( mon_create( mutex ) );
213 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
215 return( mon_destroy( *mutex ) );
219 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
221 return( mon_enter( *mutex ) );
225 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
227 return( mon_exit( *mutex ) );
231 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
233 return( mon_cond_enter( *mp ) );
237 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
239 return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
243 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
245 return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
248 #endif /* HAVE_LWP */