Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File I/O for Linux #160

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions include/cppcoro/detail/io_uring_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) GIG <[email protected]>
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef IO_URING_CONTEXT_HPP_INCLUDED
#define IO_URING_CONTEXT_HPP_INCLUDED

#include <cppcoro/config.hpp>
#include <cppcoro/detail/linux.hpp>

#include <mutex>

struct io_uring_sqe;
struct io_uring_cqe;

namespace cppcoro
{
namespace detail
{
namespace linux
{
class io_uring_context
{
public:

io_uring_context(std::uint32_t concurrencyHint);
~io_uring_context();

bool submit_one(const io_uring_sqe& sqe);
bool get_single_event(io_uring_cqe& cqe, bool waitForEvent);

private:

safe_file_descriptor m_ringFd;

std::mutex m_sqMutex;
std::mutex m_cqMutex;

struct io_sq_ring
{
void* ringPtr;
std::size_t ringSize;
unsigned* head;
unsigned* tail;
unsigned* ringMask;
unsigned* ringEntries;
unsigned* flags;
unsigned* array;
io_uring_sqe* sqes;
} m_sqRing;

struct io_cq_ring
{
void* ringPtr;
std::size_t ringSize;
unsigned* head;
unsigned* tail;
unsigned* ringMask;
unsigned* ringEntries;
io_uring_cqe* cqes;
} m_cqRing;

};

struct safe_file_data
{
safe_file_descriptor fd;
io_uring_context* aioContext;
};
}
}
}

#endif
104 changes: 104 additions & 0 deletions include/cppcoro/detail/linux.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_DETAIL_LINUX_HPP_INCLUDED
#define CPPCORO_DETAIL_LINUX_HPP_INCLUDED

#include <cppcoro/config.hpp>

#if !CPPCORO_OS_LINUX
# error <cppcoro/detail/linux.hpp> is only supported on the Linux platform.
#endif

#include <utility>
#include <cstdint>

namespace cppcoro
{
namespace detail
{
namespace linux
{
struct io_state
{
using callback_type = void(
io_state* state,
std::int32_t res);

io_state(callback_type* callback = nullptr) noexcept
: m_callback(callback)
{}

callback_type* m_callback;
};

class safe_file_descriptor
{
public:
safe_file_descriptor()
: m_fd(-1)
{}

explicit safe_file_descriptor(int fd)
: m_fd(fd)
{}

safe_file_descriptor(const safe_file_descriptor& other) = delete;

safe_file_descriptor(safe_file_descriptor&& other) noexcept
: m_fd(other.m_fd)
{
other.m_fd = -1;
}

~safe_file_descriptor()
{
close();
}

safe_file_descriptor& operator=(safe_file_descriptor fd) noexcept
{
swap(fd);
return *this;
}

constexpr int get() const { return m_fd; }

void close() noexcept;

void swap(safe_file_descriptor& other) noexcept
{
std::swap(m_fd, other.m_fd);
}

bool operator==(const safe_file_descriptor& other) const
{
return m_fd == other.m_fd;
}

bool operator!=(const safe_file_descriptor& other) const
{
return m_fd != other.m_fd;
}

bool operator==(int fd) const
{
return m_fd == fd;
}

bool operator!=(int fd) const
{
return m_fd != fd;
}

private:

int m_fd;

};
}
}
}

#endif
Loading