async   xtlang


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/libs/core/xthread.xtm

Implementation

;; async returns a 'future'
;;
;; see examples/core/xthread.xtm for usage
;;
;; async -> 1 arg
;;
(bind-func async:[[!a]*,[!a,!b]*,!b]*
  (lambda (f arg)
    ;; (println "zone in:" (peek_zone))
    (let ((z1 (push_new_zone 4096))
          (res (convert null))
          (running:i1 #t)
          (finished:i1 #f)
          (ff (lambda ()
                (push_zone z1)
                (set! res (f arg))
                (set! finished #t)
                (pop_zone)
                void))
          (thread null))
      (pop_zone) ;; pop z1
      (set! thread (thread_fork (get_native_fptr thread_native_call) (cast ff i8*)))
      (lambda ()
        (thread_join thread)
        (let ((r2 (zcopy res z1 (llvm_peek_zone_stack))))
          (llvm_zone_destroy z1)
          (thread_destroy thread)
          r2)))))


Back to Index

Similar Entries