factor/library/threads.factor

42 lines
1.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-08-20 21:26:25 -04:00
IN: threads
USING: io-internals kernel kernel-internals lists namespaces ;
2005-02-07 18:04:49 -05:00
! Core of the multitasker. Used by io-internals.factor and
! in-thread.factor.
2004-08-20 21:26:25 -04:00
: run-queue ( -- queue ) 9 getenv ;
: set-run-queue ( queue -- ) 9 setenv ;
2004-08-20 21:26:25 -04:00
2005-02-07 18:04:49 -05:00
: init-threads ( -- )
<dlist> set-run-queue ;
2004-08-20 21:26:25 -04:00
: next-thread ( -- quot )
2005-02-07 18:04:49 -05:00
run-queue dlist-pop-front ;
2004-08-20 21:26:25 -04:00
: schedule-thread ( quot -- )
2005-02-07 18:04:49 -05:00
run-queue dlist-push-end ;
2004-08-20 21:26:25 -04:00
: (yield) ( -- )
#! If there is a quotation in the run queue, call it,
#! otherwise wait for I/O. The currently executing
#! continuation is suspended. Use yield instead.
next-thread [
2004-08-20 21:26:25 -04:00
call
] [
next-io-task [
2004-08-20 21:26:25 -04:00
call
] [
(yield)
] ifte*
] ifte* ;
2004-08-20 21:26:25 -04:00
: yield ( -- )
#! Add the current continuation to the run queue, and yield
#! to the next quotation. The current continuation will
#! eventually be restored by a future call to (yield) or
#! yield.
[ schedule-thread (yield) ] callcc0 ;
2005-02-07 18:04:49 -05:00