File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef EVENTDISPATCHER_P_H
0025 #define EVENTDISPATCHER_P_H
0026 
0027 #include "eventdispatcher.h"
0028 
0029 #include "iioeventsource.h"
0030 #include "message.h"
0031 #include "platform.h"
0032 #include "spinlock.h"
0033 #include "types.h"
0034 
0035 #include <map>
0036 #include <memory>
0037 #include <unordered_map>
0038 #include <vector>
0039 
0040 struct Event;
0041 class IIoEventListener;
0042 class IEventPoller;
0043 class Message;
0044 class PendingReplyPrivate;
0045 class Timer;
0046 class ConnectionPrivate;
0047 
0048 // note that the main purpose of EventDispatcher so far is dispatching I/O events; dispatching Event
0049 // instances is secondary
0050 class EventDispatcherPrivate : public IIoEventSource
0051 {
0052 public:
0053     static EventDispatcherPrivate *get(EventDispatcher *ed) { return ed->d; }
0054 
0055     ~EventDispatcherPrivate();
0056 
0057     int timeToFirstDueTimer() const;
0058     uint nextTimerSerial();
0059     void tryCompactTimerSerials();
0060     void triggerDueTimers();
0061 
0062 protected:
0063     // IIoEventSource
0064     void addIoListenerInternal(IIoEventListener *iol, uint32 ioRw) override;
0065     void removeIoListenerInternal(IIoEventListener *iol) override;
0066     void updateIoInterestInternal(IIoEventListener *iol, uint32 ioRw) override;
0067 
0068 public:
0069     bool addIoEventListener(IIoEventListener *iol);
0070     bool removeIoEventListener(IIoEventListener *iol);
0071     void setReadWriteInterest(IIoEventListener *iol, bool read, bool write);
0072     // for IEventPoller
0073     friend class IEventPoller;
0074     void notifyListenerForIo(FileDescriptor fd, IO::RW ioRw);
0075     // for Timer
0076     friend class Timer;
0077     void printTimerMap() const;
0078     void addTimer(Timer *timer);
0079     void removeTimer(Timer *timer);
0080     // for ForeignEventLoopIntegrator (calls into it, not called from it)
0081     void maybeSetTimeoutForIntegrator();
0082     // for Connection
0083     // this is similar to interrupt(), but doesn't make poll() return false and will call
0084     // m_connectionToNotify -> processQueuedEvents()
0085     void wakeForEvents();
0086     void queueEvent(std::unique_ptr<Event> evt); // safe to call from any thread
0087     void processAuxEvents();
0088 
0089     IEventPoller *m_poller = nullptr;
0090     ForeignEventLoopIntegrator *m_integrator = nullptr;
0091     std::unordered_map<FileDescriptor, IIoEventListener*> m_ioListeners;
0092 
0093     // Attention! When changing s_maxTimerSerial, or the general approach to ensuring that timers time out
0094     // in the correct order, make sure that testSerialWraparound() still tests the ordering technique where
0095     // it's likely to break.
0096     static const int s_maxTimerSerial = 0x3ff; // 10 bits set
0097     uint m_currentTimerSerial = 0;
0098     // the highest 54 bits in "due" encode due time, the lowest 10 bits act like a serial number to reduce
0099     // (not eliminate - the serial eventually wraps around) collisions of timers with the same timeout
0100     // (this is not expressed as a struct/class to avoid compiler pessimization in the multimap code)
0101     std::multimap<uint64 /* due */, Timer*> m_timers;
0102     // for logic to prevent executing a timer in the dispatch run it was added
0103     uint64 m_triggerTime = 0;
0104     bool m_serialsCompacted = false;
0105     decltype(m_timers)::iterator m_adjustedIteratorOfNextTimer;
0106 
0107     // for inter thread event delivery to Connection
0108     ConnectionPrivate *m_connectionToNotify = nullptr;
0109 
0110     Spinlock m_queuedEventsLock;
0111     std::vector<std::unique_ptr<Event>> m_queuedEvents;
0112 };
0113 
0114 #endif