]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_lwp.c
Sync with HEAD
[openldap] / libraries / libldap_r / thr_lwp.c
1 /* thr_lwp.c - wrappers around SunOS LWP threads */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16
17 /* BUGS:
18  * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
19  *   they can become static.
20  */
21
22 #include "portable.h"
23
24 #if defined( HAVE_LWP )
25
26 /*************
27  *           *
28  * SunOS LWP *
29  *           *
30  *************/
31
32 /* This implementation NEEDS WORK.   It currently does not compile */
33
34 #include <stdio.h>
35
36 #include <ac/time.h>
37 #include <ac/socket.h>
38
39 #include "ldap-int.h"
40
41 #include "ldap_pvt_thread.h"
42
43 #include <lwp/lwp.h>
44 #include <lwp/stackdep.h>
45
46 #define MAX_STACK       51200
47 #define MAX_THREADS     20
48
49 /*
50  * Initialize LWP by spinning of a schedular
51  */
52 int
53 ldap_int_thread_initialize( void )
54 {
55         thread_t                tid;
56         stkalign_t              *stack;
57         int                     stackno;
58
59         if (( stack = get_stack( &stackno )) == NULL ) {
60                 return -1;
61         }
62
63         lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
64         return 0;
65 }
66
67 int
68 ldap_int_thread_destroy( void )
69 {
70         /* need to destory lwp_scheduler thread and clean up private
71                 variables */
72         return 0;
73 }
74
75 struct stackinfo {
76         int             stk_inuse;
77         stkalign_t      *stk_stack;
78 };
79
80 static struct stackinfo *stacks;
81
82 static stkalign_t * ldap_int_thread_get_stack( int *stacknop )
83 {
84         int     i;
85
86         if ( stacks == NULL ) {
87                 stacks = (struct stackinfo *) LDAP_CALLOC( 1, MAX_THREADS *
88                     sizeof(struct stackinfo) );
89
90                 if( stacks == NULL ) {
91                         Debug( LDAP_DEBUG_ANY, "stacks allocation failed",
92                                 0, 0, 0 );
93                         return NULL;
94                 }
95         }
96
97         for ( i = 0; i < MAX_THREADS; i++ ) {
98                 if ( stacks[i].stk_inuse == 0 ) {
99                         break;
100                 }
101         }
102
103         if ( i == MAX_THREADS ) {
104                 Debug( LDAP_DEBUG_ANY,
105                     "no more stacks (max %d) - increase MAX_THREADS for more",
106                     MAX_THREADS, 0, 0 );
107                 return( NULL );
108         }
109
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) );
114
115                 if( stacks[i].stk_stack == NULL ) {
116                         Debug( LDAP_DEBUG_ANY, "stack allocation failed",
117                                 0, 0, 0 );
118                         return( NULL );
119                 }
120         }
121
122         *stacknop = i;
123         stacks[i].stk_inuse = 1;
124         return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
125 }
126
127 static void
128 ldap_int_thread_free_stack( int stackno )
129 {
130         if ( stackno < 0 || stackno > MAX_THREADS ) {
131                 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
132                     stackno, 0, 0 );
133         }
134
135         stacks[stackno].stk_inuse = 0;
136 }
137
138 static void
139 lwp_create_stack( void *(*func)(), void *arg, int stackno )
140 {
141         (*func)( arg );
142
143         ldap_int_thread_free_stack( stackno );
144 }
145
146 int 
147 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
148         int detach,
149         void *(*start_routine)( void *),
150         void *arg)
151 {
152         stkalign_t      *stack;
153         int             stackno;
154
155         if ( (stack = ldap_int_thread_get_stack( &stackno )) == NULL ) {
156                 return( -1 );
157         }
158         return( lwp_create( thread, lwp_create_stack, MINPRIO, 0, 
159                            stack, 3, start_routine, arg, stackno ) );
160 }
161
162 void 
163 ldap_pvt_thread_exit( void *retval )
164 {
165         lwp_destroy( SELF );
166 }
167
168 unsigned int
169 ldap_pvt_thread_sleep(
170         unsigned int interval
171 )
172 {
173         thread_t                mylwp;
174         tl_t            *t, *nt;
175         time_t          now;
176
177
178         if ( lwp_self( &mylwp ) < 0 ) {
179                 return -1;
180         }
181
182         time( &now );
183
184         mon_enter( &sglob->tsl_mon );
185
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 );
193                                 return 0;
194                         }
195                 }
196         }
197
198         nt = (tl_t *) LDAP_MALLOC( sizeof( tl_t ));
199
200         if( nt == NULL ) return -1;
201
202         nt->tl_next = sglob->tsl_list;
203         nt->tl_wake = now + interval;
204         nt->tl_tid = mylwp;
205         sglob->tsl_list = nt;
206
207         mon_exit( &sglob->tsl_mon );
208
209         lwp_suspend( mylwp );
210         return 0;
211 }
212
213 /*
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
218  * of time.
219  */
220 static void
221 lwp_scheduler(
222         int             stackno
223 )
224 {
225         time_t                  now, min;
226         struct timeval          interval;
227         tl_t                    *t;
228
229         while ( !sglob->slurpd_shutdown ) {
230                 mon_enter( &sglob->tsl_mon );
231
232                 time( &now );
233                 min = 0L;
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 );
238                                         t->tl_wake = 0L;
239                                 }
240
241                                 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
242                                         min =  t->tl_wake;
243                                 }
244                         }
245                 }
246
247                 mon_exit( &sglob->tsl_mon );
248
249                 interval.tv_usec = 0L;
250                 if ( min == 0L ) {
251                         interval.tv_sec = 1L;
252                 } else {
253                         interval.tv_sec = min;
254                 }
255
256                 lwp_sleep( &interval );
257         }
258
259         mon_enter( &sglob->tsl_mon );
260
261         for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
262                 lwp_resume( t->tl_tid );
263         }
264
265         mon_exit( &sglob->tsl_mon );
266
267         free_stack( stackno );
268 }
269
270 int 
271 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
272 {
273         lwp_join( thread );
274         return 0;
275 }
276
277 int 
278 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
279 {
280         return 0;
281 }
282
283 int 
284 ldap_pvt_thread_yield( void )
285 {
286         lwp_yield( SELF );
287         return 0;
288 }
289
290 int 
291 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
292 {
293         /*
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
298          * later.
299          */
300
301         cond->lcv_created = 0;
302
303         return( 0 );
304 }
305
306 int 
307 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
308 {
309         return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
310 }
311
312 int 
313 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
314         ldap_int_thread_mutex_t *mutex )
315 {
316         if ( ! cond->lcv_created ) {
317                 cv_create( &cond->lcv_cv, *mutex );
318                 cond->lcv_created = 1;
319         }
320
321         return( cv_wait( cond->lcv_cv ) );      
322 }
323
324 int 
325 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
326 {
327         return( mon_create( mutex ) );
328 }
329
330 int 
331 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
332 {
333         return( mon_destroy( *mutex ) );
334 }
335
336 int 
337 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
338 {
339         return( mon_enter( *mutex ) );
340 }
341
342 int 
343 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
344 {
345         return( mon_exit( *mutex ) );
346 }
347
348 int
349 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
350 {
351         return( mon_cond_enter( *mp ) );
352 }
353
354 int
355 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
356 {
357         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
358 }
359
360 int
361 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
362 {
363         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
364 }
365
366 ldap_pvt_thread_t
367 ldap_pvt_thread_self( void )
368 {
369         thread_t                mylwp;
370
371         lwp_self( &mylwp );
372
373         return mylwp;
374 }
375
376 #endif /* HAVE_LWP */