1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 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.35 2003-03-25 23:54:01 rlb Exp $ */
45 /* Cooperative thread library, based on QuickThreads */
60 #include "libguile/eval.h"
62 \f/* #define COOP_STKSIZE (0x10000) */
63 #define COOP_STKSIZE (scm_eval_stack)
65 /* `alignment' must be a power of 2. */
66 #define COOP_STKALIGN(sp, alignment) \
67 ((void *)((((qt_word_t)(sp)) + (alignment) - 1) & ~((alignment)-1)))
71 /* Queue access functions. */
74 coop_qinit (coop_q_t
*q
)
76 q
->t
.next
= q
->tail
= &q
->t
;
83 q
->t
.exceptfds
= NULL
;
89 coop_qget (coop_q_t
*q
)
98 { /* If it was already empty .. */
99 return NULL
; /* .. say so. */
101 q
->tail
= &q
->t
; /* Else now it is empty. */
108 coop_qput (coop_q_t
*q
, coop_t
*t
)
116 coop_all_qput (coop_q_t
*q
, coop_t
*t
)
119 q
->t
.all_next
->all_prev
= t
;
121 t
->all_next
= q
->t
.all_next
;
126 coop_all_qremove (coop_q_t
*q
, coop_t
*t
)
129 t
->all_prev
->all_next
= t
->all_next
;
131 q
->t
.all_next
= t
->all_next
;
133 t
->all_next
->all_prev
= t
->all_prev
;
136 /* Insert thread t into the ordered queue q.
137 q is ordered after wakeup_time. Threads which aren't sleeping but
138 waiting for I/O go last into the queue. */
140 coop_timeout_qinsert (coop_q_t
*q
, coop_t
*t
)
142 coop_t
*pred
= &q
->t
;
143 int sec
= t
->wakeup_time
.tv_sec
;
144 int usec
= t
->wakeup_time
.tv_usec
;
145 while (pred
->next
!= &q
->t
146 && pred
->next
->timeoutp
147 && (pred
->next
->wakeup_time
.tv_sec
< sec
148 || (pred
->next
->wakeup_time
.tv_sec
== sec
149 && pred
->next
->wakeup_time
.tv_usec
< usec
)))
151 t
->next
= pred
->next
;
153 if (t
->next
== &q
->t
)
159 /* Thread routines. */
161 coop_q_t coop_global_runq
; /* A queue of runable threads. */
162 coop_q_t coop_global_sleepq
; /* A queue of sleeping threads. */
163 coop_q_t coop_tmp_queue
; /* A temp working queue */
164 coop_q_t coop_global_allq
; /* A queue of all threads. */
165 static coop_t coop_global_main
; /* Thread for the process. */
166 coop_t
*coop_global_curr
; /* Currently-executing thread. */
168 #ifdef GUILE_PTHREAD_COMPAT
169 static coop_q_t coop_deadq
;
170 static int coop_quitting_p
= -1;
171 static pthread_cond_t coop_cond_quit
;
172 static pthread_cond_t coop_cond_create
;
173 static pthread_mutex_t coop_mutex_create
;
174 static pthread_t coop_mother
;
175 static int mother_awake_p
= 0;
176 static coop_t
*coop_child
;
179 static void *coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
);
180 static void coop_only (void *pu
, void *pt
, qt_userf_t
*f
);
181 static void *coop_aborthelp (qt_t
*sp
, void *old
, void *null
);
182 static void *coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
);
185 /* called on process termination. */
191 extern int on_exit (void (*procp
) (), int arg
);
194 coop_finish (int status
, void *arg
)
196 #error Dont know how to setup a cleanup handler on your system.
200 #ifdef GUILE_PTHREAD_COMPAT
202 pthread_cond_signal (&coop_cond_create
);
203 pthread_cond_broadcast (&coop_cond_quit
);
210 coop_qinit (&coop_global_runq
);
211 coop_qinit (&coop_global_sleepq
);
212 coop_qinit (&coop_tmp_queue
);
213 coop_qinit (&coop_global_allq
);
214 coop_global_curr
= &coop_global_main
;
215 #ifdef GUILE_PTHREAD_COMPAT
216 coop_qinit (&coop_deadq
);
217 pthread_cond_init (&coop_cond_quit
, NULL
);
218 pthread_cond_init (&coop_cond_create
, NULL
);
219 pthread_mutex_init (&coop_mutex_create
, NULL
);
222 atexit (coop_finish
);
225 on_exit (coop_finish
, 0);
235 while ((next
= coop_qget (&coop_global_runq
)) != NULL
) {
236 coop_global_curr
= next
;
237 QT_BLOCK (coop_starthelp
, 0, 0, next
->sp
);
243 coop_starthelp (qt_t
*old
, void *ignore0
, void *ignore1
)
245 coop_global_main
.sp
= old
;
246 coop_global_main
.joining
= NULL
;
247 coop_qput (&coop_global_runq
, &coop_global_main
);
248 return NULL
; /* not used, but keeps compiler happy */
252 coop_mutex_init (coop_m
*m
)
254 return coop_new_mutex_init (m
, NULL
);
258 coop_new_mutex_init (coop_m
*m
, coop_mattr
*attr
)
262 coop_qinit(&(m
->waiting
));
267 coop_mutex_trylock (coop_m
*m
)
269 if (m
->owner
== NULL
)
271 m
->owner
= coop_global_curr
;
274 else if (m
->owner
== coop_global_curr
)
284 coop_mutex_lock (coop_m
*m
)
286 if (m
->owner
== NULL
)
288 m
->owner
= coop_global_curr
;
290 else if (m
->owner
== coop_global_curr
)
296 coop_t
*old
, *newthread
;
298 /* Record the current top-of-stack before going to sleep */
299 coop_global_curr
->top
= &old
;
301 newthread
= coop_wait_for_runnable_thread();
302 if (newthread
== coop_global_curr
)
304 old
= coop_global_curr
;
305 coop_global_curr
= newthread
;
306 QT_BLOCK (coop_yieldhelp
, old
, &(m
->waiting
), newthread
->sp
);
313 coop_mutex_unlock (coop_m
*m
)
315 coop_t
*old
, *newthread
;
319 newthread
= coop_qget (&(m
->waiting
));
320 if (newthread
!= NULL
)
322 /* Record the current top-of-stack before going to sleep */
323 coop_global_curr
->top
= &old
;
325 old
= coop_global_curr
;
326 coop_global_curr
= newthread
;
327 /* The new thread came into m->waiting through a lock operation.
328 It now owns this mutex. */
329 m
->owner
= coop_global_curr
;
330 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
337 else if (m
->level
> 0)
347 coop_mutex_destroy (coop_m
*m
)
354 coop_condition_variable_init (coop_c
*c
)
356 return coop_new_condition_variable_init (c
, NULL
);
360 coop_new_condition_variable_init (coop_c
*c
, coop_cattr
*a
)
362 coop_qinit(&(c
->waiting
));
367 coop_condition_variable_wait_mutex (coop_c
*c
, coop_m
*m
)
369 coop_t
*old
, *newthread
;
371 /* coop_mutex_unlock (m); */
372 newthread
= coop_qget (&(m
->waiting
));
373 if (newthread
!= NULL
)
375 m
->owner
= newthread
;
380 /*fixme* Should we really wait here? Isn't it OK just to proceed? */
381 newthread
= coop_wait_for_runnable_thread();
382 if (newthread
== coop_global_curr
)
385 coop_global_curr
->top
= &old
;
386 old
= coop_global_curr
;
387 coop_global_curr
= newthread
;
388 QT_BLOCK (coop_yieldhelp
, old
, &(c
->waiting
), newthread
->sp
);
395 coop_condition_variable_timed_wait_mutex (coop_c
*c
,
397 const scm_t_timespec
*abstime
)
402 #elif defined (WSAETIMEDOUT)
403 int res
= WSAETIMEDOUT
;
408 /* coop_mutex_unlock (m); */
409 t
= coop_qget (&(m
->waiting
));
417 coop_global_curr
->timeoutp
= 1;
418 coop_global_curr
->wakeup_time
.tv_sec
= abstime
->tv_sec
;
419 coop_global_curr
->wakeup_time
.tv_usec
= abstime
->tv_nsec
/ 1000;
420 coop_timeout_qinsert (&coop_global_sleepq
, coop_global_curr
);
421 t
= coop_wait_for_runnable_thread();
423 if (t
!= coop_global_curr
)
425 coop_global_curr
->top
= &old
;
426 old
= coop_global_curr
;
427 coop_global_curr
= t
;
428 QT_BLOCK (coop_yieldhelp
, old
, &(c
->waiting
), t
->sp
);
430 /* Are we still in the sleep queue? */
431 old
= &coop_global_sleepq
.t
;
432 for (t
= old
->next
; t
!= &coop_global_sleepq
.t
; old
= t
, t
= t
->next
)
433 if (t
== coop_global_curr
)
435 old
->next
= t
->next
; /* unlink */
445 coop_condition_variable_broadcast (coop_c
*c
)
449 while ((newthread
= coop_qget (&(c
->waiting
))) != NULL
)
451 coop_qput (&coop_global_runq
, newthread
);
457 coop_condition_variable_signal (coop_c
*c
)
459 return coop_condition_variable_broadcast (c
);
466 static int n_keys
= 0;
467 static int max_keys
= 0;
468 static void (**destructors
) (void *) = 0;
471 coop_key_create (coop_k
*keyp
, void (*destructor
) (void *value
))
473 if (n_keys
>= max_keys
)
476 max_keys
= max_keys
? max_keys
* 3 / 2 : 10;
477 destructors
= realloc (destructors
, sizeof (void *) * max_keys
);
478 if (destructors
== 0)
480 fprintf (stderr
, "Virtual memory exceeded in coop_key_create\n");
483 for (i
= n_keys
; i
< max_keys
; ++i
)
484 destructors
[i
] = NULL
;
486 destructors
[n_keys
] = destructor
;
492 coop_setspecific (coop_k key
, const void *value
)
494 int n_keys
= coop_global_curr
->n_keys
;
498 coop_global_curr
->n_keys
= max_keys
;
499 coop_global_curr
->specific
= realloc (n_keys
500 ? coop_global_curr
->specific
502 sizeof (void *) * max_keys
);
503 if (coop_global_curr
->specific
== 0)
505 fprintf (stderr
, "Virtual memory exceeded in coop_setspecific\n");
508 for (i
= n_keys
; i
< max_keys
; ++i
)
509 coop_global_curr
->specific
[i
] = NULL
;
511 coop_global_curr
->specific
[key
] = (void *) value
;
516 coop_getspecific (coop_k key
)
518 return (key
< coop_global_curr
->n_keys
519 ? coop_global_curr
->specific
[key
]
524 coop_key_delete (coop_k key
)
531 coop_condition_variable_destroy (coop_c
*c
)
536 #ifdef GUILE_PTHREAD_COMPAT
538 /* 1K room for the cond wait routine */
539 #ifdef SCM_STACK_GROWS_UP
540 #define COOP_STACK_ROOM (256)
542 #define COOP_STACK_ROOM (-256)
546 dummy_start (void *coop_thread
)
548 coop_t
*t
= (coop_t
*) coop_thread
;
550 t
->sp
= (qt_t
*) (&t
+ COOP_STACK_ROOM
);
551 pthread_mutex_init (&t
->dummy_mutex
, NULL
);
552 pthread_mutex_lock (&t
->dummy_mutex
);
555 res
= pthread_cond_wait (&coop_cond_quit
, &t
->dummy_mutex
);
556 while (res
== EINTR
);
563 pthread_mutex_lock (&coop_mutex_create
);
564 while (!coop_quitting_p
)
567 pthread_create (&coop_child
->dummy_thread
,
573 res
= pthread_cond_wait (&coop_cond_create
, &coop_mutex_create
);
574 while (res
== EINTR
);
582 coop_create (coop_userf_t
*f
, void *pu
)
585 #ifndef GUILE_PTHREAD_COMPAT
589 #ifdef GUILE_PTHREAD_COMPAT
590 t
= coop_qget (&coop_deadq
);
600 t
= scm_malloc (sizeof (coop_t
));
603 #ifdef GUILE_PTHREAD_COMPAT
606 if (coop_quitting_p
< 0)
609 /* We can't create threads ourselves since the pthread
610 * corresponding to this stack might be sleeping.
612 pthread_create (&coop_mother
, NULL
, mother
, NULL
);
616 pthread_cond_signal (&coop_cond_create
);
618 /* We can't use a pthreads condition variable since "this"
619 * pthread could already be asleep. We can't use a COOP
620 * condition variable because they are not safe against
621 * pre-emptive switching.
623 while (coop_child
|| mother_awake_p
)
626 t
->sto
= scm_malloc (COOP_STKSIZE
);
627 sto
= COOP_STKALIGN (t
->sto
, QT_STKALIGN
);
628 t
->sp
= QT_SP (sto
, COOP_STKSIZE
- QT_STKALIGN
);
632 t
->sp
= QT_ARGS (t
->sp
, pu
, t
, (qt_userf_t
*)f
, coop_only
);
634 coop_qput (&coop_global_runq
, t
);
635 coop_all_qput (&coop_global_allq
, t
);
642 coop_only (void *pu
, void *pt
, qt_userf_t
*f
)
644 coop_global_curr
= (coop_t
*)pt
;
645 (*(coop_userf_t
*)f
)(pu
);
654 coop_t
*old
, *newthread
;
656 /* Wake up any threads that are waiting to join this one */
657 if (coop_global_curr
->joining
)
659 while ((newthread
= coop_qget ((coop_q_t
*)(coop_global_curr
->joining
)))
662 coop_qput (&coop_global_runq
, newthread
);
664 free (coop_global_curr
->joining
);
669 newthread
= coop_wait_for_runnable_thread();
670 } while (newthread
== coop_global_curr
);
672 coop_all_qremove (&coop_global_allq
, coop_global_curr
);
673 old
= coop_global_curr
;
674 coop_global_curr
= newthread
;
675 QT_ABORT (coop_aborthelp
, old
, (void *) NULL
, newthread
->sp
);
680 coop_aborthelp (qt_t
*sp
, void *old
, void *null
)
682 coop_t
*oldthread
= (coop_t
*) old
;
684 if (oldthread
->specific
)
685 free (oldthread
->specific
);
686 #ifndef GUILE_PTHREAD_COMPAT
687 free (oldthread
->sto
);
690 coop_qput (&coop_deadq
, oldthread
);
700 coop_t
*old
, *newthread
;
702 /* Create a join list if necessary */
703 if (t
->joining
== NULL
)
705 t
->joining
= scm_malloc(sizeof(coop_q_t
));
706 coop_qinit((coop_q_t
*) t
->joining
);
709 newthread
= coop_wait_for_runnable_thread();
710 if (newthread
== coop_global_curr
)
712 old
= coop_global_curr
;
713 coop_global_curr
= newthread
;
714 QT_BLOCK (coop_yieldhelp
, old
, (coop_q_t
*) t
->joining
, newthread
->sp
);
723 newthread
= coop_next_runnable_thread();
725 /* There may be no other runnable threads. Return if this is the
727 if (newthread
== coop_global_curr
)
730 old
= coop_global_curr
;
732 coop_global_curr
= newthread
;
733 QT_BLOCK (coop_yieldhelp
, old
, &coop_global_runq
, newthread
->sp
);
738 coop_yieldhelp (qt_t
*sp
, void *old
, void *blockq
)
740 ((coop_t
*)old
)->sp
= sp
;
741 coop_qput ((coop_q_t
*)blockq
, (coop_t
*)old
);
745 /* Replacement for the system's sleep() function. Does the right thing
746 for the process - but not for the system (it busy-waits) */
749 coop_sleephelp (qt_t
*sp
, void *old
, void *blockq
)
751 ((coop_t
*)old
)->sp
= sp
;
752 /* old is already on the sleep queue - so there's no need to
753 do anything extra here */
758 scm_thread_usleep (unsigned long usec
)
760 struct timeval timeout
;
762 timeout
.tv_usec
= usec
;
763 scm_internal_select (0, NULL
, NULL
, NULL
, &timeout
);
764 return 0; /* Maybe we should calculate actual time slept,
765 but this is faster... :) */
769 scm_thread_sleep (unsigned long sec
)
771 time_t now
= time (NULL
);
772 struct timeval timeout
;
774 timeout
.tv_sec
= sec
;
776 scm_internal_select (0, NULL
, NULL
, NULL
, &timeout
);
777 slept
= time (NULL
) - now
;
778 return slept
> sec
? 0 : sec
- slept
;