1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 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, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
43 /* $Id: coop.c,v 1.28 2001-10-06 16:30:20 mdj Exp $ */
45 /* Cooperative thread library, based on QuickThreads */
56 #include "libguile/eval.h"
58 \f/* #define COOP_STKSIZE (0x10000) */
59 #define COOP_STKSIZE (scm_eval_stack)
61 /* `alignment' must be a power of 2. */
62 #define COOP_STKALIGN(sp, alignment) \
63 ((void *)((((qt_word_t)(sp)) + (alignment) - 1) & ~((alignment)-1)))
67 /* Queue access functions. */
70 coop_qinit (coop_q_t
*q
)
72 q
->t
.next
= q
->tail
= &q
->t
;
80 q
->t
.exceptfds
= NULL
;
87 coop_qget (coop_q_t
*q
)
96 { /* If it was already empty .. */
97 return NULL
; /* .. say so. */
99 q
->tail
= &q
->t
; /* Else now it is empty. */
106 coop_qput (coop_q_t
*q
, coop_t
*t
)
114 coop_all_qput (coop_q_t
*q
, coop_t
*t
)
117 q
->t
.all_next
->all_prev
= t
;
119 t
->all_next
= q
->t
.all_next
;
124 coop_all_qremove (coop_q_t
*q
, coop_t
*t
)
127 t
->all_prev
->all_next
= t
->all_next
;
129 q
->t
.all_next
= t
->all_next
;
131 t
->all_next
->all_prev
= t
->all_prev
;
135 /* Insert thread t into the ordered queue q.
136 q is ordered after wakeup_time. Threads which aren't sleeping but
137 waiting for I/O go last into the queue. */
139 coop_timeout_qinsert (coop_q_t
*q
, coop_t
*t
)
141 coop_t
*pred
= &q
->t
;
142 int sec
= t
->wakeup_time
.tv_sec
;
143 int usec
= t
->wakeup_time
.tv_usec
;
144 while (pred
->next
!= &q
->t
145 && pred
->next
->timeoutp
146 && (pred
->next
->wakeup_time
.tv_sec
< sec
147 || (pred
->next
->wakeup_time
.tv_sec
== sec
148 && pred
->next
->wakeup_time
.tv_usec
< usec
)))
150 t
->next
= pred
->next
;
152 if (t
->next
== &q
->t
)
158 \f/* Thread routines. */
160 coop_q_t coop_global_runq
; /* A queue of runable threads. */
161 coop_q_t coop_global_sleepq
; /* A queue of sleeping threads. */
162 coop_q_t coop_tmp_queue
; /* A temp working queue */
163 coop_q_t coop_global_allq
; /* A queue of all threads. */
164 static coop_t coop_global_main
; /* Thread for the process. */
165 coop_t
*coop_global_curr
; /* Currently-executing thread. */
167 #ifdef GUILE_PTHREAD_COMPAT
168 static coop_q_t coop_deadq
;
169 static int coop_quitting_p
= -1;
170 static pthread_cond_t coop_cond_quit
;
171 static pthread_cond_t coop_cond_create
;
172 static pthread_mutex_t coop_mutex_create
;
173 static pthread_t coop_mother
;
174 static int mother_awake_p
= 0;
175 static coop_t
*coop_child
;
178 static void *coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
);
179 static void coop_only (void *pu
, void *pt
, qt_userf_t
*f
);
180 static void *coop_aborthelp (qt_t
*sp
, void *old
, void *null
);
181 static void *coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
);
184 /* called on process termination. */
190 extern int on_exit (void (*procp
) (), int arg
);
193 coop_finish (int status
, void *arg
)
195 #error Dont know how to setup a cleanup handler on your system.
199 #ifdef GUILE_PTHREAD_COMPAT
201 pthread_cond_signal (&coop_cond_create
);
202 pthread_cond_broadcast (&coop_cond_quit
);
209 coop_qinit (&coop_global_runq
);
210 coop_qinit (&coop_global_sleepq
);
211 coop_qinit (&coop_tmp_queue
);
212 coop_qinit (&coop_global_allq
);
213 coop_global_curr
= &coop_global_main
;
214 #ifdef GUILE_PTHREAD_COMPAT
215 coop_qinit (&coop_deadq
);
216 pthread_cond_init (&coop_cond_quit
, NULL
);
217 pthread_cond_init (&coop_cond_create
, NULL
);
218 pthread_mutex_init (&coop_mutex_create
, NULL
);
221 atexit (coop_finish
);
224 on_exit (coop_finish
, 0);
229 /* Return the next runnable thread. If no threads are currently runnable,
230 and there are sleeping threads - wait until one wakes up. Otherwise,
233 #ifndef GUILE_ISELECT
235 coop_next_runnable_thread()
245 /* Check the sleeping queue */
246 while ((t
= coop_qget(&coop_global_sleepq
)) != NULL
)
249 if (t
->wakeup_time
<= now
)
250 coop_qput(&coop_global_runq
, t
);
252 coop_qput(&coop_tmp_queue
, t
);
254 while ((t
= coop_qget(&coop_tmp_queue
)) != NULL
)
255 coop_qput(&coop_global_sleepq
, t
);
257 t
= coop_qget (&coop_global_runq
);
259 } while ((t
== NULL
) && (sleepers
> 0));
270 while ((next
= coop_qget (&coop_global_runq
)) != NULL
) {
271 coop_global_curr
= next
;
272 QT_BLOCK (coop_starthelp
, 0, 0, next
->sp
);
278 coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
)
280 coop_global_main
.sp
= old
;
281 coop_global_main
.joining
= NULL
;
282 coop_qput (&coop_global_runq
, &coop_global_main
);
283 return NULL
; /* not used, but keeps compiler happy */
287 coop_mutex_init (coop_m
*m
)
289 return coop_new_mutex_init (m
, NULL
);
293 coop_new_mutex_init (coop_m
*m
, coop_mattr
*attr
)
296 coop_qinit(&(m
->waiting
));
301 coop_mutex_trylock (coop_m
*m
)
303 if (m
->owner
== NULL
)
305 m
->owner
= coop_global_curr
;
313 coop_mutex_lock (coop_m
*m
)
315 if (m
->owner
== NULL
)
317 m
->owner
= coop_global_curr
;
321 coop_t
*old
, *newthread
;
323 /* Record the current top-of-stack before going to sleep */
324 coop_global_curr
->top
= &old
;
327 newthread
= coop_wait_for_runnable_thread();
328 if (newthread
== coop_global_curr
)
331 newthread
= coop_next_runnable_thread();
333 old
= coop_global_curr
;
334 coop_global_curr
= newthread
;
335 QT_BLOCK (coop_yieldhelp
, old
, &(m
->waiting
), newthread
->sp
);
342 coop_mutex_unlock (coop_m
*m
)
344 coop_t
*old
, *newthread
;
346 newthread
= coop_qget (&(m
->waiting
));
347 if (newthread
!= NULL
)
349 /* Record the current top-of-stack before going to sleep */
350 coop_global_curr
->top
= &old
;
352 old
= coop_global_curr
;
353 coop_global_curr
= newthread
;
354 /* The new thread came into m->waiting through a lock operation.
355 It now owns this mutex. */
356 m
->owner
= coop_global_curr
;
357 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
368 coop_mutex_destroy (coop_m
*m
)
375 coop_condition_variable_init (coop_c
*c
)
377 return coop_new_condition_variable_init (c
, NULL
);
381 coop_new_condition_variable_init (coop_c
*c
, coop_cattr
*a
)
383 coop_qinit(&(c
->waiting
));
388 coop_condition_variable_wait_mutex (coop_c
*c
, coop_m
*m
)
390 coop_t
*old
, *newthread
;
392 /* coop_mutex_unlock (m); */
393 newthread
= coop_qget (&(m
->waiting
));
394 if (newthread
!= NULL
)
396 m
->owner
= newthread
;
401 /*fixme* Should we really wait here? Isn't it OK just to proceed? */
403 newthread
= coop_wait_for_runnable_thread();
404 if (newthread
== coop_global_curr
)
407 newthread
= coop_next_runnable_thread();
410 coop_global_curr
->top
= &old
;
411 old
= coop_global_curr
;
412 coop_global_curr
= newthread
;
413 QT_BLOCK (coop_yieldhelp
, old
, &(c
->waiting
), newthread
->sp
);
420 coop_condition_variable_timed_wait_mutex (coop_c
*c
,
422 const struct timespec
*abstime
)
427 /* coop_mutex_unlock (m); */
428 t
= coop_qget (&(m
->waiting
));
437 coop_global_curr
->timeoutp
= 1;
438 coop_global_curr
->wakeup_time
.tv_sec
= abstime
->tv_sec
;
439 coop_global_curr
->wakeup_time
.tv_usec
= abstime
->tv_nsec
/ 1000;
440 coop_timeout_qinsert (&coop_global_sleepq
, coop_global_curr
);
441 t
= coop_wait_for_runnable_thread();
443 /*fixme* Implement!*/
444 t
= coop_next_runnable_thread();
447 if (t
!= coop_global_curr
)
449 coop_global_curr
->top
= &old
;
450 old
= coop_global_curr
;
451 coop_global_curr
= t
;
452 QT_BLOCK (coop_yieldhelp
, old
, &(c
->waiting
), t
->sp
);
454 /* Are we still in the sleep queue? */
455 old
= &coop_global_sleepq
.t
;
456 for (t
= old
->next
; t
!= &coop_global_sleepq
.t
; old
= t
, t
= t
->next
)
457 if (t
== coop_global_curr
)
459 old
->next
= t
->next
; /* unlink */
469 coop_condition_variable_signal (coop_c
*c
)
473 while ((newthread
= coop_qget (&(c
->waiting
))) != NULL
)
475 coop_qput (&coop_global_runq
, newthread
);
483 static int n_keys
= 0;
484 static int max_keys
= 0;
485 static void (**destructors
) (void *) = 0;
488 coop_key_create (coop_k
*keyp
, void (*destructor
) (void *value
))
490 if (n_keys
>= max_keys
)
493 max_keys
= max_keys
? max_keys
* 3 / 2 : 10;
494 destructors
= realloc (destructors
, sizeof (void *) * max_keys
);
495 if (destructors
== 0)
497 fprintf (stderr
, "Virtual memory exceeded in coop_key_create\n");
500 for (i
= n_keys
; i
< max_keys
; ++i
)
501 destructors
[i
] = NULL
;
503 destructors
[n_keys
] = destructor
;
509 coop_setspecific (coop_k key
, const void *value
)
511 int n_keys
= coop_global_curr
->n_keys
;
515 coop_global_curr
->n_keys
= max_keys
;
516 coop_global_curr
->specific
= realloc (n_keys
517 ? coop_global_curr
->specific
519 sizeof (void *) * max_keys
);
520 if (coop_global_curr
->specific
== 0)
522 fprintf (stderr
, "Virtual memory exceeded in coop_setspecific\n");
525 for (i
= n_keys
; i
< max_keys
; ++i
)
526 coop_global_curr
->specific
[i
] = NULL
;
528 coop_global_curr
->specific
[key
] = (void *) value
;
533 coop_getspecific (coop_k key
)
535 return (key
< coop_global_curr
->n_keys
536 ? coop_global_curr
->specific
[key
]
541 coop_key_delete (coop_k key
)
548 coop_condition_variable_destroy (coop_c
*c
)
553 #ifdef GUILE_PTHREAD_COMPAT
555 /* 1K room for the cond wait routine */
556 #ifdef SCM_STACK_GROWS_UP
557 #define COOP_STACK_ROOM (256)
559 #define COOP_STACK_ROOM (-256)
563 dummy_start (void *coop_thread
)
565 coop_t
*t
= (coop_t
*) coop_thread
;
567 t
->sp
= (qt_t
*) (&t
+ COOP_STACK_ROOM
);
568 pthread_mutex_init (&t
->dummy_mutex
, NULL
);
569 pthread_mutex_lock (&t
->dummy_mutex
);
572 res
= pthread_cond_wait (&coop_cond_quit
, &t
->dummy_mutex
);
573 while (res
== EINTR
);
580 pthread_mutex_lock (&coop_mutex_create
);
581 while (!coop_quitting_p
)
584 pthread_create (&coop_child
->dummy_thread
,
590 res
= pthread_cond_wait (&coop_cond_create
, &coop_mutex_create
);
591 while (res
== EINTR
);
599 coop_create (coop_userf_t
*f
, void *pu
)
602 #ifndef GUILE_PTHREAD_COMPAT
606 #ifdef GUILE_PTHREAD_COMPAT
607 t
= coop_qget (&coop_deadq
);
617 t
= malloc (sizeof (coop_t
));
621 #ifdef GUILE_PTHREAD_COMPAT
624 if (coop_quitting_p
< 0)
627 /* We can't create threads ourselves since the pthread
628 * corresponding to this stack might be sleeping.
630 pthread_create (&coop_mother
, NULL
, mother
, NULL
);
634 pthread_cond_signal (&coop_cond_create
);
636 /* We can't use a pthreads condition variable since "this"
637 * pthread could already be asleep. We can't use a COOP
638 * condition variable because they are not safe against
639 * pre-emptive switching.
641 while (coop_child
|| mother_awake_p
)
644 t
->sto
= malloc (COOP_STKSIZE
);
645 sto
= COOP_STKALIGN (t
->sto
, QT_STKALIGN
);
646 t
->sp
= QT_SP (sto
, COOP_STKSIZE
- QT_STKALIGN
);
650 t
->sp
= QT_ARGS (t
->sp
, pu
, t
, (qt_userf_t
*)f
, coop_only
);
652 coop_qput (&coop_global_runq
, t
);
653 coop_all_qput (&coop_global_allq
, t
);
660 coop_only (void *pu
, void *pt
, qt_userf_t
*f
)
662 coop_global_curr
= (coop_t
*)pt
;
663 (*(coop_userf_t
*)f
)(pu
);
672 coop_t
*old
, *newthread
;
674 /* Wake up any threads that are waiting to join this one */
675 if (coop_global_curr
->joining
)
677 while ((newthread
= coop_qget ((coop_q_t
*)(coop_global_curr
->joining
)))
680 coop_qput (&coop_global_runq
, newthread
);
682 free (coop_global_curr
->joining
);
688 newthread
= coop_wait_for_runnable_thread();
689 } while (newthread
== coop_global_curr
);
692 newthread
= coop_next_runnable_thread();
694 coop_all_qremove (&coop_global_allq
, coop_global_curr
);
695 old
= coop_global_curr
;
696 coop_global_curr
= newthread
;
697 QT_ABORT (coop_aborthelp
, old
, (void *) NULL
, newthread
->sp
);
702 coop_aborthelp (qt_t
*sp
, void *old
, void *null
)
704 coop_t
*oldthread
= (coop_t
*) old
;
706 if (oldthread
->specific
)
707 free (oldthread
->specific
);
708 #ifndef GUILE_PTHREAD_COMPAT
709 free (oldthread
->sto
);
712 coop_qput (&coop_deadq
, oldthread
);
722 coop_t
*old
, *newthread
;
724 /* Create a join list if necessary */
725 if (t
->joining
== NULL
)
727 t
->joining
= malloc(sizeof(coop_q_t
));
728 coop_qinit((coop_q_t
*) t
->joining
);
732 newthread
= coop_wait_for_runnable_thread();
733 if (newthread
== coop_global_curr
)
736 newthread
= coop_next_runnable_thread();
738 old
= coop_global_curr
;
739 coop_global_curr
= newthread
;
740 QT_BLOCK (coop_yieldhelp
, old
, (coop_q_t
*) t
->joining
, newthread
->sp
);
749 newthread
= coop_next_runnable_thread();
751 /* There may be no other runnable threads. Return if this is the
754 if (newthread
== coop_global_curr
)
757 if (newthread
== NULL
)
761 old
= coop_global_curr
;
763 coop_global_curr
= newthread
;
764 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
769 coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
)
771 ((coop_t
*)old
)->sp
= sp
;
772 coop_qput ((coop_q_t
*)blockq
, (coop_t
*)old
);
776 /* Replacement for the system's sleep() function. Does the right thing
777 for the process - but not for the system (it busy-waits) */
780 coop_sleephelp (qt_t
*sp
, void *old
, void *blockq
)
782 ((coop_t
*)old
)->sp
= sp
;
783 /* old is already on the sleep queue - so there's no need to
784 do anything extra here */
791 scm_thread_usleep (unsigned long usec
)
793 struct timeval timeout
;
795 timeout
.tv_usec
= usec
;
796 scm_internal_select (0, NULL
, NULL
, NULL
, &timeout
);
797 return 0; /* Maybe we should calculate actual time slept,
798 but this is faster... :) */
802 scm_thread_sleep (unsigned long sec
)
804 time_t now
= time (NULL
);
805 struct timeval timeout
;
807 timeout
.tv_sec
= sec
;
809 scm_internal_select (0, NULL
, NULL
, NULL
, &timeout
);
810 slept
= time (NULL
) - now
;
811 return slept
> sec
? 0 : sec
- slept
;
814 #else /* GUILE_ISELECT */
817 scm_thread_sleep (unsigned long s
)
819 coop_t
*newthread
, *old
;
820 time_t now
= time (NULL
);
821 coop_global_curr
->wakeup_time
= now
+ s
;
823 /* Put the current thread on the sleep queue */
824 coop_qput (&coop_global_sleepq
, coop_global_curr
);
826 newthread
= coop_next_runnable_thread();
828 /* If newthread is the same as the sleeping thread, do nothing */
829 if (newthread
== coop_global_curr
)
832 old
= coop_global_curr
;
834 coop_global_curr
= newthread
;
835 QT_BLOCK (coop_sleephelp
, old
, NULL
, newthread
->sp
);
841 scm_thread_usleep (unsigned long usec
)
843 /* We're so cheap. */
844 scm_thread_sleep (usec
/ 1000000);
845 struct timeval timeout
;
846 return 0; /* Maybe we should calculate actual time slept,
847 but this is faster... :) */
850 #endif /* GUILE_ISELECT */