2005-01-14 12:01:48 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
2005-02-07 18:27:55 -05:00
|
|
|
! Copyright (C) 2005 Mackenzie Straight.
|
2005-01-29 14:18:28 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-08-20 21:26:25 -04:00
|
|
|
IN: threads
|
2005-05-03 20:09:04 -04:00
|
|
|
USING: errors kernel kernel-internals lists namespaces ;
|
2005-02-07 18:04:49 -05:00
|
|
|
|
2004-08-22 01:46:26 -04:00
|
|
|
! Core of the multitasker. Used by io-internals.factor and
|
|
|
|
! in-thread.factor.
|
2004-08-20 21:26:25 -04:00
|
|
|
|
2005-01-14 12:01:48 -05:00
|
|
|
: run-queue ( -- queue ) 9 getenv ;
|
|
|
|
: set-run-queue ( queue -- ) 9 setenv ;
|
2005-02-25 18:11:10 -05:00
|
|
|
: init-threads ( -- ) <queue> set-run-queue ;
|
2005-02-07 18:04:49 -05:00
|
|
|
|
2004-08-20 21:26:25 -04:00
|
|
|
: next-thread ( -- quot )
|
2005-02-25 18:11:10 -05:00
|
|
|
run-queue dup queue-empty? [
|
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
deque set-run-queue
|
|
|
|
] ifte ;
|
2004-08-20 21:26:25 -04:00
|
|
|
|
2005-05-01 14:34:01 -04:00
|
|
|
: schedule-thread ( quot -- )
|
|
|
|
run-queue enque set-run-queue ;
|
2004-08-20 21:26:25 -04:00
|
|
|
|
2005-04-22 00:22:36 -04:00
|
|
|
: stop ( -- )
|
2005-05-03 23:50:04 -04:00
|
|
|
#! Stop the current thread and begin executing the next one.
|
2005-05-01 14:34:01 -04:00
|
|
|
next-thread [ call ] [ "No more tasks" throw ] ifte* ;
|
2004-08-20 21:26:25 -04:00
|
|
|
|
2004-08-22 01:46:26 -04:00
|
|
|
: yield ( -- )
|
|
|
|
#! Add the current continuation to the run queue, and yield
|
|
|
|
#! to the next quotation. The current continuation will
|
2005-04-22 00:22:36 -04:00
|
|
|
#! eventually be restored by a future call to stop or
|
2004-08-22 01:46:26 -04:00
|
|
|
#! yield.
|
2005-04-22 00:22:36 -04:00
|
|
|
[ schedule-thread stop ] callcc0 ;
|