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.
11 #include "platform/Platform.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/Thread.h"
18 #include <sys/epoll.h>
21 * Monitor a file descriptor with callback on poll() events.
23 class CFDEventMonitor
: public IPlatformService
, private CThread
27 typedef void (*EventCallback
)(int id
, int fd
, short revents
, void *data
);
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;
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
);
52 void Process() override
;
55 void AddFDLocked(const MonitoredFD
& monitoredFD
, int& id
);
57 void AddPollDesc(int id
, int fd
, short events
);
58 void UpdatePollDescs();
60 void StartMonitoring();
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
;
72 CCriticalSection m_mutex
;
73 CCriticalSection m_pollMutex
;