Merge pull request #26142 from fritsch/ac3ftv
[xbmc.git] / xbmc / platform / linux / FDEventMonitor.h
blobc0b8395579d29d0fccbdeb52e2756c15012656f9
1 /*
2 * Copyright (C) 2014-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "platform/Platform.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/Thread.h"
15 #include <map>
16 #include <vector>
18 #include <sys/epoll.h>
20 /**
21 * Monitor a file descriptor with callback on poll() events.
23 class CFDEventMonitor : public IPlatformService, private CThread
25 public:
27 typedef void (*EventCallback)(int id, int fd, short revents, void *data);
29 struct MonitoredFD
31 int fd = -1; /**< File descriptor to be monitored */
32 short events = 0; /**< Events to be monitored (see poll(2)) */
34 EventCallback callback = nullptr; /** Callback to be called on events */
35 void *callbackData = nullptr; /** data parameter for EventCallback */
37 MonitoredFD(int fd_, short events_, EventCallback callback_, void *callbackData_) :
38 fd(fd_), events(events_), callback(callback_), callbackData(callbackData_) {}
39 MonitoredFD() = default;
42 CFDEventMonitor();
43 ~CFDEventMonitor() override;
45 void AddFD(const MonitoredFD& monitoredFD, int& id);
46 void AddFDs(const std::vector<MonitoredFD>& monitoredFDs, std::vector<int>& ids);
48 void RemoveFD(int id);
49 void RemoveFDs(const std::vector<int>& ids);
51 protected:
52 void Process() override;
54 private:
55 void AddFDLocked(const MonitoredFD& monitoredFD, int& id);
57 void AddPollDesc(int id, int fd, short events);
58 void UpdatePollDescs();
60 void StartMonitoring();
61 void InterruptPoll();
63 std::map<int, MonitoredFD> m_monitoredFDs;
65 /* these are kept synchronized */
66 std::vector<int> m_monitoredFDbyPollDescs;
67 std::vector<struct pollfd> m_pollDescs;
69 int m_nextID = 0;
70 int m_wakeupfd = -1;
72 CCriticalSection m_mutex;
73 CCriticalSection m_pollMutex;