Loop.hxx 5.05 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_EVENT_LOOP_HXX
#define MPD_EVENT_LOOP_HXX

#include "check.h"
24
#include "thread/Id.hxx"
25
#include "Compiler.h"
26

27
#include "PollGroup.hxx"
28 29 30
#include "thread/Mutex.hxx"
#include "WakeFD.hxx"
#include "SocketMonitor.hxx"
31
#include "TimerEvent.hxx"
32 33
#include "IdleMonitor.hxx"
#include "DeferredMonitor.hxx"
34 35

#include <boost/intrusive/set.hpp>
36
#include <boost/intrusive/list.hpp>
37

38
#include <chrono>
39

40 41
#include <assert.h>

42 43 44 45 46 47 48
/**
 * An event loop that polls for events on file/socket descriptors.
 *
 * This class is not thread-safe, all methods must be called from the
 * thread that runs it, except where explicitly documented as
 * thread-safe.
 *
49
 * @see SocketMonitor, MultiSocketMonitor, TimerEvent, IdleMonitor
50
 */
51
class EventLoop final : SocketMonitor
52
{
53
	WakeFD wake_fd;
54

55
	struct TimerCompare {
56 57
		constexpr bool operator()(const TimerEvent &a,
					  const TimerEvent &b) const {
58 59
			return a.due < b.due;
		}
60 61
	};

62 63 64 65
	typedef boost::intrusive::multiset<TimerEvent,
					   boost::intrusive::member_hook<TimerEvent,
									 TimerEvent::TimerSetHook,
									 &TimerEvent::timer_set_hook>,
66 67 68
					   boost::intrusive::compare<TimerCompare>,
					   boost::intrusive::constant_time_size<false>> TimerSet;
	TimerSet timers;
69

70 71 72 73 74 75
	typedef boost::intrusive::list<IdleMonitor,
				       boost::intrusive::member_hook<IdleMonitor,
								     IdleMonitor::ListHook,
								     &IdleMonitor::list_hook>,
				       boost::intrusive::constant_time_size<false>> IdleList;
	IdleList idle;
76 77

	Mutex mutex;
78 79 80 81 82 83 84

	typedef boost::intrusive::list<DeferredMonitor,
				       boost::intrusive::member_hook<DeferredMonitor,
								     DeferredMonitor::ListHook,
								     &DeferredMonitor::list_hook>,
				       boost::intrusive::constant_time_size<false>> DeferredList;
	DeferredList deferred;
85

86
	std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
87

88
	bool quit = false;
89

90 91 92 93 94 95
	/**
	 * True when the object has been modified and another check is
	 * necessary before going to sleep via PollGroup::ReadEvents().
	 */
	bool again;

96 97 98 99 100 101
	/**
	 * True when handling callbacks, false when waiting for I/O or
	 * timeout.
	 *
	 * Protected with #mutex.
	 */
102
	bool busy = true;
103

104 105
	PollGroup poll_group;
	PollResult poll_result;
106

107 108 109
	/**
	 * A reference to the thread that is currently inside Run().
	 */
110
	ThreadId thread = ThreadId::Null();
111

112
public:
113 114 115
	explicit EventLoop(ThreadId _thread);
	EventLoop():EventLoop(ThreadId::GetCurrent()) {}

116 117
	~EventLoop();

118
	/**
119
	 * A caching wrapper for std::chrono::steady_clock::now().
120
	 */
121
	std::chrono::steady_clock::time_point GetTime() const {
122 123
		assert(IsInside());

124
		return now;
125 126
	}

127 128 129 130 131
	/**
	 * Stop execution of this #EventLoop at the next chance.  This
	 * method is thread-safe and non-blocking: after returning, it
	 * is not guaranteed that the EventLoop has really stopped.
	 */
132 133 134
	void Break();

	bool AddFD(int _fd, unsigned flags, SocketMonitor &m) {
135
		assert(IsInside());
136

137
		return poll_group.Add(_fd, flags, &m);
138 139 140
	}

	bool ModifyFD(int _fd, unsigned flags, SocketMonitor &m) {
141 142
		assert(IsInside());

143
		return poll_group.Modify(_fd, flags, &m);
144 145
	}

146 147 148 149 150
	/**
	 * Remove the given #SocketMonitor after the file descriptor
	 * has been closed.  This is like RemoveFD(), but does not
	 * attempt to use #EPOLL_CTL_DEL.
	 */
151
	bool Abandon(int fd, SocketMonitor &m);
152

153 154 155 156 157
	bool RemoveFD(int fd, SocketMonitor &m);

	void AddIdle(IdleMonitor &i);
	void RemoveIdle(IdleMonitor &i);

158
	void AddTimer(TimerEvent &t,
159
		      std::chrono::steady_clock::duration d);
160
	void CancelTimer(TimerEvent &t);
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	/**
	 * Schedule a call to DeferredMonitor::RunDeferred().
	 *
	 * This method is thread-safe.
	 */
	void AddDeferred(DeferredMonitor &d);

	/**
	 * Cancel a pending call to DeferredMonitor::RunDeferred().
	 * However after returning, the call may still be running.
	 *
	 * This method is thread-safe.
	 */
	void RemoveDeferred(DeferredMonitor &d);

177 178 179 180
	/**
	 * The main function of this class.  It will loop until
	 * Break() gets called.  Can be called only once.
	 */
181 182 183
	void Run();

private:
184 185 186 187 188 189 190
	/**
	 * Invoke all pending DeferredMonitors.
	 *
	 * Caller must lock the mutex.
	 */
	void HandleDeferred();

191 192 193 194 195 196 197 198
	virtual bool OnSocketReady(unsigned flags) override;

public:

	/**
	 * Are we currently running inside this EventLoop's thread?
	 */
	gcc_pure
199
	bool IsInside() const noexcept {
200 201
		return thread.IsInside();
	}
202 203 204
};

#endif /* MAIN_NOTIFY_H */