forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce
EdgeTriggeredEvents
, move {epoll, kqueue} fd there
- Loading branch information
Showing
5 changed files
with
131 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2020-2024 The Dash Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <util/edge.h> | ||
|
||
#include <logging.h> | ||
#include <util/sock.h> | ||
|
||
#include <assert.h> | ||
|
||
#ifdef USE_EPOLL | ||
#include <sys/epoll.h> | ||
#endif | ||
|
||
#ifdef USE_KQUEUE | ||
#include <sys/event.h> | ||
#endif | ||
|
||
EdgeTriggeredEvents::EdgeTriggeredEvents(SocketEventsMode events_mode) | ||
: m_mode(events_mode) | ||
{ | ||
if (m_mode == SocketEventsMode::EPoll) { | ||
#ifdef USE_EPOLL | ||
m_fd = epoll_create1(0); | ||
if (m_fd == -1) { | ||
LogPrintf("Unable to initialize EdgeTriggeredEvents, epoll_create1 returned -1\n"); | ||
return; | ||
} | ||
#else | ||
LogPrintf("Attempting to initialize EdgeTriggeredEvents for epoll without support compiled in!\n"); | ||
return; | ||
#endif /* USE_EPOLL */ | ||
} else if (m_mode == SocketEventsMode::KQueue) { | ||
#ifdef USE_KQUEUE | ||
m_fd = kqueue(); | ||
if (m_fd == -1) { | ||
LogPrintf("Unable to initialize EdgeTriggeredEvents, kqueue returned -1\n"); | ||
return; | ||
} | ||
#else | ||
LogPrintf("Attempting to initialize EdgeTriggeredEvents for kqueue without support compiled in!\n"); | ||
return; | ||
#endif /* USE_KQUEUE */ | ||
} else { | ||
assert(false); | ||
} | ||
m_valid = true; | ||
} | ||
|
||
EdgeTriggeredEvents::~EdgeTriggeredEvents() | ||
{ | ||
if (m_valid) { | ||
#if defined(USE_KQUEUE) || defined(USE_EPOLL) | ||
close(m_fd); | ||
#else | ||
assert(false); | ||
#endif /* defined(USE_KQUEUE) || defined(USE_EPOLL) */ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2020-2024 The Dash Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_UTIL_EDGE_H | ||
#define BITCOIN_UTIL_EDGE_H | ||
|
||
#include <cstdint> | ||
|
||
enum class SocketEventsMode : int8_t; | ||
|
||
/** | ||
* A manager for abstracting logic surrounding edge-triggered socket events | ||
* modes like kqueue and epoll. | ||
*/ | ||
class EdgeTriggeredEvents | ||
{ | ||
public: | ||
explicit EdgeTriggeredEvents(SocketEventsMode events_mode); | ||
~EdgeTriggeredEvents(); | ||
|
||
bool IsValid() const { return m_valid; } | ||
|
||
public: | ||
/* File descriptor used to interact with events mode */ | ||
int m_fd{-1}; | ||
|
||
private: | ||
/* Instance validity flag set during construction */ | ||
bool m_valid{false}; | ||
/* Flag for storing selected socket events mode */ | ||
SocketEventsMode m_mode; | ||
}; | ||
|
||
#endif /* BITCOIN_UTIL_EDGE_H */ |