[ANN] atomic 0.0.1 - An atomic reference for Ruby

Discussion in 'Ruby' started by Charles Oliver Nutter, Jun 8, 2010.

  1. 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
    Charles Oliver Nutter, Jun 8, 2010
    #1
    1. Advertising

  2. And just pushed a minor update in 0.0.2 :)

    On Tue, Jun 8, 2010 at 12:06 AM, Charles Oliver Nutter
    <> wrote:
    > atomic: An atomic reference implementation for JRuby and green or GIL-thr=

    eaded
    > Ruby implementations (MRI 1.8/1.9, Rubinius)
    >
    > =3D=3D Summary =3D=3D
    >
    > 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 up=

    date
    > methods:
    >
    > * update will run the provided block, passing the current value and repla=

    cing
    > =C2=A0it with the block result iff the value has not been changed in the =

    mean time.
    > =C2=A0It may run the block repeatedly if there are other concurrent updat=

    es in
    > =C2=A0progress.
    > * try_update will run the provided block, passing the current value and
    > =C2=A0replacing it with the block result. If the value changes before the=

    update
    > =C2=A0can happen, it will throw Atomic::ConcurrentUpdateError.
    >
    > The atomic repository is at http://github.com/headius/ruby-atomic.
    >
    > =3D=3D Usage =3D=3D
    >
    > =C2=A0# gem install atomic
    > require 'atomic'
    >
    > my_atomic =3D Atomic.new(0)
    > my_atomic.update {|v| v + 1}
    > begin
    > =C2=A0my_atomic.try_update {|v| v + 1}
    > rescue Atomic::ConcurrentUpdateError =3D> cue
    > =C2=A0# deal with it (retry, propagate, etc)
    > end
    >
    >
    Charles Oliver Nutter, Jun 8, 2010
    #2
    1. Advertising

  3. Charles Oliver Nutter

    Robert Dober Guest

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

    --
    The best way to predict the future is to invent it.
    -- Alan Kay
    Robert Dober, Jun 8, 2010
    #3
  4. 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.

    On Tue, Jun 8, 2010 at 8:08 AM, Robert Dober <> wrote:
    > On Tue, Jun 8, 2010 at 6:06 AM, Charles Oliver Nutter
    > <> 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
    >
    > --
    > The best way to predict the future is to invent it.
    > -- Alan Kay
    >
    >
    Charles Oliver Nutter, Jun 8, 2010
    #4
  5. 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 Nutter
    <> wrote:
    > And just pushed a minor update in 0.0.2 :)
    >
    > On Tue, Jun 8, 2010 at 12:06 AM, Charles Oliver Nutter
    > <> wrote:
    >> atomic: An atomic reference implementation for JRuby and green or GIL-th=

    readed
    >> Ruby implementations (MRI 1.8/1.9, Rubinius)
    >>
    >> =3D=3D Summary =3D=3D
    >>
    >> 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 u=

    pdate
    >> methods:
    >>
    >> * update will run the provided block, passing the current value and repl=

    acing
    >> =C2=A0it with the block result iff the value has not been changed in the=

    mean time.
    >> =C2=A0It may run the block repeatedly if there are other concurrent upda=

    tes in
    >> =C2=A0progress.
    >> * try_update will run the provided block, passing the current value and
    >> =C2=A0replacing it with the block result. If the value changes before th=

    e update
    >> =C2=A0can happen, it will throw Atomic::ConcurrentUpdateError.
    >>
    >> The atomic repository is at http://github.com/headius/ruby-atomic.
    >>
    >> =3D=3D Usage =3D=3D
    >>
    >> =C2=A0# gem install atomic
    >> require 'atomic'
    >>
    >> my_atomic =3D Atomic.new(0)
    >> my_atomic.update {|v| v + 1}
    >> begin
    >> =C2=A0my_atomic.try_update {|v| v + 1}
    >> rescue Atomic::ConcurrentUpdateError =3D> cue
    >> =C2=A0# deal with it (retry, propagate, etc)
    >> end
    >>
    >>

    >
    Charles Oliver Nutter, Jun 8, 2010
    #5
  6. Charles Oliver Nutter

    Robert Dober Guest

    On Tue, Jun 8, 2010 at 2:21 PM, Charles Oliver Nutter
    <> wrote:
    > This is in the "cloby" library I've never released, at
    > http://github.com/headius/cloby.

    Just too good to be true :p
    Thx for your work.
    Cheers
    R.

    --
    The best way to predict the future is to invent it.
    -- Alan Kay
    Robert Dober, Jun 8, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravi Sandhu

    Atomic Clock - Applet

    Ravi Sandhu, Feb 17, 2004, in forum: Java
    Replies:
    2
    Views:
    2,311
  2. Jacques Vidal
    Replies:
    1
    Views:
    365
    Bryce
    Jul 21, 2004
  3. Chris Thomasson
    Replies:
    0
    Views:
    638
    Chris Thomasson
    Jan 29, 2007
  4. Dmitriy V'jukov
    Replies:
    11
    Views:
    606
  5. why the lucky stiff

    [ANN] A Textile Reference (and Quick Reference)

    why the lucky stiff, Apr 21, 2004, in forum: Ruby
    Replies:
    0
    Views:
    136
    why the lucky stiff
    Apr 21, 2004
Loading...

Share This Page