|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) Lewis Baker |
| 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 <cppcoro/config.hpp> |
| 9 | + |
| 10 | +#include <cstdint> |
| 11 | +#include <cstdio> |
| 12 | +#include <utility> |
| 13 | + |
| 14 | +#include <fcntl.h> |
| 15 | +#include <mqueue.h> |
| 16 | +#include <sys/epoll.h> |
| 17 | +#include <sys/eventfd.h> |
| 18 | +#include <sys/resource.h> |
| 19 | +#include <sys/stat.h> |
| 20 | +#include <ctime> |
| 21 | +#include <unistd.h> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include <string_view> |
| 25 | +#include <mutex> |
| 26 | +#include <cppcoro/coroutine.hpp> |
| 27 | +#include <functional> |
| 28 | + |
| 29 | +namespace cppcoro |
| 30 | +{ |
| 31 | + namespace detail |
| 32 | + { |
| 33 | + namespace lnx |
| 34 | + { |
| 35 | + using fd_t = int; |
| 36 | + |
| 37 | + class safe_fd |
| 38 | + { |
| 39 | + public: |
| 40 | + safe_fd() |
| 41 | + : m_fd(-1) |
| 42 | + { |
| 43 | + } |
| 44 | + |
| 45 | + explicit safe_fd(fd_t fd) |
| 46 | + : m_fd(fd) |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + safe_fd(const safe_fd& other) = delete; |
| 51 | + |
| 52 | + safe_fd(safe_fd&& other) noexcept |
| 53 | + : m_fd(other.m_fd) |
| 54 | + { |
| 55 | + other.m_fd = -1; |
| 56 | + } |
| 57 | + |
| 58 | + ~safe_fd() { close(); } |
| 59 | + |
| 60 | + safe_fd& operator=(safe_fd fd) noexcept |
| 61 | + { |
| 62 | + swap(fd); |
| 63 | + return *this; |
| 64 | + } |
| 65 | + |
| 66 | + constexpr fd_t fd() const { return m_fd; } |
| 67 | + constexpr fd_t handle() const { return m_fd; } |
| 68 | + |
| 69 | + /// Calls close() and sets the fd to -1. |
| 70 | + void close() noexcept; |
| 71 | + |
| 72 | + void swap(safe_fd& other) noexcept { std::swap(m_fd, other.m_fd); } |
| 73 | + |
| 74 | + /// Test operator |
| 75 | + explicit operator bool() noexcept { |
| 76 | + return m_fd >= 0; |
| 77 | + } |
| 78 | + |
| 79 | + /// Dereference operator |
| 80 | + int operator*() const noexcept { |
| 81 | + return fd(); |
| 82 | + } |
| 83 | + |
| 84 | + bool operator==(const safe_fd& other) const { return m_fd == other.m_fd; } |
| 85 | + |
| 86 | + bool operator!=(const safe_fd& other) const { return m_fd != other.m_fd; } |
| 87 | + |
| 88 | + bool operator==(fd_t fd) const { return m_fd == fd; } |
| 89 | + |
| 90 | + bool operator!=(fd_t fd) const { return m_fd != fd; } |
| 91 | + |
| 92 | + private: |
| 93 | + fd_t m_fd; |
| 94 | + }; |
| 95 | + |
| 96 | + struct io_message |
| 97 | + { |
| 98 | + std::function<void()> resume; |
| 99 | + int result = -1; |
| 100 | + |
| 101 | + io_message& operator=(coroutine_handle<> coroutine_handle) noexcept { |
| 102 | + resume = [coroutine_handle]() mutable { |
| 103 | + coroutine_handle.resume(); |
| 104 | + }; |
| 105 | + return *this; |
| 106 | + } |
| 107 | + io_message& operator=(std::function<void()> function) noexcept { |
| 108 | + resume = std::move(function); |
| 109 | + return *this; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + } // namespace linux |
| 114 | + |
| 115 | + using safe_handle = lnx::safe_fd; |
| 116 | + using dword_t = int; |
| 117 | + struct sock_buf { |
| 118 | + sock_buf(void *buf, size_t sz) : buffer(buf), size(sz) {} |
| 119 | + void * buffer; |
| 120 | + size_t size; |
| 121 | + }; |
| 122 | + using handle_t = lnx::fd_t; |
| 123 | + } // namespace detail |
| 124 | +} // namespace cppcoro |
| 125 | + |
| 126 | +#endif // CPPCORO_DETAIL_LINUX_HPP_INCLUDED |
0 commit comments