Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
event-poll.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef CARGO_IPC_EPOLL_EVENT_POLL_HPP
26 #define CARGO_IPC_EPOLL_EVENT_POLL_HPP
27 
29 
30 #include <functional>
31 #include <mutex>
32 #include <unordered_map>
33 #include <memory>
34 
35 namespace cargo {
36 namespace ipc {
37 namespace epoll {
38 
47 class EventPoll {
48 public:
56  typedef std::function<void(int fd, Events events)> Callback;
57 
62  EventPoll();
63  ~EventPoll();
64 
69  int getPollFD() const;
70 
80  void addFD(const int fd, const Events events, Callback&& callback);
81 
89  void modifyFD(const int fd, const Events events);
90 
95  void removeFD(const int fd);
96 
106  bool dispatchIteration(const int timeoutMs);
107 
108 private:
109  typedef std::recursive_mutex Mutex;
110 
111  const int mPollFD;
113  std::unordered_map<int, std::shared_ptr<Callback>> mCallbacks;
114 
115  bool addFDInternal(const int fd, const Events events);
116  bool modifyFDInternal(const int fd, const Events events);
117  void removeFDInternal(const int fd);
118 };
119 
120 
121 } // namespace epoll
122 } // namespace ipc
123 } // namespace cargo
124 
125 #endif // CARGO_IPC_EPOLL_EVENT_POLL_HPP
std::function< void(int fd, Events events)> Callback
Generic function type used as callback for epoll events.
Definition: event-poll.hpp:56
std::unordered_map< int, std::shared_ptr< Callback > > mCallbacks
Definition: event-poll.hpp:113
bool dispatchIteration(const int timeoutMs)
Wait for events on descriptor on the watch list.
Definition: event-poll.cpp:113
void modifyFD(const int fd, const Events events)
Modify watched events for descriptor.
Definition: event-poll.cpp:89
const int mPollFD
Definition: event-poll.hpp:111
bool modifyFDInternal(const int fd, const Events events)
Definition: event-poll.cpp:165
std::recursive_mutex Mutex
Definition: event-poll.hpp:109
Mutex mMutex
Definition: event-poll.hpp:112
void removeFD(const int fd)
Remove descriptor from the watch list.
Definition: event-poll.cpp:99
void removeFDInternal(const int fd)
Definition: event-poll.cpp:179
int getPollFD() const
Returns epoll handle.
Definition: event-poll.cpp:64
~EventPoll()
Definition: event-poll.cpp:52
unsigned int Events
bitmask of EPOLL* constants
Definition: events.hpp:39
This class waits on registered file descriptor for events.
Definition: event-poll.hpp:47
void addFD(const int fd, const Events events, Callback &&callback)
Add descriptor and it's watched events.
Definition: event-poll.cpp:69
EventPoll()
Constructs the EventPoll and initializes the underlaying epoll mechanism.
Definition: event-poll.cpp:42
Epoll events.
bool addFDInternal(const int fd, const Events events)
Definition: event-poll.cpp:151