Moritz Heidkamp committed 34f017a

properly gc forked channels

Comments (0)

Files changed (2)

  on-channel-receive on-channel-close on-channel-drain
  channel-messages channel-empty?
  channel-close! channel-closed? channel-drained?
- make-receiving-channel
+ channel-forks make-receiving-channel
  channel-fork! channel-fold! channel-map! channel-filter!)
 (import chicken scheme)
-(use data-structures miscmacros srfi-18)
+(use data-structures srfi-18 
+     (only srfi-1 filter)
+     (only miscmacros push!)
+     (only lolevel make-weak-locative locative->object))
 (define-record channel
   (setter closed?)
              (mutex (channel-mutex channel)))
          (mutex-lock! mutex)
          (queue-add-list! (channel-queue channel) messages)
-         (for-each (lambda (fork)
-                     (apply channel-enqueue! fork messages))
-                   (channel-forks channel))
+         (set! (channel-forks channel)
+               (filter (lambda (fork)
+                         (and-let* ((fork (locative->object fork)))
+                           (apply channel-enqueue! fork messages)))
+                       (channel-forks channel)))
          (mutex-unlock! mutex)
          (channel-flush! channel)
   (let ((fork  (make-channel))
         (mutex (channel-mutex channel)))
     (mutex-lock! mutex)
-    (push! fork (channel-forks channel))
+    (push! (make-weak-locative fork) (channel-forks channel))
     (apply channel-enqueue! fork (channel-messages channel))
     (mutex-unlock! mutex)
   (channel-enqueue! c1 3)
   (channel-enqueue! c2 4)
   (test (r1) '(1 2 3))
-  (test (r2) '(1 2 3 4)))
+  (test (r2) '(1 2 3 4))
+  (test-group "gc"
+    (define c (make-channel))
+    (define f (channel-fork! c))
+    (test 1 (length (channel-forks c)))
+    (set! f #f)
+    (gc #t)
+    (test 1 (length (channel-forks c)))
+    (channel-enqueue! c 'x)
+    (test 0 (length (channel-forks c)))))
 (test-group "on-channel-close, on-channel-drain"
   (define close (make-receiver))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.