This is the mail archive of the kawa@sourceware.org mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: proposed support for lazy values


On 06/03/2011 03:25 PM, Jamison Hope wrote:
So the mechanism for creating an object of type lazy[T] would be to call
(delay EXP), where EXP is an expression that would evaluate to an instance
of T?

So for instance:
(define the-answer ::lazy[integer] (delay (* 2 3 7)))
;;; Right now, the multiplication hasn't happened yet
(+ the-answer 0) ;; <- implicit force here as context requires an integer

Correct.


By the way, I notice that force() in kawa.lang.Promise checks result==null
twice, presumably as a lockless mechanism to make sure the result is only
memoized once in case two threads both force it simultaneously. It seems
like
this could still lead to trouble, if the promised computation has side
effects.
Even though the result seen by all threads will be the same (usually,
though I
think you'd need an AtomicReference and compareAndSet in there to be
absolutely
sure), there's still nothing preventing the promise from being evaluated N
times:

#|kawa:14|# (define my-promise (delay (begin (display "Ding!") (newline))))
#|kawa:15|# (do ((i 0 (+ i 1))) ((= i 10)) ((java.lang.Thread (runnable
(lambda () (force my-promise)))):start))
Ding!
Ding!
Ding!
Ding!
Ding!
Ding!


That may be an acceptable trade-off to avoid using a synchronized block, but it seems like something that should be documented.

Could you try the attached patch? A review would be appreciated as well. -- --Per Bothner per@bothner.com http://per.bothner.com/

Attachment: Promise.patch
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]