Hello Clojure developers

I am currently doing my masters thesis about Clojure as a paraller 
programming language. I have studied Clojures STM from the source and I 
have a next question about the implementation. If this is a wrong place to 
ask this question I apologize beforehand.

Read operations in transaction can fault if the refs history queue doesnt 
have a val which is older than running transaction. That can happen if 
other transactions commit between the start of the transaction and 
dereferencing.

LockingTransaction.java seems to have ref cache which is updated in write 
operations (alter).
line 107: final HashMap<Ref, Object> vals = new HashMap<Ref, Object>();

This is updated when calling doSet method (lines 424 - 436).

LockingTransaction doGet-method (lines 397-422) seem to work as next:
- Check vals-map with ref as a key, return if found
- check if ref is unbound, if so throw exception 
- check if refs tvals list have value which is older than running 
transaction. If found return that value else retry transaction.

According the code succesful read from the refs history is not cached.

This means that if transaction have two same read operations there is a 
possibility that latter read may fault.

I have tested that with next program.

(def a (ref 1))
(ref-max-history a 0)
(ref-min-history a 0)

(def counter(atom 0))

(defn readtrans[]
  (
    dosync
            (swap! counter inc)
            (deref a)
            (Thread/sleep 2000)
            (deref a)
            (swap! counter inc))
)

(defn writetrans[]
 (
   dosync           
           (Thread/sleep 100)
           (ref-set a 2))
) 

(do
  (def readfuture (future (readtrans)))
  (def writefuture (future (writetrans))))

Atom counter is 3 after running the code. 

Code works as:
- readfuture increments counter and reads ref
- readfuture is suspended
- writefuture alters ref 
- readfuture wakes and tries to read the value again -> fault and retry
- readfuture runs again and increments counter twice.

This could be averted if the reads were cached as well as writes. Is there 
some reason why reads are not cached like writes?

Yours,
Heikki Hämäläinen 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to