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 f64946a556138500880f12684e0321802accf86f..bf1f511a7fb3ec4249a2400b6c6ab8640448bacd 100644 (file)
@@ -1,12 +1,14 @@
 #include <unrelacy.h>
 #include <unrelacy.h>
+#include <atomic>
+#include <mutex>
 
 class eventcount
 {
 public:
 
 class eventcount
 {
 public:
-       eventcount()
-               : count(0)
-               , waiters(0)
-       {}
+       eventcount() : waiters(0)
+       {
+               count = 0;
+       }
 
        void signal_relaxed()
        {
 
        void signal_relaxed()
        {
@@ -46,7 +48,7 @@ std::memory_order_seq_cst);
 private:
        std::atomic<unsigned> count;
        rl::var<unsigned> waiters;
 private:
        std::atomic<unsigned> count;
        rl::var<unsigned> waiters;
-       mutex guard;
+       std::mutex guard;
        condition_variable_any cv;
 
        void signal_impl(unsigned cmp)
        condition_variable_any cv;
 
        void signal_impl(unsigned cmp)
@@ -54,7 +56,7 @@ private:
                if (cmp & 0x80000000)
                {
                        guard.lock($);
                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;
                                (cmp + 1) & 0x7FFFFFFF, std::memory_order_relaxed));
                        unsigned w = waiters($);
                        waiters = 0;
index 7a6f29e80c60bd6d9f9b0ef6e5571aae5501db4e..3167e22bf8e4f98d1d29129ea91dbbf933ef635a 100644 (file)
@@ -1,4 +1,5 @@
 #include <unrelacy.h>
 #include <unrelacy.h>
+#include <atomic>
 
 #include "eventcount.h"
 
 
 #include "eventcount.h"
 
@@ -51,9 +52,10 @@ private:
                rl::var<T> data;
 
                node(T data = T())
                rl::var<T> data;
 
                node(T data = T())
-                       : next(0)
-                       , data(data)
-               {}
+                       : data(data)
+               {
+                       next = 0;
+               }
        };
 
        rl::var<node*> head;
        };
 
        rl::var<node*> head;
index a81a1d0ce357e66c4f0b86bec5f6e6ca98b5ec66..ef7b02635d8f1e525d14191b9ba6b85b33fe7ec2 100644 (file)
@@ -1,8 +1,8 @@
+#include <threads.h>
+
 #include "queue.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)
        {
 
        void thread(unsigned thread_index)
        {
@@ -16,9 +16,13 @@ struct spsc_queue_test : rl::test_suite<spsc_queue_test, 2>
                        RL_ASSERT(11 == d);
                }
        }
                        RL_ASSERT(11 == d);
                }
        }
-};
 
 int main()
 {
 
 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;
 }
 }