Atomic 0.0.1 - An atomic reference for Ruby

atomic: An atomic reference implementation for JRuby and green or
GIL-threaded
Ruby implementations (MRI 1.8/1.9, Rubinius)

== Summary ==

This library provides:

  • an Atomic class that guarantees atomic updates to its contained value

The Atomic class provides accessors for the contained “value” plus two
update
methods:

  • update will run the provided block, passing the current value and
    replacing
    it with the block result iff the value has not been changed in the
    mean time.
    It may run the block repeatedly if there are other concurrent updates
    in
    progress.
  • try_update will run the provided block, passing the current value and
    replacing it with the block result. If the value changes before the
    update
    can happen, it will throw Atomic::ConcurrentUpdateError.

The atomic repository is at GitHub - headius/ruby-atomic: Atomic reference implementation for Ruby.

== Usage ==

gem install atomic

require ‘atomic’

my_atomic = Atomic.new(0)
my_atomic.update {|v| v + 1}
begin
my_atomic.try_update {|v| v + 1}
rescue Atomic::ConcurrentUpdateError => cue

deal with it (retry, propagate, etc)

end

And just pushed a minor update in 0.0.2 :slight_smile:

On Tue, Jun 8, 2010 at 12:06 AM, Charles Oliver N.

On Tue, Jun 8, 2010 at 6:06 AM, Charles Oliver N.
[email protected] wrote:
What a great idea!
Continue like that and we will be able to program Clojure with Ruby
Syntax.
Seriously this is a great concept, as Clojure is a great language. I
guess that following Ola’s ideas about polyglot programming Ruby and
Clojure would indeed make a beautiful couple.
Many kudos!

Cheers
Robert

It’s possible to do some of Clojure’s magic right now with my “Cloby”
library, which adds a Clojure::Object supertype you can use to get
transactional semantics for Ruby instance variables:

require 'clojure'

class MyClojureObj < Clojure::Object
def initialize
dosync { @foo = ‘foo’ }
end

attr_accessor :foo
end

obj = MyClojureObj.new
puts "obj.foo = " + obj.foo

begin
puts “Setting obj.foo to ‘bar’”
obj.foo = ‘bar’
rescue ConcurrencyError
puts “Oops, need a transaction”
end

puts “Trying again with a transaction”
dosync { obj.foo = ‘bar’ }
puts “Success”

puts "obj.foo = " + obj.foo

This is in the “cloby” library I’ve never released, at
GitHub - headius/cloby: A Clojure Ref/STM plugin for JRuby.

On Tue, Jun 8, 2010 at 2:21 PM, Charles Oliver N.
[email protected] wrote:

This is in the “cloby” library I’ve never released, at
GitHub - headius/cloby: A Clojure Ref/STM plugin for JRuby.
Just too good to be true :stuck_out_tongue:
Thx for your work.
Cheers
R.

And now a 0.0.3 that adds “swap”, fixes “update” return value to be
the new value, and uses a small extension for JRuby for perf and to
preserve value identity.

On Tue, Jun 8, 2010 at 4:29 AM, Charles Oliver N.