
#include "builtin/class.hpp"
#include "builtin/atomic.hpp"
#include "ontology.hpp"

namespace rubinius {
  void AtomicReference::init(STATE) {
    GO(atomic_ref).set(ontology::new_class(state,
          "AtomicReference", G(object), G(rubinius)));

    G(atomic_ref)->set_object_type(state, AtomicReferenceType);
  }

  AtomicReference* AtomicReference::allocate(STATE) {
    return state->new_object<AtomicReference>(G(atomic_ref));
  }

  Object* AtomicReference::compare_and_set(STATE, Object* old, Object* new_) {
    Object** pp = &value_;

    if (atomic::compare_and_swap((void**)pp, old, new_)) {
      if(mature_object_p()) this->write_barrier(state, new_);
      return cTrue;
    } else {
      return cFalse;
    }
  }
}


#include "builtin/class.hpp"
#include "builtin/atomic.hpp"
#include "ontology.hpp"

namespace rubinius {
  void AtomicReference::init(STATE) {
    GO(atomic_ref).set(ontology::new_class(state,
          "AtomicReference", G(object), G(rubinius)));

    G(atomic_ref)->set_object_type(state, AtomicReferenceType);
  }

  AtomicReference* AtomicReference::allocate(STATE) {
    return state->new_object<AtomicReference>(G(atomic_ref));
  }

  Object* AtomicReference::compare_and_set(STATE, Object* old, Object* new_) {
    Object** pp = &value_;
    return atomic::compare_and_swap((void**)pp, old, new_) ? cTrue : cFalse;
  }
}

--- 
+++ 
@@ -17,6 +17,12 @@
 
   Object* AtomicReference::compare_and_set(STATE, Object* old, Object* new_) {
     Object** pp = &value_;
-    return atomic::compare_and_swap((void**)pp, old, new_) ? cTrue : cFalse;
+
+    if (atomic::compare_and_swap((void**)pp, old, new_)) {
+      if(mature_object_p()) this->write_barrier(state, new_);
+      return cTrue;
+    } else {
+      return cFalse;
+    }
   }
 }
Fix GC-related SEGV when heavily using AtomicReference
