|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) Microsoft |
| 3 | +// Licenced under MIT license. See LICENSE.txt for details. |
| 4 | +/////////////////////////////////////////////////////////////////////////////// |
| 5 | +#ifndef CPPCORO_DETAIL_LINUX_HPP_INCLUDED |
| 6 | +#define CPPCORO_DETAIL_LINUX_HPP_INCLUDED |
| 7 | + |
| 8 | +#include <fcntl.h> |
| 9 | +#include <linux/limits.h> |
| 10 | +#include <sys/epoll.h> |
| 11 | +#include <sys/eventfd.h> |
| 12 | +#include <sys/resource.h> |
| 13 | +#include <sys/stat.h> |
| 14 | +#include <sys/timerfd.h> |
| 15 | +#include <unistd.h> |
| 16 | +#include <utility> |
| 17 | + |
| 18 | +namespace cppcoro |
| 19 | +{ |
| 20 | + namespace detail |
| 21 | + { |
| 22 | + namespace linux |
| 23 | + { |
| 24 | + using fd_t = int; |
| 25 | + |
| 26 | + enum message_type |
| 27 | + { |
| 28 | + CALLBACK_TYPE, |
| 29 | + RESUME_TYPE |
| 30 | + }; |
| 31 | + |
| 32 | + class safe_fd |
| 33 | + { |
| 34 | + public: |
| 35 | + safe_fd() |
| 36 | + : m_fd(-1) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + explicit safe_fd(fd_t fd) |
| 41 | + : m_fd(fd) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + safe_fd(const safe_fd& other) = delete; |
| 46 | + |
| 47 | + safe_fd(safe_fd&& other) noexcept |
| 48 | + : m_fd(other.m_fd) |
| 49 | + { |
| 50 | + other.m_fd = -1; |
| 51 | + } |
| 52 | + |
| 53 | + ~safe_fd() { close(); } |
| 54 | + |
| 55 | + safe_fd& operator=(safe_fd fd) noexcept |
| 56 | + { |
| 57 | + swap(fd); |
| 58 | + return *this; |
| 59 | + } |
| 60 | + |
| 61 | + constexpr fd_t fd() const { return m_fd; } |
| 62 | + |
| 63 | + /// Calls close() and sets the fd to -1. |
| 64 | + void close() noexcept; |
| 65 | + |
| 66 | + void swap(safe_fd& other) noexcept { std::swap(m_fd, other.m_fd); } |
| 67 | + |
| 68 | + bool operator==(const safe_fd& other) const { return m_fd == other.m_fd; } |
| 69 | + |
| 70 | + bool operator!=(const safe_fd& other) const { return m_fd != other.m_fd; } |
| 71 | + |
| 72 | + bool operator==(fd_t fd) const { return m_fd == fd; } |
| 73 | + |
| 74 | + bool operator!=(fd_t fd) const { return m_fd != fd; } |
| 75 | + |
| 76 | + private: |
| 77 | + fd_t m_fd; |
| 78 | + }; |
| 79 | + |
| 80 | + struct message |
| 81 | + { |
| 82 | + enum message_type m_type; |
| 83 | + void* m_ptr; |
| 84 | + }; |
| 85 | + |
| 86 | + struct io_state : linux::message |
| 87 | + { |
| 88 | + using callback_type = void(io_state* state); |
| 89 | + callback_type* m_callback; |
| 90 | + }; |
| 91 | + |
| 92 | + class message_queue |
| 93 | + { |
| 94 | + private: |
| 95 | + int m_pipefd[2]; |
| 96 | + safe_fd m_epollfd; |
| 97 | + struct epoll_event m_ev; |
| 98 | + |
| 99 | + public: |
| 100 | + message_queue(); |
| 101 | + ~message_queue(); |
| 102 | + bool enqueue_message(void* message, message_type type); |
| 103 | + bool dequeue_message(void*& message, message_type& type, bool wait); |
| 104 | + }; |
| 105 | + |
| 106 | + safe_fd create_event_fd(); |
| 107 | + safe_fd create_timer_fd(); |
| 108 | + safe_fd create_epoll_fd(); |
| 109 | + } // namespace linux |
| 110 | + } // namespace detail |
| 111 | +} // namespace cppcoro |
| 112 | + |
| 113 | +#endif |
0 commit comments