Skip to content

Commit 294b8bf

Browse files
committed
Merge 'Garcia6l20/cppcoro/master': linux support using liburing
lewissbaker#169
2 parents 7cc9433 + e1d53e6 commit 294b8bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2229
-553
lines changed

.github/workflows/cmake.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: CMake
22

3-
on: [push, pull_request]
3+
on:
4+
- push
5+
- pull_request
46

57
env:
68
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)

cmake/Findliburing.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
find_path(LIBURING_INCLUDE_DIR liburing.h
2+
PATH_SUFFIXES liburing)
3+
4+
find_library(LIBURING_LIBRARY NAMES uring)
5+
6+
include(FindPackageHandleStandardArgs)
7+
8+
find_package_handle_standard_args(liburing DEFAULT_MSG
9+
LIBURING_LIBRARY LIBURING_INCLUDE_DIR)
10+
11+
mark_as_advanced(LIBURING_LIBRARY LIBURING_INCLUDE_DIR)
12+
13+
add_library(liburing::liburing INTERFACE IMPORTED GLOBAL)
14+
target_link_libraries(liburing::liburing INTERFACE ${LIBURING_LIBRARY})
15+
target_include_directories(liburing::liburing INTERFACE ${LIBURING_INCLUDE_DIR})

include/cppcoro/detail/linux.hpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)