[ANN] atomic 0.0.1 - An atomic reference for Ruby

  • Thread starter Charles Oliver Nutter
  • Start date
C

Charles Oliver Nutter

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 http://github.com/headius/ruby-atomic.

== 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
 
R

Robert Dober

On Tue, Jun 8, 2010 at 6:06 AM, Charles Oliver Nutter
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
 
C

Charles Oliver Nutter

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:

<code>
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
</code>

This is in the "cloby" library I've never released, at
http://github.com/headius/cloby.
 
C

Charles Oliver Nutter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top