4 #include "atomic_compat.h"
6 #define SYNC_QUEUE_BUFSIZE 16
13 head_node = tail_node = new node();
14 head.store(head_node->values);
15 tail.store(tail_node->values);
30 return head.load() == tail.load();
33 // size() is not not thread-safe
37 if (head_node == tail_node) {
38 count = tail.load() - head.load();
41 (head_node->values + (SYNC_QUEUE_BUFSIZE - 1)) - head.load();
42 for (node* n = head_node->next; n != tail_node; n = n->next)
43 count += SYNC_QUEUE_BUFSIZE;
44 count += tail.load() - tail_node->values;
49 bool push(const T& val)
51 T* old_tail = tail.load();
53 if (old_tail == tail_node->values + (SYNC_QUEUE_BUFSIZE - 1)) {
54 tail_node->next = new node();
55 tail_node = tail_node->next;
56 new_tail = tail_node->values;
58 new_tail = old_tail + 1;
62 return (old_tail == head.load());
67 T* old_head = head.load();
68 if (old_head == tail.load()) // empty?
72 if (old_head == head_node->values + (SYNC_QUEUE_BUFSIZE - 1)) {
73 node* old_head_node = head_node;
74 head_node = head_node->next;
76 new_head = head_node->values;
78 new_head = old_head + 1;
87 node(): next(NULL) { }
88 T values[SYNC_QUEUE_BUFSIZE];
98 #endif // !SYNC_QUEUE_H