2023-03-12 19:57:20 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
#include "SocketEvent.hxx"
|
2013-01-10 19:05:47 +01:00
|
|
|
#include "Loop.hxx"
|
2020-10-13 16:11:57 +02:00
|
|
|
#include "event/Features.h"
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <utility>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2020-04-23 16:48:10 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
#include <cerrno>
|
|
|
|
#endif
|
|
|
|
|
2013-01-15 22:48:38 +01:00
|
|
|
void
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Open(SocketDescriptor _fd) noexcept
|
2013-01-15 22:48:38 +01:00
|
|
|
{
|
2017-08-10 18:25:22 +02:00
|
|
|
assert(_fd.IsDefined());
|
2020-12-04 09:23:56 +01:00
|
|
|
assert(!fd.IsDefined());
|
|
|
|
assert(GetScheduledFlags() == 0);
|
2013-01-15 22:48:38 +01:00
|
|
|
|
|
|
|
fd = _fd;
|
|
|
|
}
|
|
|
|
|
2013-01-27 22:37:01 +01:00
|
|
|
void
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Close() noexcept
|
2013-01-27 22:37:01 +01:00
|
|
|
{
|
2020-10-14 16:23:13 +02:00
|
|
|
if (!fd.IsDefined())
|
|
|
|
return;
|
|
|
|
|
2020-12-04 09:55:01 +01:00
|
|
|
/* closing the socket automatically unregisters it from epoll,
|
|
|
|
so we can omit the epoll_ctl(EPOLL_CTL_DEL) call and save
|
|
|
|
one system call */
|
2020-11-09 19:50:54 +01:00
|
|
|
if (std::exchange(scheduled_flags, 0) != 0) {
|
|
|
|
#ifdef HAVE_THREADED_EVENT_LOOP
|
|
|
|
/* can't use this optimization in multi-threaded
|
|
|
|
programs, because all file descriptors get
|
|
|
|
duplicated in forked processes, leaving them
|
|
|
|
registered in epoll, which could cause the parent
|
|
|
|
to crash */
|
|
|
|
loop.RemoveFD(fd.Get(), *this);
|
|
|
|
#else
|
2020-10-28 14:58:23 +01:00
|
|
|
loop.AbandonFD(*this);
|
2020-11-09 19:50:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
2020-10-14 16:29:24 +02:00
|
|
|
fd.Close();
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
2013-01-30 10:39:17 +01:00
|
|
|
|
2020-10-15 16:59:27 +02:00
|
|
|
void
|
|
|
|
SocketEvent::Abandon() noexcept
|
|
|
|
{
|
|
|
|
if (std::exchange(scheduled_flags, 0) != 0)
|
2020-10-28 14:58:23 +01:00
|
|
|
loop.AbandonFD(*this);
|
2020-10-15 16:59:27 +02:00
|
|
|
|
|
|
|
fd = SocketDescriptor::Undefined();
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool
|
2020-10-14 14:24:16 +02:00
|
|
|
SocketEvent::Schedule(unsigned flags) noexcept
|
2013-08-07 23:57:30 +02:00
|
|
|
{
|
2020-12-04 09:14:59 +01:00
|
|
|
if (flags != 0)
|
|
|
|
flags |= IMPLICIT_FLAGS;
|
|
|
|
|
2013-08-07 23:57:30 +02:00
|
|
|
if (flags == GetScheduledFlags())
|
2019-12-18 17:46:33 +01:00
|
|
|
return true;
|
2013-08-07 23:57:30 +02:00
|
|
|
|
2020-10-14 16:19:08 +02:00
|
|
|
assert(IsDefined());
|
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
bool success;
|
2013-08-07 22:16:59 +02:00
|
|
|
if (scheduled_flags == 0)
|
2019-12-18 17:46:33 +01:00
|
|
|
success = loop.AddFD(fd.Get(), flags, *this);
|
2013-08-07 22:16:59 +02:00
|
|
|
else if (flags == 0)
|
2020-10-18 19:39:21 +02:00
|
|
|
success = loop.RemoveFD(fd.Get(), *this);
|
2013-08-07 22:16:59 +02:00
|
|
|
else
|
2019-12-18 17:46:33 +01:00
|
|
|
success = loop.ModifyFD(fd.Get(), flags, *this);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
scheduled_flags = flags;
|
2020-04-23 16:48:10 +02:00
|
|
|
#ifdef USE_EPOLL
|
|
|
|
else if (errno == EBADF || errno == ENOENT)
|
|
|
|
/* the socket was probably closed by somebody else
|
|
|
|
(EBADF) or a new file descriptor with the same
|
|
|
|
number was created but not registered already
|
|
|
|
(ENOENT) - we can assume that there are no
|
|
|
|
scheduled events */
|
|
|
|
/* note that when this happens, we're actually lucky
|
|
|
|
that it has failed - imagine another thread may
|
|
|
|
meanwhile have created something on the same file
|
|
|
|
descriptor number, and has registered it; the
|
|
|
|
epoll_ctl() call above would then have succeeded,
|
|
|
|
but broke the other thread's epoll registration */
|
|
|
|
scheduled_flags = 0;
|
|
|
|
#endif
|
2013-08-07 22:16:59 +02:00
|
|
|
|
2019-12-18 17:46:33 +01:00
|
|
|
return success;
|
2013-08-07 23:57:30 +02:00
|
|
|
}
|
2020-10-14 16:21:33 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
SocketEvent::Dispatch() noexcept
|
|
|
|
{
|
|
|
|
const unsigned flags = std::exchange(ready_flags, 0) &
|
2020-12-04 09:14:59 +01:00
|
|
|
GetScheduledFlags();
|
2020-10-14 16:21:33 +02:00
|
|
|
|
|
|
|
if (flags != 0)
|
|
|
|
callback(flags);
|
|
|
|
}
|