1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
43 /* $Id: coop.c,v 1.1 1997-04-15 01:34:30 jimb Exp $ */
45 /* Cooperative thread library, based on QuickThreads */
49 #define COOP_STKSIZE (0x10000)
51 /* `alignment' must be a power of 2. */
52 #define COOP_STKALIGN(sp, alignment) \
53 ((void *)((((qt_word_t)(sp)) + (alignment) - 1) & ~((alignment)-1)))
57 /* Queue access functions. */
61 coop_qinit (coop_q_t
*q
)
68 q
->t
.next
= q
->tail
= &q
->t
;
77 coop_qget (coop_q_t
*q
)
88 if (t
->next
== &q
->t
) {
89 if (t
== &q
->t
) { /* If it was already empty .. */
90 return (NULL
); /* .. say so. */
92 q
->tail
= &q
->t
; /* Else now it is empty. */
100 coop_qput (coop_q_t
*q
, coop_t
*t
)
115 coop_all_qput (coop_q_t
*q
, coop_t
*t
)
124 q
->t
.all_next
->all_prev
= t
;
126 t
->all_next
= q
->t
.all_next
;
132 coop_all_qremove (coop_q_t
*q
, coop_t
*t
)
135 coop_all_qremove (q
, t
)
141 t
->all_prev
->all_next
= t
->all_next
;
143 q
->t
.all_next
= t
->all_next
;
145 t
->all_next
->all_prev
= t
->all_prev
;
149 \f/* Thread routines. */
151 coop_q_t coop_global_runq
; /* A queue of runable threads. */
152 coop_q_t coop_global_sleepq
; /* A queue of sleeping threads. */
153 static coop_q_t tmp_queue
; /* A temp working queue */
154 coop_q_t coop_global_allq
; /* A queue of all threads. */
155 static coop_t coop_global_main
; /* Thread for the process. */
156 coop_t
*coop_global_curr
; /* Currently-executing thread. */
158 static void *coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
);
159 static void coop_only (void *pu
, void *pt
, qt_userf_t
*f
);
160 static void *coop_aborthelp (qt_t
*sp
, void *old
, void *null
);
161 static void *coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
);
172 coop_qinit (&coop_global_runq
);
173 coop_qinit (&coop_global_sleepq
);
174 coop_qinit (&tmp_queue
);
175 coop_qinit (&coop_global_allq
);
176 coop_global_curr
= &coop_global_main
;
180 /* Return the next runnable thread. If no threads are currently runnable,
181 and there are sleeping threads - wait until one wakes up. Otherwise,
186 coop_next_runnable_thread()
189 coop_next_runnable_thread()
200 /* Check the sleeping queue */
201 while ((t
= coop_qget(&coop_global_sleepq
)) != NULL
)
204 if (t
->wakeup_time
<= now
)
205 coop_qput(&coop_global_runq
, t
);
207 coop_qput(&tmp_queue
, t
);
209 while ((t
= coop_qget(&tmp_queue
)) != NULL
)
210 coop_qput(&coop_global_sleepq
, t
);
212 t
= coop_qget (&coop_global_runq
);
214 } while ((t
== NULL
) && (sleepers
> 0));
230 while ((next
= coop_qget (&coop_global_runq
)) != NULL
) {
231 coop_global_curr
= next
;
232 QT_BLOCK (coop_starthelp
, 0, 0, next
->sp
);
239 coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
)
242 coop_starthelp (old
, ignore0
, ignore1
)
248 coop_global_main
.sp
= old
;
249 coop_global_main
.joining
= NULL
;
250 coop_qput (&coop_global_runq
, &coop_global_main
);
251 return NULL
; /* not used, but keeps compiler happy */
256 coop_mutex_init (coop_m
*m
)
264 coop_qinit(&(m
->waiting
));
269 coop_mutex_lock (coop_m
*m
)
276 if (m
->owner
== NULL
)
278 m
->owner
= coop_global_curr
;
282 coop_t
*old
, *newthread
;
284 /* Record the current top-of-stack before going to sleep */
285 coop_global_curr
->top
= &old
;
287 newthread
= coop_next_runnable_thread();
288 old
= coop_global_curr
;
289 coop_global_curr
= newthread
;
290 QT_BLOCK (coop_yieldhelp
, old
, &(m
->waiting
), newthread
->sp
);
297 coop_mutex_unlock (coop_m
*m
)
300 coop_mutex_unlock (m
)
304 coop_t
*old
, *newthread
;
306 newthread
= coop_qget (&(m
->waiting
));
307 if (newthread
!= NULL
)
309 /* Record the current top-of-stack before going to sleep */
310 coop_global_curr
->top
= &old
;
312 old
= coop_global_curr
;
313 coop_global_curr
= newthread
;
314 m
->owner
= coop_global_curr
;
315 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
326 coop_condition_variable_init (coop_c
*c
)
329 coop_condition_variable_init (c
)
333 coop_qinit(&(c
->waiting
));
338 coop_condition_variable_wait (coop_c
*c
)
341 coop_condition_variable_wait (c
)
345 coop_t
*old
, *newthread
;
347 newthread
= coop_next_runnable_thread();
348 old
= coop_global_curr
;
349 coop_global_curr
= newthread
;
350 QT_BLOCK (coop_yieldhelp
, old
, &(c
->waiting
), newthread
->sp
);
355 coop_condition_variable_signal (coop_c
*c
)
358 coop_condition_variable_signal (c
)
364 while ((newthread
= coop_qget (&(c
->waiting
))) != NULL
)
366 coop_qput (&coop_global_runq
, newthread
);
373 coop_create (coop_userf_t
*f
, void *pu
)
384 t
= malloc (sizeof(coop_t
));
387 t
->sto
= malloc (COOP_STKSIZE
);
388 sto
= COOP_STKALIGN (t
->sto
, QT_STKALIGN
);
389 t
->sp
= QT_SP (sto
, COOP_STKSIZE
- QT_STKALIGN
);
391 t
->sp
= QT_ARGS (t
->sp
, pu
, t
, (qt_userf_t
*)f
, coop_only
);
393 coop_qput (&coop_global_runq
, t
);
394 coop_all_qput (&coop_global_allq
, t
);
402 coop_only (void *pu
, void *pt
, qt_userf_t
*f
)
405 coop_only (pu
. pt
, f
)
411 coop_global_curr
= (coop_t
*)pt
;
412 (*(coop_userf_t
*)f
)(pu
);
426 coop_t
*old
, *newthread
;
428 /* Wake up any threads that are waiting to join this one */
429 if (coop_global_curr
->joining
)
431 while ((newthread
= coop_qget ((coop_q_t
*)(coop_global_curr
->joining
)))
434 coop_qput (&coop_global_runq
, newthread
);
436 free(coop_global_curr
->joining
);
439 newthread
= coop_next_runnable_thread();
440 coop_all_qremove(&coop_global_allq
, coop_global_curr
);
441 old
= coop_global_curr
;
442 coop_global_curr
= newthread
;
443 QT_ABORT (coop_aborthelp
, old
, (void *)NULL
, newthread
->sp
);
449 coop_aborthelp (qt_t
*sp
, void *old
, void *null
)
452 coop_aborthelp (sp
, old
, null
)
458 coop_t
*oldthread
= (coop_t
*) old
;
460 free (oldthread
->sto
);
462 /* "old" is freed in scm_threads_thread_die().
463 Marking old->base NULL indicates that this thread is dead */
465 oldthread
->base
= NULL
;
480 coop_t
*old
, *newthread
;
482 /* Check if t is already finished */
486 /* Create a join list if necessary */
487 if (t
->joining
== NULL
)
489 t
->joining
= malloc(sizeof(coop_q_t
));
490 coop_qinit((coop_q_t
*) t
->joining
);
493 newthread
= coop_next_runnable_thread();
494 old
= coop_global_curr
;
495 coop_global_curr
= newthread
;
496 QT_BLOCK (coop_yieldhelp
, old
, (coop_q_t
*) t
->joining
, newthread
->sp
);
510 newthread
= coop_next_runnable_thread();
512 /* There may be no other runnable threads. Return if this is the
514 if (newthread
== NULL
)
517 old
= coop_global_curr
;
519 coop_global_curr
= newthread
;
520 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
526 coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
)
529 coop_yieldhelp (sp
, old
, blockq
)
535 ((coop_t
*)old
)->sp
= sp
;
536 coop_qput ((coop_q_t
*)blockq
, (coop_t
*)old
);
540 /* Replacement for the system's sleep() function. Does the right thing
541 for the process - but not for the system (it busy-waits) */
545 coop_sleephelp (qt_t
*sp
, void *old
, void *blockq
)
548 coop_sleephelp (sp
, old
, bolckq
)
554 ((coop_t
*)old
)->sp
= sp
;
555 /* old is already on the sleep queue - so there's no need to
556 do anything extra here */
569 coop_t
*newthread
, *old
;
570 time_t now
= time(NULL
);
571 coop_global_curr
->wakeup_time
= now
+ s
;
573 /* Put the current thread on the sleep queue */
574 coop_qput (&coop_global_sleepq
, coop_global_curr
);
576 newthread
= coop_next_runnable_thread();
578 /* If newthread is the same as the sleeping thread, do nothing */
579 if (newthread
== coop_global_curr
)
582 old
= coop_global_curr
;
584 coop_global_curr
= newthread
;
585 QT_BLOCK (coop_sleephelp
, old
, NULL
, newthread
->sp
);