Lumiera  0.pre.03
»edit your freedom«
call-queue.hpp
Go to the documentation of this file.
1 /*
2  CALL-QUEUE.hpp - a queue to dispatch bound function invocations into another thread
3 
4  Copyright (C) Lumiera.org
5  2017, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
36 #ifndef LIB_CALL_QUEUE_H
37 #define LIB_CALL_QUEUE_H
38 
39 
40 #include "lib/error.hpp"
41 #include "lib/sync.hpp"
42 #include "lib/iter-stack.hpp"
43 #include "lib/nocopy.hpp"
44 
45 #include <functional>
46 
47 
48 namespace lib {
49  namespace error = lumiera::error;
50 
51 
52 
53  /*********************************************************/
58  class CallQueue
60  , public Sync<>
61  {
62  public:
63  using Operation = std::function<void(void)>;
64 
65  private:
67 
68  public:
69  CallQueue() { }
70 
71 
72  CallQueue&
73  feed (Operation&& op)
74  {
75  if (not op)
76  throw error::Logic( "Unbound Functor fed to dispatcher CallQueue"
77  , error::LUMIERA_ERROR_BOTTOM_VALUE);
78  {
79  Lock sync{this};
80  queue_.feed (move(op));
81  }
82  return *this;
83  }
84 
85  CallQueue&
86  invoke()
87  {
88  if (not empty())
89  {
90  Operation operate;
91  {
92  Lock sync{this};
93  operate = move (*queue_);
94  ++queue_;
95  }
96  ASSERT (operate);
97  operate();
98  }
99  return *this;
100  }
101 
102 
103  /* == diagnostics == */
104 
105  size_t
106  size() const
107  {
108  Lock sync{this};
109  return queue_.size();
110  }
111 
112  bool
113  empty() const
114  {
115  return 0 == size();
116  }
117  };
118 
119 
120 
121 } // namespace lib
122 #endif /*LIB_CALL_QUEUE_H*/
Facility for monitor object based locking.
Definition: sync.hpp:217
A threadsafe queue for bound void(void) functors.
Definition: call-queue.hpp:58
Conveniently iterable stack and queue containers.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Implementation namespace for support and library code.
Object Monitor based synchronisation.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Lumiera error handling (C++ interface).