shhyou / ypsilon-fork

Automatically exported from code.google.com/p/ypsilon
Other
0 stars 0 forks source link

[request] Serialize closures #97

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to implement a threadpool library to more efficiently make use
of multiple threads. However, the runtime does not appear to support
serialization of closures. If it is possible to implement, this would be a
very useful feature to have. 

Using ypsilon 0.9.6-update3 on cygwin. 

> (import (concurrent))
> (define q (make-mailbox))
> (send q (lambda () (display "Passed a function.\n")))

error in shared-queue-push!: encountered non-serializable object #<closure
0x13e1040>

irritants:
  (#<shared-queue 0/1 0x13d5880> #<closure 0x13e1040>)

backtrace:
  0  (send q |.L0|)
  ..."/dev/stdin" line 1
> 

Original issue reported on code.google.com by sura...@gmail.com on 2 May 2009 at 2:25

GoogleCodeExporter commented 9 years ago
Thank you for your suggestion!
I will think about how to implement it.
-- fujita

Original comment by y.fujita...@gmail.com on 6 May 2009 at 2:33

GoogleCodeExporter commented 9 years ago
I have implemented closure serialization in revision 484. (experimental)
Your comments are very welcome. Thank you!

-- fujita                                      

======

The closures they only reference top level variables and its local variables are
serializable.

> (import (ypsilon concurrent))
> (define mbox-req (make-mailbox))
> (define mbox-ans (make-mailbox))
> (spawn (lambda () (let ((proc (recv mbox-req))) (proc "hello world"))))
1
> (send mbox-req
        (lambda (m1)
          (let ((m2 (string-upcase m1))
                (m3 (string-titlecase m1)))
            (send mbox-ans (list m2 m3))))
        100)
#t
> (recv mbox-ans 100)
("HELLO WORLD" "Hello World")

====

The closures they reference any free non-global variable(*1) are not 
serializable.

> (import (ypsilon concurrent))
> (define mbox-req (make-mailbox))
> (spawn (lambda () (let ((proc (recv mbox-req))) (proc "hello world"))))
1
> (let ((mbox-ans (make-mailbox))) ;; mbox-ans(*1)
    (send mbox-req
          (lambda (m1)
            (let ((m2 (string-upcase m1))
                  (m3 (string-titlecase m1)))
              (send mbox-ans (list m2 m3)))) ;; mbox-ans(*1)
          100))

error in shared-queue-push!: encountered non-serializable object, #<closure
0x776e1370> may have reference to free non-global variable

irritants:
  (#<shared-queue 0/1 0x77752000> #<closure 0x77753980> 100)

Original comment by y.fujita...@gmail.com on 30 May 2009 at 3:44