2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * ldap_pvt_thread_sleep.c - allow a thread to sleep without putting
15 * the whole process (e.g. pod under lwp) to sleep.
17 * Contains platform-specific code to allow this:
19 * Under non-preemptive threads packages like SunOS lwp, tsleep() adds
20 * the thread to a list of sleepers. The lwp_scheduler process takes
21 * care of resuming suspended threads.
23 * Under a fully-preemptive threads package, like Solaris threads,
24 * tsleep just calls sleep(), and there is no scheduler thread. Life
25 * is so much simpler...
32 #include <ac/unistd.h> /* get sleep() */
34 #if !defined( HAVE_LWP )
37 * Here we assume we have fully preemptive threads and that sleep()
38 * does the right thing.
41 ldap_pvt_thread_sleep(
52 ldap_pvt_thread_sleep(
61 if ( lwp_self( &mylwp ) < 0 ) {
67 mon_enter( &sglob->tsl_mon );
69 if ( sglob->tsl_list != NULL ) {
70 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
71 if ( SAMETHREAD( t->tl_tid, mylwp )) {
72 /* We're already sleeping? */
73 t->tl_wake = now + interval;
74 mon_exit( &sglob->tsl_mon );
81 nt = (tl_t *) malloc( sizeof( tl_t ));
83 nt->tl_next = sglob->tsl_list;
84 nt->tl_wake = now + interval;
88 mon_exit( &sglob->tsl_mon );
95 * The lwp_scheduler thread periodically checks to see if any threads
96 * are due to be resumed. If there are, it resumes them. Otherwise,
97 * it computes the lesser of ( 1 second ) or ( the minimum time until
98 * a thread need to be resumed ) and puts itself to sleep for that amount
107 struct timeval interval;
110 while ( !sglob->slurpd_shutdown ) {
111 mon_enter( &sglob->tsl_mon );
115 if ( sglob->tsl_list != NULL ) {
116 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
117 if (( t->tl_wake > 0L ) && ( t->tl_wake < now )) {
118 lwp_resume( t->tl_tid );
122 if (( t->tl_wake > now ) && ( t->tl_wake < min )) {
128 mon_exit( &sglob->tsl_mon );
130 interval.tv_usec = 0L;
132 interval.tv_sec = 1L;
134 interval.tv_sec = min;
137 lwp_sleep( &interval );
140 mon_enter( &sglob->tsl_mon );
142 for ( t = sglob->tsl_list; t != NULL; t = t->tl_next ) {
143 lwp_resume( t->tl_tid );
146 mon_exit( &sglob->tsl_mon );
148 free_stack( stackno );
151 #endif /* HAVE_LWP */