50dd43ce1f166cbe66936df42e09109f4d0118a7
[ric-plt/sdl.git] / include / private / eventfd.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #ifndef SHAREDDATALAYER_EVENTFD_HPP_
18 #define SHAREDDATALAYER_EVENTFD_HPP_
19
20 #include <functional>
21 #include <list>
22 #include <mutex>
23 #include "private/filedescriptor.hpp"
24
25 namespace shareddatalayer
26 {
27     class FDMonitor;
28     class Engine;
29     class System;
30
31     class EventFD
32     {
33     public:
34         explicit EventFD(Engine& engine);
35
36         EventFD(System& system, Engine& engine);
37
38         ~EventFD();
39
40         using Callback = std::function<void()>;
41
42         void post(const Callback& callback);
43
44         EventFD(EventFD&&) = delete;
45         EventFD(const EventFD&) = delete;
46         EventFD& operator = (EventFD&&) = delete;
47         EventFD& operator = (const EventFD&) = delete;
48
49     private:
50         using Callbacks = std::list<Callback>;
51
52         void atomicPushBack(const Callback& callback);
53
54         Callbacks atomicPopAll();
55
56         void handleEvents();
57
58         void executeCallbacks();
59
60         static void popAndExecuteFirstCallback(Callbacks& callbacks);
61
62         System& system;
63         FileDescriptor fd;
64         Callbacks callbacks;
65         std::mutex callbacksMutex;
66     };
67 }
68
69 #endif