No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap_r / thr_lwp.c
blob183939faad660913c5865119e462e979ee36093f
1 /* thr_lwp.c - wrappers around SunOS LWP threads */
2 /* $OpenLDAP: pkg/ldap/libraries/libldap_r/thr_lwp.c,v 1.20.2.3 2008/02/11 23:26:42 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
17 /* BUGS:
18 * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
19 * they can become static.
22 #include "portable.h"
24 #if defined( HAVE_LWP )
26 /*************
27 * *
28 * SunOS LWP *
29 * *
30 *************/
32 /* This implementation NEEDS WORK. It currently does not compile */
34 #include <stdio.h>
36 #include <ac/time.h>
37 #include <ac/socket.h>
39 #include "ldap-int.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 */
45 #include <lwp/lwp.h>
46 #include <lwp/stackdep.h>
48 #define MAX_STACK 51200
49 #define MAX_THREADS 20
52 * Initialize LWP by spinning of a schedular
54 int
55 ldap_int_thread_initialize( void )
57 thread_t tid;
58 stkalign_t *stack;
59 int stackno;
61 if (( stack = get_stack( &stackno )) == NULL ) {
62 return -1;
65 lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
66 return 0;
69 int
70 ldap_int_thread_destroy( void )
72 /* need to destroy lwp_scheduler thread and clean up private
73 variables */
74 return 0;
77 struct stackinfo {
78 int stk_inuse;
79 stkalign_t *stk_stack;
82 static struct stackinfo *stacks;
84 static stkalign_t * ldap_int_thread_get_stack( int *stacknop )
86 int i;
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",
94 0, 0, 0 );
95 return NULL;
99 for ( i = 0; i < MAX_THREADS; i++ ) {
100 if ( stacks[i].stk_inuse == 0 ) {
101 break;
105 if ( i == MAX_THREADS ) {
106 Debug( LDAP_DEBUG_ANY,
107 "no more stacks (max %d) - increase MAX_THREADS for more",
108 MAX_THREADS, 0, 0 );
109 return( NULL );
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",
119 0, 0, 0 );
120 return( NULL );
124 *stacknop = i;
125 stacks[i].stk_inuse = 1;
126 return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
129 static void
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",
134 stackno, 0, 0 );
137 stacks[stackno].stk_inuse = 0;
140 static void
141 lwp_create_stack( void *(*func)(), void *arg, int stackno )
143 (*func)( arg );
145 ldap_int_thread_free_stack( stackno );
148 int
149 ldap_pvt_thread_create( ldap_pvt_thread_t * thread,
150 int detach,
151 void *(*start_routine)( void *),
152 void *arg)
154 stkalign_t *stack;
155 int stackno;
157 if ( (stack = ldap_int_thread_get_stack( &stackno )) == NULL ) {
158 return( -1 );
160 return( lwp_create( thread, lwp_create_stack, MINPRIO, 0,
161 stack, 3, start_routine, arg, stackno ) );
164 void
165 ldap_pvt_thread_exit( void *retval )
167 lwp_destroy( SELF );
170 unsigned int
171 ldap_pvt_thread_sleep(
172 unsigned int interval
175 thread_t mylwp;
176 tl_t *t, *nt;
177 time_t now;
180 if ( lwp_self( &mylwp ) < 0 ) {
181 return -1;
184 time( &now );
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 );
195 return 0;
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;
206 nt->tl_tid = mylwp;
207 sglob->tsl_list = nt;
209 mon_exit( &sglob->tsl_mon );
211 lwp_suspend( mylwp );
212 return 0;
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
220 * of time.
222 static void
223 lwp_scheduler(
224 int stackno
227 time_t now, min;
228 struct timeval interval;
229 tl_t *t;
231 while ( !sglob->slurpd_shutdown ) {
232 mon_enter( &sglob->tsl_mon );
234 time( &now );
235 min = 0L;
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 );
240 t->tl_wake = 0L;
243 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
244 min = t->tl_wake;
249 mon_exit( &sglob->tsl_mon );
251 interval.tv_usec = 0L;
252 if ( min == 0L ) {
253 interval.tv_sec = 1L;
254 } else {
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 );
272 int
273 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
275 lwp_join( thread );
276 return 0;
279 int
280 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
282 return 0;
285 int
286 ldap_pvt_thread_yield( void )
288 lwp_yield( SELF );
289 return 0;
292 int
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
300 * later.
303 cond->lcv_created = 0;
305 return( 0 );
308 int
309 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
311 return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
314 int
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 ) );
326 int
327 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
329 return( mon_create( mutex ) );
332 int
333 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
335 return( mon_destroy( *mutex ) );
338 int
339 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
341 return( mon_enter( *mutex ) );
344 int
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 );
368 ldap_pvt_thread_t
369 ldap_pvt_thread_self( void )
371 thread_t mylwp;
373 lwp_self( &mylwp );
375 return mylwp;
378 #endif /* HAVE_LWP */