Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
ThreadedDispatcher.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2014 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: ThreadedDispatcher.h
21  *
22  * Created on 26 June 2014
23  *
24  */
25 
26 #ifndef THREADEDDISPATCHER_H
27 #define THREADEDDISPATCHER_H
28 
29 #include <IDispatcher.h>
30 #include <mutex>
31 #include <thread>
32 #include <condition_variable>
33 #include <deque>
34 #include <iostream>
35 #include <string>
36 #include <memory>
37 
38 namespace AICommon
39 {
40 
46 {
47 public: //IDispatcher
48 
52  virtual void post(std::function<void ()> work) final;
53 
58  virtual void sync() final;
59 
63  virtual bool invokedFromDispatcherThread() final;
64 
65 public: //this class
66 
70  void flush();
71 
75  void stop();
76 
77  ThreadedDispatcher(const std::string& name = std::string());
78 
82  ThreadedDispatcher(int priority, const std::string& name = std::string());
83 
85 
86 private:
87 
92 
96  void doWork(const std::string& name, int priority);
97 
103  inline std::function<void ()> next();
104 
105  std::mutex m;
106  std::condition_variable cv;
107  bool running;
108  std::deque<std::function<void ()>> q;
109  std::thread t;
110 
111  typedef ThreadedDispatcher This;
112 };
113 
114 } //AICommon
115 
116 #endif /* THREADEDDISPATCHER_H */
117 
A dispatcher interface.
Definition: IDispatcher.h:41
A dispatcher that does all the work on a single, separate thread started in constructor.
Definition: ThreadedDispatcher.h:46
void stop()
Cancels any work that is not already in progress, stop accepting new work.
Definition: ThreadedDispatcher.cpp:163
std::function< void()> next()
Returns next work item.
Definition: ThreadedDispatcher.cpp:211
virtual void post(std::function< void()> work) final
Definition: ThreadedDispatcher.cpp:41
virtual void sync() final
Ensures that anything that was in the queue before the call has been executed before returning.
Definition: ThreadedDispatcher.cpp:106
void flush()
Perform any work remaining in the queue, then stop accepting new work.
Definition: ThreadedDispatcher.cpp:141
bool hasMoreWorkOrWasStopRequested()
Predicate for condition variable used communication with the worker thread.
Definition: ThreadedDispatcher.cpp:178
void doWork(const std::string &name, int priority)
The dispatcher thread entry point.
Definition: ThreadedDispatcher.cpp:182
virtual bool invokedFromDispatcherThread() final
Get dispatcher thread Id.
Definition: ThreadedDispatcher.cpp:83