mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-11 23:07:36 +02:00
WIP
This commit is contained in:
parent
827dd711c1
commit
9498856c49
1 changed files with 52 additions and 21 deletions
|
@ -3,15 +3,26 @@
|
||||||
;; https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/future?
|
;; https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/future?
|
||||||
;; https://purelyfunctional.tv/guide/clojure-concurrency/#future
|
;; https://purelyfunctional.tv/guide/clojure-concurrency/#future
|
||||||
|
|
||||||
(import (scheme base)
|
(define-library (futures)
|
||||||
(scheme write)
|
(import (scheme base)
|
||||||
(cyclone concurrency)
|
(scheme write)
|
||||||
(srfi 18)
|
(cyclone concurrency)
|
||||||
)
|
(srfi 18)
|
||||||
|
)
|
||||||
(define *future-sym* (string->symbol " future "))
|
(export
|
||||||
(define (future? obj)
|
future?
|
||||||
(and (vector? obj) (eq? (vector-ref obj 0) *future-sym*)))
|
future-call
|
||||||
|
future-deref
|
||||||
|
)
|
||||||
|
;(define *future-sym* (string->symbol " future "))
|
||||||
|
;(define (future? obj)
|
||||||
|
; (and (vector? obj) (eq? (vector-ref obj 0) *future-sym*)))
|
||||||
|
(define-record-type <future>
|
||||||
|
(make-future done result lock)
|
||||||
|
future?
|
||||||
|
(done get-done set-done!)
|
||||||
|
(result get-result set-result!)
|
||||||
|
(lock get-lock set-lock!))
|
||||||
|
|
||||||
|
|
||||||
;; TODO: macro (future expr ...)
|
;; TODO: macro (future expr ...)
|
||||||
|
@ -24,18 +35,38 @@
|
||||||
; of deref with timeout is used. See also - realized?.
|
; of deref with timeout is used. See also - realized?.
|
||||||
|
|
||||||
(define (future-call thunk)
|
(define (future-call thunk)
|
||||||
(let ((ftr (vector *future-sym* 'todo)))
|
(let* (
|
||||||
;; TODO: setup and call the thread here
|
(lock (make-mutex))
|
||||||
|
(ftr (make-future #f #f lock)
|
||||||
;; Sketching out what is needed:
|
; (vector
|
||||||
;;(define (sum-entry-pt)
|
; *future-sym* ;; Type indicator
|
||||||
;; (sum-loop (* 100 100 100)))
|
; #f ;; Done?
|
||||||
;;
|
; #f ;; Result
|
||||||
;;;; Thread - Do something, then let main thread know when we are done
|
; lock)
|
||||||
;;(define t9 (make-thread sum-entry-pt))
|
)
|
||||||
;;(thread-start! t1)
|
(tfnc (lambda ()
|
||||||
|
(mutex-lock! lock)
|
||||||
|
(let ((result (thunk))) ;; TODO: Catch exceptions (?)
|
||||||
|
(set-done! #t)
|
||||||
|
(set-result! result)
|
||||||
|
;(vector-set! ftr 1 #t) ;; Done
|
||||||
|
;(vector-set! ftr 2 result)
|
||||||
|
(mutex-unlock! lock)
|
||||||
|
)))
|
||||||
|
(t (make-thread tfnc))
|
||||||
|
)
|
||||||
|
(thread-start! t)
|
||||||
ftr))
|
ftr))
|
||||||
|
|
||||||
|
|
||||||
TODO: custom deref but eventually need to fold that functionality back into the main one
|
;;TODO: custom deref but eventually need to fold this functionality back into the main one
|
||||||
|
(define (future-deref ftr)
|
||||||
|
(when (not (future? ftr))
|
||||||
|
(error "Expected future but received" ftr))
|
||||||
|
(let ((result #f))
|
||||||
|
(mutex-lock! (get-lock ftr)) ;(vector-ref ftr 3))
|
||||||
|
(set! result (get-result ftr)) ;(vector-ref ftr 2))
|
||||||
|
(mutex-unlock! (get-lock ftr)) ;(vector-ref ftr 3))
|
||||||
|
result))
|
||||||
|
|
||||||
|
))
|
||||||
|
|
Loading…
Add table
Reference in a new issue