kabufuda/include/kabufuda/AsyncIO.hpp

61 lines
1.4 KiB
C++
Raw Normal View History

2018-10-06 20:41:40 -07:00
#pragma once
2018-02-06 01:34:01 -08:00
#ifndef _WIN32
2020-10-20 21:45:02 -07:00
#ifdef __SWITCH__
#include <sys/types.h>
using SizeReturn = ssize_t;
#else
2018-02-06 01:34:01 -08:00
#include <aio.h>
using SizeReturn = ssize_t;
2020-10-20 21:45:02 -07:00
#endif
2018-02-06 01:34:01 -08:00
#else
using SizeReturn = unsigned long;
2018-02-06 01:34:01 -08:00
#endif
#include <cstddef>
2018-02-06 01:34:01 -08:00
#include <vector>
#include "kabufuda/Util.hpp"
2018-12-07 21:20:24 -08:00
namespace kabufuda {
#if _WIN32
struct AsyncIOInner;
#endif
2018-02-06 01:34:01 -08:00
2018-12-07 21:20:24 -08:00
class AsyncIO {
2020-10-20 21:45:02 -07:00
#ifdef __SWITCH__
FILE* m_fd;
#elif !defined(_WIN32)
2018-12-07 21:20:24 -08:00
int m_fd = -1;
std::vector<std::pair<struct aiocb, SizeReturn>> m_queue;
2018-02-06 01:34:01 -08:00
#else
AsyncIOInner* m_inner;
2018-02-06 01:34:01 -08:00
#endif
2018-12-07 21:20:24 -08:00
void _waitForOperation(size_t qIdx) const;
mutable size_t m_maxBlock = 0;
2018-12-07 21:20:24 -08:00
2018-02-06 01:34:01 -08:00
public:
2018-12-07 21:20:24 -08:00
AsyncIO() = default;
2021-06-28 15:58:22 -07:00
explicit AsyncIO(std::string_view filename, bool truncate = false);
2018-12-07 21:20:24 -08:00
~AsyncIO();
AsyncIO(AsyncIO&& other);
AsyncIO& operator=(AsyncIO&& other);
AsyncIO(const AsyncIO* other) = delete;
AsyncIO& operator=(const AsyncIO& other) = delete;
void resizeQueue(size_t queueSz);
2018-12-07 21:20:24 -08:00
bool asyncRead(size_t qIdx, void* buf, size_t length, off_t offset);
bool asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset);
ECardResult pollStatus(size_t qIdx, SizeReturn* szRet = nullptr) const;
ECardResult pollStatus() const;
void waitForCompletion() const;
2020-10-20 21:45:02 -07:00
#ifdef __SWITCH__
explicit operator bool() const { return m_fd != nullptr; }
#elif !defined(_WIN32)
explicit operator bool() const { return m_fd != -1; }
2018-02-06 17:36:51 -08:00
#else
explicit operator bool() const;
2018-02-06 17:36:51 -08:00
#endif
2018-02-06 01:34:01 -08:00
};
2018-12-07 21:20:24 -08:00
} // namespace kabufuda