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