about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-12-28 15:57:55 -0800
committerKartik K. Agaram <vc@akkartik.com>2014-12-28 15:57:55 -0800
commit05a2cf45a52c580081ec242251c73b76760a2597 (patch)
treeed13473c52aba30a4a557579dbb07c934b84e968
parent1ccde82e8dbfb0202fb3f76a2d75be698a4b9152 (diff)
downloadmu-05a2cf45a52c580081ec242251c73b76760a2597.tar.gz
456 - routines can now have routine globals
But we don't do nonsense like copy all state from the parent routine.
-rw-r--r--mu.arc15
-rw-r--r--mu.arc.t23
2 files changed, 30 insertions, 8 deletions
diff --git a/mu.arc b/mu.arc
index 5378cccb..9106f68b 100644
--- a/mu.arc
+++ b/mu.arc
@@ -134,7 +134,10 @@
               list-address-address (obj size 1  address t  elem '(list-address))
               ; parallel routines use channels to synchronize
               channel (obj size 3  and-record t  elems '((integer) (integer) (tagged-value-array-address))  fields '(first-full first-free circular-buffer))
+              ; be careful of accidental copies to channels
               channel-address (obj size 1  address t  elem '(channel))
+              channel-address-array (obj array t  elem '(channel-address))
+              channel-address-array-address (obj size 1  address t  elem '(channel-address-array))
               ; editor
               line (obj array t  elem '(character))
               line-address (obj size 1  address t  elem '(line))
@@ -235,7 +238,8 @@
 ;
 ; responsibilities:
 ;   add routine* to either running-routines* or sleeping-routines* or completed-routines*
-;   wake up any necessary sleeping routines (either by time or on a location)
+;   wake up any necessary sleeping routines (which might be waiting for a
+;     particular time or for a particular memory location to change)
 ;   detect deadlock: kill all sleeping routines when none can be woken
 (def update-scheduler-state ()
 ;?   (trace "schedule" curr-cycle*)
@@ -487,11 +491,12 @@
                 run
                   (run (v arg.0))
                 fork
-                  (let routine (apply make-routine (v car.arg) (map m cdr.arg))
-;?                     (tr "before: " rep.routine*!alloc)
+                  ; args: fn channels globals
+                  (let routine  (apply make-routine (m arg.0) (map m (nthcdr 3 arg)))
                     (= rep.routine!alloc rep.routine*!alloc)
-                    (++ rep.routine*!alloc 1000)
-;?                     (tr "after: " rep.routine*!alloc " " rep.routine!alloc)
+                    (++ rep.routine*!alloc 1000)  ; todo: allow routines to expand past initial allocation, or to spawn multiple routines at once
+                    (= rep.routine!globals (when (len> arg 1) (m arg.1)))
+                    (= rep.routine!channels (when (len> arg 2) (m arg.2)))
                     (enq routine running-routines*))
                 assert
                   (unless (m arg.0)
diff --git a/mu.arc.t b/mu.arc.t
index 1d23dc10..e591c6e1 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -2581,7 +2581,7 @@
 (new-trace "fork-with-args")
 (add-code
   '((function f1 [
-      (fork f2:fn 4:literal)
+      (fork f2:fn nil:literal nil:literal 4:literal)
      ])
     (function f2 [
       (2:integer <- next-input)
@@ -2596,7 +2596,7 @@
   '((function f1 [
       (default-scope:scope-address <- new scope:literal 5:literal)
       (x:integer <- copy 4:literal)
-      (fork f2:fn x:integer)
+      (fork f2:fn nil:literal nil:literal x:integer)
       (x:integer <- copy 0:literal)  ; should be ignored
      ])
     (function f2 [
@@ -2606,6 +2606,23 @@
 (if (~iso memory*.2 4)
   (prn "F - fork passes args by value"))
 
+(reset)
+(new-trace "fork-global")
+(add-code
+  '((function f1 [
+      (1:integer/raw <- copy 2:integer/space:global)
+     ])
+    (function main [
+      (default-scope:scope-address <- new scope:literal 5:literal)
+      (2:integer <- copy 4:literal)
+      (fork f1:fn default-scope:scope-address)
+     ])))
+(run 'main)
+(each routine completed-routines*
+  (awhen rep.routine!error (prn "error - " it)))
+(if (~iso memory*.1 4)
+  (prn "F - fork can take a space of global variables to access"))
+
 ; The scheduler needs to keep track of the call stack for each routine.
 ; Eventually we'll want to save this information in mu's address space itself,
 ; along with the types array, the magic buffers for args and oargs, and so on.
@@ -2920,7 +2937,7 @@
   '((function f1 [
       (default-scope:scope-address <- new scope:literal 30:literal)
       (chan:channel-address <- init-channel 3:literal)
-      (fork f2:fn chan:channel-address)
+      (fork f2:fn nil:literal nil:literal chan:channel-address)
       (1:tagged-value/raw <- read chan:channel-address)  ; output
      ])
     (function f2 [