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)
5  2017, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
27 #ifndef LIB_CALL_QUEUE_H
28 #define LIB_CALL_QUEUE_H
29 
30 
31 #include "lib/error.hpp"
32 #include "lib/sync.hpp"
33 #include "lib/iter-stack.hpp"
34 #include "lib/nocopy.hpp"
35 
36 #include <functional>
37 
38 
39 namespace lib {
40  namespace error = lumiera::error;
41 
42 
43 
44  /*********************************************************/
49  class CallQueue
51  , public Sync<>
52  {
53  public:
54  using Operation = std::function<void(void)>;
55 
56  private:
58 
59  public:
60  CallQueue() { }
61 
62 
63  CallQueue&
64  feed (Operation&& op)
65  {
66  if (not op)
67  throw error::Logic( "Unbound Functor fed to dispatcher CallQueue"
68  , error::LUMIERA_ERROR_BOTTOM_VALUE);
69  {
70  Lock sync{this};
71  queue_.feed (move(op));
72  }
73  return *this;
74  }
75 
76  CallQueue&
77  invoke()
78  {
79  if (not empty())
80  {
81  Operation operate;
82  {
83  Lock sync{this};
84  operate = move (*queue_);
85  ++queue_;
86  }
87  ASSERT (operate);
88  operate();
89  }
90  return *this;
91  }
92 
93 
94  /* == diagnostics == */
95 
96  size_t
97  size() const
98  {
99  Lock sync{this};
100  return queue_.size();
101  }
102 
103  bool
104  empty() const
105  {
106  return 0 == size();
107  }
108  };
109 
110 
111 
112 } // namespace lib
113 #endif /*LIB_CALL_QUEUE_H*/
Facility for monitor object based locking.
Definition: sync.hpp:209
A threadsafe queue for bound void(void) functors.
Definition: call-queue.hpp:49
Conveniently iterable stack and queue containers.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Implementation namespace for support and library code.
Object Monitor based synchronisation.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Lumiera error handling (C++ interface).