-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from tgockel/issue/82/server-spin
server: Do not spin in `server::run_process`.
- Loading branch information
Showing
6 changed files
with
201 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "event_handle.hpp" | ||
#include "close.hpp" | ||
|
||
#include <cerrno> | ||
#include <cstdint> | ||
#include <system_error> | ||
|
||
#include <sys/eventfd.h> | ||
#include <unistd.h> | ||
|
||
namespace zk::server::detail | ||
{ | ||
|
||
event_handle::event_handle() : | ||
_fd(::eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) | ||
{ } | ||
|
||
event_handle::~event_handle() noexcept | ||
{ | ||
close(); | ||
} | ||
|
||
void event_handle::close() noexcept | ||
{ | ||
if (_fd != -1) | ||
{ | ||
detail::close(_fd); | ||
_fd = -1; | ||
} | ||
} | ||
|
||
void event_handle::notify_one() | ||
{ | ||
std::uint64_t x = 1; | ||
if (::write(_fd, &x, sizeof x) == -1 && errno != EAGAIN) | ||
throw std::system_error(errno, std::system_category(), "event_handle::notify_one()"); | ||
} | ||
|
||
bool event_handle::try_wait() | ||
{ | ||
std::uint64_t burn; | ||
if (::read(_fd, &burn, sizeof burn) == -1) | ||
return errno == EAGAIN ? false | ||
: throw std::system_error(errno, std::system_category(), "event_handle::try_wait()"); | ||
else | ||
return true; | ||
} | ||
|
||
} |
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,42 @@ | ||
#pragma once | ||
|
||
#include <zk/config.hpp> | ||
|
||
namespace zk::server::detail | ||
{ | ||
|
||
class event_handle final | ||
{ | ||
public: | ||
using native_handle_type = int; | ||
|
||
public: | ||
explicit event_handle(); | ||
|
||
event_handle(const event_handle&) = delete; | ||
|
||
event_handle& operator=(const event_handle&) = delete; | ||
|
||
~event_handle() noexcept; | ||
|
||
/// Close this event for future signalling. This is automatically called from the destructor. | ||
void close() noexcept; | ||
|
||
/// Signal this handle that something has happened. | ||
void notify_one(); | ||
|
||
/// Attempt to wait for this handle to be signalled, but do not block. | ||
/// | ||
/// \returns \c true if we successfully waited for a signal (and consumed it); \c false if this handle was not | ||
/// signalled. | ||
bool try_wait(); | ||
|
||
/// Get the file descriptor backing this handle. This is generally only used when interacting with the kernel and | ||
/// should be avoided in regular use. | ||
native_handle_type native_handle() { return _fd; } | ||
|
||
private: | ||
native_handle_type _fd; | ||
}; | ||
|
||
} |
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