Atomic updates: Difference between revisions

Refactored process creation code into a new function
(Added Mathematica)
(Refactored process creation code into a new function)
Line 2,702:
(de pickBucket ()
(db 'key '+Bucket (rand 1 *Buckets)) )
 
# Create process
(de process (QuadFunction)
(unless (fork)
(seed *Pid) # Ensure local random sequence
(loop
(let (B1 (pickBucket) B2 (pickBucket)) # Pick two buckets 'B1' and 'B2'
(commitunless 'upd)(== )B1 B2) ) ) # CloseFound two different transactionones?
(dbSync) # Atomic DB operation
(let (V1 (; B1 val) V2 (; B2 val)) # Get current values
(inc>QuadFunction B1 'val)V1 )B2 V2) )
(commit 'upd) ) ) ) ) ) # Close transaction
 
 
# First process
(process
(unless (fork)
(quote (B1 V1 B2 V2)
(seed *Pid) # Ensure local random sequence
(loopcond
((> V1 V2 V1)
(let (B1 (pickBucket) B2 (pickBucket)) # Pick two buckets 'B1' and 'B2'
(dbSync) (dec> B1 'val) # Make them closer to # Atomic DB operationequal
(let (V1 (;inc> B1B2 'val) V2 (; B2 val)) # Get current values
((> V2 (condV1)
((dec> V1B2 V2'val)
(decinc> B1 'val) ) ) ) # Make them closer to equal)
(inc> B2 'val) )
((> V2 V1)
(dec> B2 'val)
(inc> B1 'val) ) ) )
(commit 'upd) ) ) ) # Close transaction
 
# Second process
(process
(unless (fork)
(quote (B1 V1 B2 V2)
(seed *Pid) # Ensure local random sequence
(loopcond
((> V1 V2 V1 0)
(let (B1 (pickBucket) B2 (pickBucket)) # Pick two buckets 'B1' and 'B2'
(unless (==inc> B1 B2'val) # Found two differentRedistribute ones?them
(incdec> B2 'val) )
(dbSync) # Atomic DB operation
((> V2 V1 0)
(let (V1 (; B1 val) V2 (; B2 val)) # Get current values
(inc> B2 (cond'val)
(dec> B1 'val) ) ) ) ((> V1 V2 0)
(inc> B1 'val) # Redistribute them
(dec> B2 'val) )
((> V2 V1 0)
(inc> B2 'val)
(dec> B1 'val) ) ) )
(commit 'upd) ) ) ) ) # Close transaction
 
# Third process