]> git.sur5r.net Git - openldap/blob - libraries/libldap_r/thr_lwp.c
Provide a little information about SDF, how to use it, and where to get it.h
[openldap] / libraries / libldap_r / thr_lwp.c
1 /*
2  * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
3  * All rights reserved.
4  *
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.
9  */
10
11 /* thr_lwp.c - wrappers around SunOS LWP threads */
12
13 /* BUGS:
14  * - slurpd calls the get_stack/free_stack functions. Should be fixed, so
15  *   they can become static.
16  */
17
18 #include "portable.h"
19 #include "ldap_pvt_thread.h"
20
21 #if defined( HAVE_LWP )
22
23 /*************
24  *           *
25  * SunOS LWP *
26  *           *
27  *************/
28
29 /* This implementation NEEDS WORK.   It currently does not compile */
30
31 #include <stdio.h>
32
33 #include <ac/time.h>
34 #include <ac/socket.h>
35
36 #include "lber.h"
37 #include "ldap.h"
38 #include "ldap_log.h"
39
40 #include <lwp/lwp.h>
41 #include <lwp/stackdep.h>
42
43 #define MAX_STACK       51200
44 #define MAX_THREADS     20
45
46 /*
47  * Initialize LWP by spinning of a schedular
48  */
49 int
50 ldap_pvt_thread_initialize( void )
51 {
52         thread_t                tid;
53         stkalign_t              *stack;
54         int                     stackno;
55
56         if (( stack = get_stack( &stackno )) == NULL ) {
57                 return -1;
58         }
59
60         lwp_create( &tid, lwp_scheduler, MINPRIO, 0, stack, 1, stackno );
61         return 0;
62 }
63
64 int
65 ldap_pvt_thread_destroy( void )
66 {
67         /* need to destory lwp_scheduler thread and clean up private
68                 variables */
69         return 0;
70 }
71
72 struct stackinfo {
73         int             stk_inuse;
74         stkalign_t      *stk_stack;
75 };
76
77 static struct stackinfo *stacks;
78
79 static stkalign_t * ldap_pvt_thread_get_stack( int *stacknop )
80 {
81         int     i;
82
83         if ( stacks == NULL ) {
84                 stacks = (struct stackinfo *) ch_calloc( 1, MAX_THREADS *
85                     sizeof(struct stackinfo) );
86         }
87
88         for ( i = 0; i < MAX_THREADS; i++ ) {
89                 if ( stacks[i].stk_inuse == 0 ) {
90                         break;
91                 }
92         }
93
94         if ( i == MAX_THREADS ) {
95                 Debug( LDAP_DEBUG_ANY,
96                     "no more stacks (max %d) - increase MAX_THREADS for more",
97                     MAX_THREADS, 0, 0 );
98                 return( NULL );
99         }
100
101         if ( stacks[i].stk_stack == NULL ) {
102                 stacks[i].stk_stack = (stkalign_t *) malloc(
103                     (MAX_STACK / sizeof(stkalign_t) + 1 )
104                     * sizeof(stkalign_t) );
105         }
106
107         *stacknop = i;
108         stacks[i].stk_inuse = 1;
109         return( stacks[i].stk_stack + MAX_STACK / sizeof(stkalign_t) );
110 }
111
112 static void
113 ldap_pvt_thread_free_stack( int stackno )
114 {
115         if ( stackno < 0 || stackno > MAX_THREADS ) {
116                 Debug( LDAP_DEBUG_ANY, "free_stack of bogus stack %d\n",
117                     stackno, 0, 0 );
118         }
119
120         stacks[stackno].stk_inuse = 0;
121 }
122
123 static static void
124 lwp_create_stack( void *(*func)(), void *arg, int stackno )
125 {
126         (*func)( arg );
127
128         ldap_pvt_thread_free_stack( stackno );
129 }
130
131 int 
132 ldap_pvt_thread_create( ldap_pvt_thread_t * thread, 
133         int detach,
134         void *(*start_routine)( void *),
135         void *arg)
136 {
137         stkalign_t      *stack;
138         int             stackno;
139
140         if ( (stack = ldap_pvt_thread_get_stack( &stackno )) == NULL ) {
141                 return( -1 );
142         }
143         return( lwp_create( thread, lwp_create_stack, MINPRIO, 0, 
144                            stack, 3, start_routine, arg, stackno ) );
145 }
146
147 void 
148 ldap_pvt_thread_exit( void *retval )
149 {
150         lwp_destroy( SELF );
151 }
152
153 unsigned int
154 ldap_pvt_thread_sleep(
155         unsigned int interval
156 )
157 {
158         thread_t                mylwp;
159         tl_t            *t, *nt;
160         time_t          now;
161
162
163         if ( lwp_self( &mylwp ) < 0 ) {
164                 return -1;
165         }
166
167         time( &now );
168
169         mon_enter( &sglob->tsl_mon );
170
171         if ( sglob->tsl_list != NULL ) {
172                 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
173                         if ( SAMETHREAD( t->tl_tid, mylwp )) {
174                                 /* We're already sleeping? */
175                                 t->tl_wake = now + interval;
176                                 mon_exit( &sglob->tsl_mon );
177                                 lwp_suspend( mylwp );
178                                 return 0;
179                         }
180                 }
181         }
182
183         nt = (tl_t *) malloc( sizeof( tl_t ));
184
185         nt->tl_next = sglob->tsl_list;
186         nt->tl_wake = now + interval;
187         nt->tl_tid = mylwp;
188         sglob->tsl_list = nt;
189
190         mon_exit( &sglob->tsl_mon );
191
192         lwp_suspend( mylwp );
193         return 0;
194 }
195
196 /*
197  * The lwp_scheduler thread periodically checks to see if any threads
198  * are due to be resumed.  If there are, it resumes them.  Otherwise,
199  * it computes the lesser of ( 1 second ) or ( the minimum time until
200  * a thread need to be resumed ) and puts itself to sleep for that amount
201  * of time.
202  */
203 static void
204 lwp_scheduler(
205         int             stackno
206 )
207 {
208         time_t                  now, min;
209         struct timeval          interval;
210         tl_t                    *t;
211
212         while ( !sglob->slurpd_shutdown ) {
213                 mon_enter( &sglob->tsl_mon );
214
215                 time( &now );
216                 min = 0L;
217                 if ( sglob->tsl_list != NULL ) {
218                         for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
219                                 if (( t->tl_wake  > 0L ) && ( t->tl_wake < now )) {
220                                         lwp_resume( t->tl_tid );
221                                         t->tl_wake = 0L;
222                                 }
223
224                                 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
225                                         min =  t->tl_wake;
226                                 }
227                         }
228                 }
229
230                 mon_exit( &sglob->tsl_mon );
231
232                 interval.tv_usec = 0L;
233                 if ( min == 0L ) {
234                         interval.tv_sec = 1L;
235                 } else {
236                         interval.tv_sec = min;
237                 }
238
239                 lwp_sleep( &interval );
240         }
241
242         mon_enter( &sglob->tsl_mon );
243
244         for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
245                 lwp_resume( t->tl_tid );
246         }
247
248         mon_exit( &sglob->tsl_mon );
249
250         free_stack( stackno );
251 }
252
253 int 
254 ldap_pvt_thread_join( ldap_pvt_thread_t thread, void **thread_return )
255 {
256         lwp_join( thread );
257         return 0;
258 }
259
260 int 
261 ldap_pvt_thread_kill( ldap_pvt_thread_t thread, int signo )
262 {
263         return 0;
264 }
265
266 int 
267 ldap_pvt_thread_yield( void )
268 {
269         lwp_yield( SELF );
270         return 0;
271 }
272
273 int 
274 ldap_pvt_thread_cond_init( ldap_pvt_thread_cond_t *cond )
275 {
276         /*
277          * lwp cv_create requires the monitor id be passed in
278          * when the cv is created, pthreads passes it when the
279          * condition is waited for.  so, we fake the creation
280          * here and actually do it when the cv is waited for
281          * later.
282          */
283
284         cond->lcv_created = 0;
285
286         return( 0 );
287 }
288
289 int 
290 ldap_pvt_thread_cond_signal( ldap_pvt_thread_cond_t *cond )
291 {
292         return( cond->lcv_created ? cv_notify( cv->lcv_cv ) : 0 );
293 }
294
295 int 
296 ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
297                       ldap_pvt_thread_mutex_t *mutex )
298 {
299         if ( ! cond->lcv_created ) {
300                 cv_create( &cond->lcv_cv, *mutex );
301                 cond->lcv_created = 1;
302         }
303
304         return( cv_wait( cond->lcv_cv ) );      
305 }
306
307 int 
308 ldap_pvt_thread_mutex_init( ldap_pvt_thread_mutex_t *mutex )
309 {
310         return( mon_create( mutex ) );
311 }
312
313 int 
314 ldap_pvt_thread_mutex_destroy( ldap_pvt_thread_mutex_t *mutex )
315 {
316         return( mon_destroy( *mutex ) );
317 }
318
319 int 
320 ldap_pvt_thread_mutex_lock( ldap_pvt_thread_mutex_t *mutex )
321 {
322         return( mon_enter( *mutex ) );
323 }
324
325 int 
326 ldap_pvt_thread_mutex_unlock( ldap_pvt_thread_mutex_t *mutex )
327 {
328         return( mon_exit( *mutex ) );
329 }
330
331 int
332 ldap_pvt_thread_mutex_trylock( ldap_pvt_thread_mutex_t *mp )
333 {
334         return( mon_cond_enter( *mp ) );
335 }
336
337 int
338 ldap_pvt_thread_cond_destroy( ldap_pvt_thread_cond_t *cv )
339 {
340         return( cv->lcv_created ? cv_destroy( cv->lcv_cv ) : 0 );
341 }
342
343 int
344 ldap_pvt_thread_cond_broadcast( ldap_pvt_thread_cond_t *cv )
345 {
346         return( cv->lcv_created ? cv_broadcast( cv->lcv_cv ) : 0 );
347 }
348
349 #endif /* HAVE_LWP */