Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / protocols / ace / TMCast / MTQueue.hpp
blob464d720e3296a348cf99c8b59abc1ed99f69b8ff
1 // author : Boris Kolpackov <boris@dre.vanderbilt.edu>
3 #ifndef TMCAST_MT_QUEUE_HPP
4 #define TMCAST_MT_QUEUE_HPP
6 #include "ace/Unbounded_Set.h"
7 #include "ace/Unbounded_Queue.h"
8 #include "ace/os_include/sys/os_types.h"
9 #include "ace/Condition_T.h"
10 #include <memory>
12 namespace ACE_TMCast
14 template <typename T,
15 typename M,
16 typename C,
17 typename Q = ACE_Unbounded_Queue<T> >
18 class MTQueue
20 public:
21 typedef T ElementType;
22 typedef M MutexType;
23 typedef C ConditionalType;
24 typedef Q QueueType;
26 public:
28 MTQueue ()
29 : mutexp_ (new MutexType),
30 mutex_ (*mutexp_),
31 queue_ (),
32 signal_ (false)
36 MTQueue (MutexType& mutex)
37 : mutexp_ (),
38 mutex_ (mutex),
39 queue_ (),
40 signal_ (false)
44 public:
45 bool
46 empty () const
48 return queue_.is_empty ();
51 size_t
52 size () const
54 return queue_.size ();
57 // typedef typename QueueType::Empty Empty;
59 class Empty {};
62 front ()
64 ACE_Unbounded_Queue_Iterator<T> f (queue_);
65 T* tmp;
66 if (!f.next (tmp)) throw Empty ();
68 return *tmp;
72 T const&
73 front () const
75 ACE_Unbounded_Queue_Const_Iterator<T> f (queue_);
76 T* tmp;
77 if (!f.next (tmp)) throw Empty ();
79 return *tmp;
84 back ()
86 return queue_.back ();
90 T const&
91 back () const
93 return queue_.back ();
97 void
98 push (T const& t)
100 signal_ = empty ();
101 queue_.enqueue_tail (t);
104 void
105 pop ()
107 T junk;
108 queue_.dequeue_head (junk);
111 public:
112 void
113 lock () const
115 mutex_.acquire ();
118 void
119 unlock () const
121 if (signal_)
123 signal_ = false;
125 for (ConditionalSetConstIterator_ i (cond_set_);
126 !i.done ();
127 i.advance ())
129 ConditionalType** c = 0;
131 i.next (c);
133 (*c)->signal ();
137 mutex_.release ();
140 void
141 subscribe (ConditionalType& c)
143 //@@ should check for duplicates
145 cond_set_.insert (&c);
148 void
149 unsubscribe (ConditionalType& c)
151 //@@ should check for absence
153 cond_set_.remove (&c);
156 private:
157 std::unique_ptr<MutexType> mutexp_;
158 MutexType& mutex_;
159 QueueType queue_;
161 typedef
162 ACE_Unbounded_Set<ConditionalType*>
163 ConditionalSet_;
165 typedef
166 ACE_Unbounded_Set_Const_Iterator<ConditionalType*>
167 ConditionalSetConstIterator_;
169 ConditionalSet_ cond_set_;
171 mutable bool signal_;
175 #endif // TMCAST_MT_QUEUE_HPP