How really commute works. (commute IDENTITY FUNCTION &values)

the Clojure Documentation says:

Usage: (commute ref fun & args) Must be called in a transaction. Sets the in-transaction-value of ref to: (apply fun in-transaction-value-of-ref args) and returns the in-transaction-value of ref. At the commit point of the transaction, sets the value of ref to be: (apply fun most-recently-committed-value-of-ref args)

So the commute form is performed in two phases.

is the second phase atomic ?(apply fun most-recently-committed-value-of-ref args)

if not, what happen in this example: 2 Threads ( T1 and T2 ).

Both will increment (commutative function) the same identity.

IDENTITY: (def i (ref 0 ) (dosync (commute inc i ) )

T1 in the first step of the commute call inc with ref i = 0 ( in transaction value = 1 )

T1 stop

T2 in the first step of the commute call inc with ref i= 0 ( in transaction value = 1 )

T2 stop

T1 in the second step call again inc with the recent commit value i = 0, the inc function return but before update the ref ( i ) T1 stop

T2 in the second step call again inc with the recent commit value i = 0 and update the reference

T1 start again and update the reference with the inc returned value = 1

This is a race condition problem ? how clojure avoid this ? if the second phase be atomic, this will no happen.

Thanks in advance

UPDATE: if i understand correctly the the last phase of the commute operations ( commit point ) is synchronized"LOCK commute fun UNLOCK**" ?