SoDaRadio-5.0.3-master:8901fb5
MultiMBox.hxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef MULTI_MBOX_HDR
30 #define MULTI_MBOX_HDR
31 
32 #include <queue>
33 #include <vector>
34 #include <map>
35 #include <boost/thread.hpp>
36 #include <boost/thread/thread.hpp>
37 #include <boost/thread/mutex.hpp>
38 #include <boost/thread/condition.hpp>
39 
40 namespace SoDa {
41 
42  // base class for all messaging schemes.
43  class MBoxMessage {
44  public:
46  {
47  reader_count = 0;
48  }
49 
50  void setReaderCount(unsigned int rc)
51  {
52  boost::mutex::scoped_lock lock(rc_mutex);
53  reader_count = rc;
54  }
55 
56  bool readyToDie() { return reader_count == 1; }
57  bool decReaderCount() { return reader_count--; }
58 
59  bool free(std::queue<MBoxMessage *> & free_list) {
60  boost::mutex::scoped_lock lock(rc_mutex);
61  if(reader_count == 1) {
62  reader_count = 0;
63  free_list.push(this);
64  }
65  else {
66  reader_count--;
67  }
68  return true;
69  }
70 
71  // these are to detect "free" actions on the wrong mailbox.
72  void setMBoxTag(void * tag) { mbox_tag = tag; }
73  bool checkMBoxTag(void * tag) { return mbox_tag == tag; }
74 
75  private:
76  unsigned int reader_count;
77  boost::mutex rc_mutex;
78  void * mbox_tag;
79  };
80 
81  template <typename T> class MultiMBox {
82  public:
83  MultiMBox(bool _keep_freelist = true) {
84  subscriber_count = 0;
85  keep_freelist = _keep_freelist;
86  }
87 
88  int subscribe() {
89  int subscriber_id = subscriber_count;
90  subscriber_count++;
91  subscribers[subscriber_id] = new Subscriber;
92  return subscriber_id;
93  }
94 
95  int getSubscriberCount() { return subscriber_count; }
96 
97  void put(T * m) {
98  unsigned int i;
99  m->setReaderCount(subscriber_count);
100  m->setMBoxTag(this);
101  for(i = 0; i < subscriber_count; i++) {
102  Subscriber * s = subscribers[i];
103  boost::mutex::scoped_lock lock(s->postmutex);
104  s->posted_list.push(m);
105  s->post_count++;
106  s->postcond.notify_all();
107  }
108  }
109 
110  T * get(unsigned int subscriber_id) {
111  return getCommon(subscriber_id, false);
112  }
113 
114  T * getWait(unsigned int subscriber_id) {
115  return getCommon(subscriber_id, true);
116  }
117 
118  void free(T * m) {
119  if(m != NULL) {
120  if(keep_freelist && m->checkMBoxTag(this)) {
121  if(m->readyToDie()) {
122  boost::mutex::scoped_lock lock(free_lock);
123  m->free(free_list);
124  }
125  else m->decReaderCount();
126  }
127  else {
128  if(m->readyToDie()) delete m;
129  else m->decReaderCount();
130  }
131  }
132  }
133 
134  T * alloc()
135  {
136  T * ret = NULL;
137  boost::mutex::scoped_lock lock(free_lock);
138  if(!free_list.empty() && keep_freelist) {
139  ret = (T*) free_list.front();
140  free_list.pop();
141  }
142 
143  return ret;
144  }
145 
146  void addToPool(T * v) {
147  if(keep_freelist) {
148  boost::mutex::scoped_lock lock(free_lock);
149  free_list.push(v);
150  }
151  }
152 
153  unsigned int inFlightCount() {
154  // go through the subscribers and find the
155  // longest inflight list.
156  int max_len = 0;
157  int itercount = 0;
158  typename std::map<int, typename MultiMBox< T >::Subscriber * >::iterator sl;
159  for(sl = subscribers.begin();
160  sl != subscribers.end();
161  ++sl) {
162  Subscriber * s = sl->second;
163  {
164  boost::mutex::scoped_lock lock(s->postmutex);
165  int le = s->posted_list.size();
166  itercount++;
167  // std::cerr << "multimbox list len = " << le
168  // << " iter count = " << itercount
169  // << " posted count = " << s->post_count << std::endl;
170  if(le > max_len) max_len = le;
171  }
172  }
173 
174  return max_len;
175  }
176 
182  bool flush(unsigned int subscriber_id) {
183  T * dummy;
184  while((dummy = getCommon(subscriber_id, false)) != NULL) {
185  free(dummy);
186  }
187  return true;
188  }
189 
190 
191  private:
192  T * getCommon(unsigned int subscriber_id, bool wait)
193  {
194  if(subscriber_id >= subscriber_count) {
195  // someone is asking for a message from
196  // an unallocated subscriber id... throw
197  // an exception.
198  // or just return empty.
199  return NULL;
200  }
201 
202  Subscriber * s = subscribers[subscriber_id];
203  boost::mutex::scoped_lock lock(s->postmutex);
204  if(s->posted_list.empty()) {
205  if(!wait) {
206  return NULL;
207  }
208  else {
209  while(s->posted_list.empty()) {
210  s->postcond.wait(s->postmutex);
211  }
212  }
213  }
214 
215  // if we get here, then we have a message.
216  T * ret = NULL;
217  ret = s->posted_list.front();
218  s->posted_list.pop();
219  s->post_count--;
220  return ret;
221  }
222 
223  unsigned int subscriber_count;
225 
226  class Subscriber {
227  public:
228  Subscriber() { post_count = 0; }
229  std::queue<T *> posted_list;
231  boost::mutex postmutex;
232  boost::condition postcond;
233  };
234  std::map<int, Subscriber *> subscribers;
235 
236  std::queue<MBoxMessage *> free_list;
237  boost::mutex free_lock;
238 
239  };
240 }
241 
242 #endif
bool decReaderCount()
Definition: MultiMBox.hxx:57
bool flush(unsigned int subscriber_id)
flush all items from this mailbox for this subscriber
Definition: MultiMBox.hxx:182
boost::mutex rc_mutex
Definition: MultiMBox.hxx:77
void setMBoxTag(void *tag)
Definition: MultiMBox.hxx:72
boost::mutex free_lock
Definition: MultiMBox.hxx:237
unsigned int subscriber_count
Definition: MultiMBox.hxx:223
std::map< int, Subscriber * > subscribers
Definition: MultiMBox.hxx:234
int getSubscriberCount()
Definition: MultiMBox.hxx:95
T * getWait(unsigned int subscriber_id)
Definition: MultiMBox.hxx:114
MultiMBox(bool _keep_freelist=true)
Definition: MultiMBox.hxx:83
bool free(std::queue< MBoxMessage *> &free_list)
Definition: MultiMBox.hxx:59
void free(T *m)
Definition: MultiMBox.hxx:118
void setReaderCount(unsigned int rc)
Definition: MultiMBox.hxx:50
void addToPool(T *v)
Definition: MultiMBox.hxx:146
std::queue< T * > posted_list
Definition: MultiMBox.hxx:229
unsigned int inFlightCount()
Definition: MultiMBox.hxx:153
unsigned int reader_count
Definition: MultiMBox.hxx:76
boost::condition postcond
Definition: MultiMBox.hxx:232
T * getCommon(unsigned int subscriber_id, bool wait)
Definition: MultiMBox.hxx:192
bool checkMBoxTag(void *tag)
Definition: MultiMBox.hxx:73
std::queue< MBoxMessage * > free_list
Definition: MultiMBox.hxx:236
void put(T *m)
Definition: MultiMBox.hxx:97