spsc-queue: fixup atomics, mutexes, threads
authorBrian Norris <banorris@uci.edu>
Thu, 11 Oct 2012 01:03:37 +0000 (18:03 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 11 Oct 2012 01:04:30 +0000 (18:04 -0700)
spsc-queue/eventcount.h
spsc-queue/queue.h
spsc-queue/spsc-queue.cc

index f64946a..bf1f511 100644 (file)
@@ -1,12 +1,14 @@
 #include <unrelacy.h>
+#include <atomic>
+#include <mutex>
 
 class eventcount
 {
 public:
-       eventcount()
-               : count(0)
-               , waiters(0)
-       {}
+       eventcount() : waiters(0)
+       {
+               count = 0;
+       }
 
        void signal_relaxed()
        {
@@ -46,7 +48,7 @@ std::memory_order_seq_cst);
 private:
        std::atomic<unsigned> count;
        rl::var<unsigned> waiters;
-       mutex guard;
+       std::mutex guard;
        condition_variable_any cv;
 
        void signal_impl(unsigned cmp)
@@ -54,7 +56,7 @@ private:
                if (cmp & 0x80000000)
                {
                        guard.lock($);
-                       while (false == count.compare_swap(cmp,
+                       while (false == count.compare_exchange_weak(cmp,
                                (cmp + 1) & 0x7FFFFFFF, std::memory_order_relaxed));
                        unsigned w = waiters($);
                        waiters = 0;
index 7a6f29e..3167e22 100644 (file)
@@ -1,4 +1,5 @@
 #include <unrelacy.h>
+#include <atomic>
 
 #include "eventcount.h"
 
@@ -51,9 +52,10 @@ private:
                rl::var<T> data;
 
                node(T data = T())
-                       : next(0)
-                       , data(data)
-               {}
+                       : data(data)
+               {
+                       next = 0;
+               }
        };
 
        rl::var<node*> head;
index a81a1d0..ef7b026 100644 (file)
@@ -1,8 +1,8 @@
+#include <threads.h>
+
 #include "queue.h"
 
-struct spsc_queue_test : rl::test_suite<spsc_queue_test, 2>
-{
-       spsc_queue<int> q;
+spsc_queue<int> q;
 
        void thread(unsigned thread_index)
        {
@@ -16,9 +16,13 @@ struct spsc_queue_test : rl::test_suite<spsc_queue_test, 2>
                        RL_ASSERT(11 == d);
                }
        }
-};
 
 int main()
 {
-       rl::simulate<spsc_queue_test>();
+       thrd_t A, B;
+       thrd_create(&A, (thrd_start_t)&thread, (void *)0);
+       thrd_create(&B, (thrd_start_t)&thread, (void *)1);
+       thrd_join(A);
+       thrd_join(B);
+       return 0;
 }