forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
457 additions
and
11 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,113 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) Microsoft | ||
// Licenced under MIT license. See LICENSE.txt for details. | ||
/////////////////////////////////////////////////////////////////////////////// | ||
#ifndef CPPCORO_DETAIL_LINUX_HPP_INCLUDED | ||
#define CPPCORO_DETAIL_LINUX_HPP_INCLUDED | ||
|
||
#include <fcntl.h> | ||
#include <linux/limits.h> | ||
#include <sys/epoll.h> | ||
#include <sys/eventfd.h> | ||
#include <sys/resource.h> | ||
#include <sys/stat.h> | ||
#include <sys/timerfd.h> | ||
#include <unistd.h> | ||
#include <utility> | ||
|
||
namespace cppcoro | ||
{ | ||
namespace detail | ||
{ | ||
namespace linux | ||
{ | ||
using fd_t = int; | ||
|
||
enum message_type | ||
{ | ||
CALLBACK_TYPE, | ||
RESUME_TYPE | ||
}; | ||
|
||
class safe_fd | ||
{ | ||
public: | ||
safe_fd() | ||
: m_fd(-1) | ||
{ | ||
} | ||
|
||
explicit safe_fd(fd_t fd) | ||
: m_fd(fd) | ||
{ | ||
} | ||
|
||
safe_fd(const safe_fd& other) = delete; | ||
|
||
safe_fd(safe_fd&& other) noexcept | ||
: m_fd(other.m_fd) | ||
{ | ||
other.m_fd = -1; | ||
} | ||
|
||
~safe_fd() { close(); } | ||
|
||
safe_fd& operator=(safe_fd fd) noexcept | ||
{ | ||
swap(fd); | ||
return *this; | ||
} | ||
|
||
constexpr fd_t fd() const { return m_fd; } | ||
|
||
/// Calls close() and sets the fd to -1. | ||
void close() noexcept; | ||
|
||
void swap(safe_fd& other) noexcept { std::swap(m_fd, other.m_fd); } | ||
|
||
bool operator==(const safe_fd& other) const { return m_fd == other.m_fd; } | ||
|
||
bool operator!=(const safe_fd& other) const { return m_fd != other.m_fd; } | ||
|
||
bool operator==(fd_t fd) const { return m_fd == fd; } | ||
|
||
bool operator!=(fd_t fd) const { return m_fd != fd; } | ||
|
||
private: | ||
fd_t m_fd; | ||
}; | ||
|
||
struct message | ||
{ | ||
enum message_type m_type; | ||
void* m_ptr; | ||
}; | ||
|
||
struct io_state : linux::message | ||
{ | ||
using callback_type = void(io_state* state); | ||
callback_type* m_callback; | ||
}; | ||
|
||
class message_queue | ||
{ | ||
private: | ||
int m_pipefd[2]; | ||
safe_fd m_epollfd; | ||
struct epoll_event m_ev; | ||
|
||
public: | ||
message_queue(); | ||
~message_queue(); | ||
bool enqueue_message(void* message, message_type type); | ||
bool dequeue_message(void*& message, message_type& type, bool wait); | ||
}; | ||
|
||
safe_fd create_event_fd(); | ||
safe_fd create_timer_fd(); | ||
safe_fd create_epoll_fd(); | ||
} // namespace linux | ||
} // namespace detail | ||
} // namespace cppcoro | ||
|
||
#endif |
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
Oops, something went wrong.