tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
queues.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 /* queues.hpp
12  *
13  * Sequential and concurrent queues.
14  * Element type should always be (the same size as) a pointer.
15  */
16 
17 #ifndef QUEUES_HPP__
18 #define QUEUES_HPP__
19 
20 #include <cstdlib>
21 #include <cassert>
22 #include <queue>
23 using std::queue;
24 
25 #include "common.hpp"
26 #include "counted_ptr.hpp"
27 
28 template<typename T>
29 class simple_queue {
30  public:
31  virtual void enqueue(const T item, const int tid) = 0;
32  virtual T dequeue(const int tid) = 0;
33  virtual ~simple_queue() {}
34 };
35 
36 template<typename T>
37 class sequential_queue : public simple_queue<T> {
38  private:
40  public:
41  virtual void enqueue(const T item, const int) {
42  assert(item != 0);
43  queue_impl.push(item);
44  }
45  virtual T dequeue(int) {
46  if (queue_impl.empty()) return T(0);
47  T rtn = queue_impl.front();
48  queue_impl.pop();
49  return rtn;
50  }
51  virtual ~sequential_queue() {}
52 };
53 
54 // Class exists only as a (non-templated) base for concurrent_queue<T>
55 //
56 template<typename T>
57 class MS_queue {
60  protected:
61  void enqueue(T item, const int tid);
62  T dequeue(const int tid);
63  MS_queue(const int tid);
64  virtual ~MS_queue() { assert(false); }
65  // Destruction of concurrent queue not currently supported.
66 };
67 
68 template<typename T>
69 class concurrent_queue : public simple_queue<T>, private MS_queue<T>
70 {
72  // no implementation; forbids passing by value
74  // no implementation; forbids copying
75 
76  public:
77  virtual void enqueue(T item, const int tid) {
78  MS_queue<T>::enqueue(item, tid);
79  }
80  virtual T dequeue(const int tid) {
81  return MS_queue<T>::dequeue(tid);
82  }
83 
84  // make sure that the queue is cache-line aligned
85  static void* operator new(const size_t size) {
86  return memalign(CACHELINE_BYTES, size);
87  }
88  // the C++ runtime's delete operator should do this the same way,
89  // but just in case...
90  static void operator delete(void *ptr) {free(ptr);}
91 
92  concurrent_queue(const int tid) : MS_queue<T>(tid) {
93  assert(sizeof(T) == sizeof(void*));
94  }
95  virtual ~concurrent_queue() { }
96 };
97 
98 #endif // QUEUES_HPP__
Definition: queues.hpp:37
void *volatile ptr
Definition: counted_ptr.hpp:57
void enqueue(T item, const int tid)
Definition: queues.cpp:254
MS_queue(const int tid)
Definition: queues.cpp:232
counted_ptr tail
Definition: queues.hpp:59
Definition: counted_ptr.hpp:20
virtual void enqueue(const T item, const int tid)=0
virtual T dequeue(const int tid)=0
concurrent_queue(const int tid)
Definition: queues.hpp:92
virtual ~concurrent_queue()
Definition: queues.hpp:95
long push
Definition: queue.c:83
#define CACHELINE_BYTES
Definition: platform.hpp:46
counted_ptr head
Definition: queues.hpp:58
virtual T dequeue(const int tid)
Definition: queues.hpp:80
long pop
Definition: queue.c:82
T dequeue(const int tid)
Definition: queues.cpp:293
Definition: queues.hpp:29
queue< T > queue_impl
Definition: queues.hpp:39
virtual T dequeue(int)
Definition: queues.hpp:45
concurrent_queue & operator=(const concurrent_queue &)
virtual void enqueue(const T item, const int)
Definition: queues.hpp:41
virtual ~sequential_queue()
Definition: queues.hpp:51
Definition: queues.hpp:69
concurrent_queue(const concurrent_queue &)
virtual ~simple_queue()
Definition: queues.hpp:33
virtual ~MS_queue()
Definition: queues.hpp:64
virtual void enqueue(T item, const int tid)
Definition: queues.hpp:77
Definition: queues.hpp:57