Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DbusEventDispatcher.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  * DbusEventDispatcher.h
21  *
22  */
23 
24 #ifndef AI_IPC_DBUSEVENTDISPATCHER_H
25 #define AI_IPC_DBUSEVENTDISPATCHER_H
26 
27 #include <dbus/dbus.h>
28 
29 #include <thread>
30 #include <cstdint>
31 #include <memory>
32 #include <queue>
33 #include <mutex>
34 #include <condition_variable>
35 #include <functional>
36 
37 namespace AI_IPC
38 {
39 
40 // -----------------------------------------------------------------------------
60 {
61 public:
63 
65 
66 public:
67  void startEventDispatcher(DBusConnection *connection);
68 
69  void stopEventDispatcher();
70 
71 public:
72  template< class Function, class... Args >
73  bool callInEventLoop(Function&& f, Args&&... args)
74  {
75  return this->callInEventLoopImpl(std::bind(std::forward<Function>(f),
76  std::forward<Args>(args)...));
77  }
78 
79  template< class Function >
80  bool callInEventLoop(Function func)
81  {
82  return this->callInEventLoopImpl(func);
83  }
84 
85 private:
87 
88  void cleanupAllEvents();
89 
90 private:
91  static void dispatchStatusCb(DBusConnection *connection, DBusDispatchStatus status, void *userData);
92 
93  static void wakeUpCb(void *userData);
94 
95 private:
96  DBusConnection* mDbusConnection;
97 
98  std::thread mEventDispatcherThread;
99 
100  int mDeathEventFd;
101  int mWakeupEventFd;
102  int mDispatchEventFd;
103 
104 private:
105  bool callInEventLoopImpl(const std::function<void()>& func);
106 
107  uint64_t mCallCounter;
108  std::mutex mCallLock;
109  std::condition_variable mCallCondVar;
110  std::queue< std::pair<uint64_t, std::function<void()>> > mCallQueue;
111 
112 };
113 
114 } // namespace AI_IPC
115 
116 #endif // !defined(AI_IPC_DBUSEVENTDISPATCHER_H)
Event dispatcher loop, runs the thread that polls on the dbus fds.
Definition: DbusEventDispatcher.h:60
void startEventDispatcher(DBusConnection *connection)
Starts the event dispatch thread for the given dbus connection.
Definition: DbusEventDispatcher.cpp:81
static void dispatchStatusCb(DBusConnection *connection, DBusDispatchStatus status, void *userData)
libdbus callback when dispatch status changes.
Definition: DbusEventDispatcher.cpp:414
static void wakeUpCb(void *userData)
libdbus callback request to wake-up the event loop.
Definition: DbusEventDispatcher.cpp:448
void eventDispatcherThreadFn()
Thread function that processes the events.
Definition: DbusEventDispatcher.cpp:199
void stopEventDispatcher()
Stops the event dispatch thread and cleans up all file descriptors.
Definition: DbusEventDispatcher.cpp:144
void cleanupAllEvents()
Closes the three eventfd's used to wake up and trigger events in the poll loop.
Definition: DbusEventDispatcher.cpp:379
bool callInEventLoopImpl(const std::function< void()> &func)
Calls the supplied function in the context of the dispatch thread.
Definition: DbusEventDispatcher.cpp:485