Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyWorkQueue.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 2020 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: DobbyWorkQueue.h
21  *
22  */
23 
24 #ifndef DOBBYWORKQUEUE_H
25 #define DOBBYWORKQUEUE_H
26 
27 #include <string>
28 #include <chrono>
29 #include <queue>
30 #include <atomic>
31 #include <thread>
32 #include <mutex>
33 #include "ConditionVariable.h"
34 #include <functional>
35 
37 {
38 public:
40  ~DobbyWorkQueue();
41 
42  void run();
43  bool runFor(const std::chrono::milliseconds &msecs);
44  bool runUntil(const std::chrono::steady_clock::time_point &deadline);
45 
46  void exit();
47 
48 public:
49  using WorkFunc = std::function<void()>;
50 
51  bool doWork(WorkFunc &&work);
52  bool postWork(WorkFunc &&work);
53 
54 private:
55  struct WorkItem
56  {
57  uint64_t tag;
58  WorkFunc func;
59 
60  WorkItem(uint64_t t, WorkFunc &&f)
61  : tag(t), func(std::move(f))
62  { }
63  };
64 
65  uint64_t mWorkCounter;
66 
67  bool mExitRequested;
68  std::atomic<std::thread::id> mRunningThreadId;
69 
70  AICommon::Mutex mWorkQueueLock;
71  AICommon::ConditionVariable mWorkQueueCond;
72  std::queue< WorkItem > mWorkQueue;
73 
74  AICommon::Mutex mWorkCompleteLock;
75  AICommon::ConditionVariable mWorkCompleteCond;
76  uint64_t mWorkCompleteCounter;
77 };
78 
79 
80 #endif // DOBBYWORKQUEUE_H
Definition: ConditionVariable.h:67
Definition: Mutex.h:67
Definition: DobbyWorkQueue.h:37
void exit()
Unblocks the runXXX functions.
Definition: DobbyWorkQueue.cpp:52
bool runUntil(const std::chrono::steady_clock::time_point &deadline)
Runs the event loop until the deadline time passes.
Definition: DobbyWorkQueue.cpp:112
void run()
Runs the event loop.
Definition: DobbyWorkQueue.cpp:80
bool postWork(WorkFunc &&work)
Posts a work job onto the queue.
Definition: DobbyWorkQueue.cpp:246
bool doWork(WorkFunc &&work)
Posts a work job onto the queue and waits till it completes.
Definition: DobbyWorkQueue.cpp:196
bool runFor(const std::chrono::milliseconds &msecs)
Runs the event loop for msecs milliseconds.
Definition: DobbyWorkQueue.cpp:96
Definition: DobbyWorkQueue.h:56