cwidget  0.5.17
event_queue.h
1 // channel.h -*-c++-*-
2 //
3 // Copyright (C) 2005, 2007 Daniel Burrows
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; see the file COPYING. If not, write to
17 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 // Boston, MA 02111-1307, USA.
19 
20 #ifndef EVENT_QUEUE_H
21 #define EVENT_QUEUE_H
22 
23 #include "threads.h"
24 
25 #include <deque>
26 
27 namespace cwidget
28 {
29  namespace threads
30  {
45  template<typename T>
47  {
48  std::deque<T> q;
49 
50  condition c;
51  mutable mutex m;
52 
53  struct not_empty
54  {
55  const std::deque<T> &q;
56  public:
57  not_empty(const std::deque<T> &_q)
58  :q(_q)
59  {
60  }
61 
62  bool operator()() const
63  {
64  return !q.empty();
65  }
66  };
67 
68  event_queue(const event_queue &other);
69  event_queue &operator=(const event_queue &other);
70  public:
73  {
74  }
75 
76  ~event_queue()
77  {
78  }
79 
81  void put(const T &t)
82  {
83  mutex::lock l(m);
84 
85  q.push_back(t);
86  c.wake_one();
87  }
88 
90  T get()
91  {
92  mutex::lock l(m);
93 
94  c.wait(l, not_empty(q));
95  T rval = q.front();
96  q.pop_front();
97 
98  return rval;
99  }
100 
107  bool try_get(T &out)
108  {
109  mutex::lock l(m);
110 
111  if(q.empty())
112  return false;
113  else
114  {
115  out = q.front();
116  q.pop_front();
117  return true;
118  }
119  }
120 
124  bool timed_get(T &out, const timespec &until)
125  {
126  mutex::lock l(m);
127 
128  if(c.timed_wait(l, until, not_empty(q)))
129  {
130  out = q.front();
131  q.pop_front();
132  return true;
133  }
134  else
135  return false;
136  }
137 
139  bool empty() const
140  {
141  // Not sure the lock is required here, but it makes things a bit
142  // safer in case the STL is thread-unsafe in weird ways.
143  mutex::lock l(m);
144  bool rval = q.empty();
145  return rval;
146  }
147  };
148  }
149 }
150 
151 #endif
bool timed_wait(const Lock &l, const timespec &until)
Wait until either the condition is signalled or until the given time.
Definition: threads.h:546
A simple unbounded communications channel suitable for use as, eg, an event queue.
Definition: event_queue.h:46
bool timed_get(T &out, const timespec &until)
Retrieve a single value from the event queue, or fail if the time "until" is reached.
Definition: event_queue.h:124
bool empty() const
Return true if the event queue is currently empty.
Definition: event_queue.h:139
bool try_get(T &out)
Retrieve a single value from the event queue if the queue is non-empty.
Definition: event_queue.h:107
The namespace containing everything defined by cwidget.
Definition: columnify.cc:26
void put(const T &t)
Push the given value onto the event queue.
Definition: event_queue.h:81
event_queue()
Create an empty queue.
Definition: event_queue.h:72
Represents a lock on a mutex.
Definition: threads.h:347
Definition: threads.h:286
void wait(const Lock &l)
Wait with the given guard (should be a lock type that is a friend of this condition object)...
Definition: threads.h:502
A abstraction over conditions.
Definition: threads.h:469