Class that wraps asynchronous file I/O.
More...
#include "nn/nlib/threading/AsyncFileIo.h"
|
| AsyncFileIo () noexcept |
| Instantiates the object.
|
|
| ~AsyncFileIo () noexcept |
| Destructor. Closes a file if it is open.
|
|
errno_t | Init () noexcept |
| Performs initialization. More...
|
|
errno_t | Open (const char *path, int flags, int mode) noexcept |
| Opens a file. More...
|
|
errno_t | FdOpen (nlib_fd fd) noexcept |
| Opens a file using nlib_fd . More...
|
|
nlib_fd | GetFd () const noexcept |
| Gets nlib_fd . More...
|
|
errno_t | Close () noexcept |
| Closes a file. More...
|
|
errno_t | Read (Future< size_t > *future, void *buf, size_t nbytes, nlib_offset ofs, AsyncFileIoService *ioservice) noexcept |
| Starts an asynchronous file read operation. More...
|
|
errno_t | Read (size_t *read_bytes, void *buf, size_t nbytes, nlib_offset ofs, AsyncFileIoService *ioservice) noexcept |
| Reads files. Waits internally for an asynchronous read operation. More...
|
|
errno_t | Write (Future< size_t > *future, const void *buf, size_t nbytes, nlib_offset ofs, AsyncFileIoService *ioservice) noexcept |
| Starts an asynchronous file write operation. More...
|
|
errno_t | Write (size_t *write_bytes, const void *buf, size_t nbytes, nlib_offset ofs, AsyncFileIoService *ioservice) noexcept |
| Writes to file. Waits internally for an asynchronous write operation. More...
|
|
errno_t | Cancel (AsyncFileIoService *ioservice) noexcept |
| Cancels the current asynchronous file I/O running against the opened file. More...
|
|
Class that wraps asynchronous file I/O.
- Description
- This class makes asynchronous file I/O easier to handle by using
Future
for notification. This class may be implemented in a manner that allows different threads to issue synchronous I/O and have it seem asynchronous.
- This class is currently implemented in Win32 and Linux using native asynchronous I/O.
SIGUSR1
is used in Linux, and a handler is set in AsyncFileIoService::Init
.
- An example of asynchronous I/O read code is shown below.
.....
AsyncFileIoService ioservice;
if (ioservice.Init() != 0) { error }
.....
AsyncFileIo io;
if (io.Init() != 0) { error }
if (io.Open("file.bin", AsyncFileIo::kAsyncFileIoRead) != 0) { error }
Future<size_t> readResult;
if (io.Read(&readResult, buf, bufsize, 0, &ioservice) != 0) { error }
.... Perform other processes while reading.
if (readResult.Get(&readsize) != 0) { error }
Definition at line 28 of file AsyncFileIo.h.
◆ Cancel()
Cancels the current asynchronous file I/O running against the opened file.
- Parameters
-
[in] | ioservice | Pointer to the asynchronous file I/O control object. |
- Return values
-
0 | Success. |
ENOTSUP | The system does not support this function. |
EBADF | Indicates that the file is not open. |
◆ Close()
nn::nlib::threading::AsyncFileIo::Close |
( |
| ) |
|
|
noexcept |
Closes a file.
- Return values
-
0 | Indicates that the file closed successfully. |
EBADF | Indicates that the file is not open. |
◆ FdOpen()
nn::nlib::threading::AsyncFileIo::FdOpen |
( |
nlib_fd |
fd | ) |
|
|
noexcept |
Opens a file using nlib_fd
.
- Parameters
-
- Return values
-
0 | Success. |
EALREADY | Indicates that the file is already open. |
◆ GetFd()
nn::nlib::threading::AsyncFileIo::GetFd |
( |
| ) |
const |
|
inlinenoexcept |
Gets nlib_fd
.
- Returns
- Returns the
nlib_fd
value held by the object.
Definition at line 46 of file AsyncFileIo.h.
◆ HasNativeAsyncFileIo()
nn::nlib::threading::AsyncFileIo::HasNativeAsyncFileIo |
( |
| ) |
|
|
staticnoexcept |
Checks whether the system's asynchronous file I/O is used in an implementation.
- Returns
- Returns
true
if asynchronous file I/O is used in an implementation.
◆ Init()
nn::nlib::threading::AsyncFileIo::Init |
( |
| ) |
|
|
noexcept |
Performs initialization.
- Returns
- Returns
0
on success.
◆ Open()
nn::nlib::threading::AsyncFileIo::Open |
( |
const char * |
path, |
|
|
int |
flags, |
|
|
int |
mode |
|
) |
| |
|
noexcept |
Opens a file.
- Parameters
-
[in] | path | Path of the file to open. |
[in] | flags | Flags to be passed to nlib_fd_open() . |
[in] | mode | A permission argument to be passed to nlib_fd_open() (0644 if it is omitted). |
- Return values
-
0 | Indicates that the file opened successfully. |
EIO | Indicates that the file failed to open. |
◆ Read() [1/2]
Starts an asynchronous file read operation.
- Parameters
-
[in,out] | future | Pointer to the Future used to store the result of the asynchronous file read operation. |
[in,out] | buf | Pointer to the buffer that data is read from. |
[in] | nbytes | Buffer size. |
[in] | ofs | Starting file read offset. |
[in] | ioservice | Pointer to the asynchronous file I/O control object. |
- Return values
-
0 | The asynchronous file read operation has started. |
EINVAL | Invalid argument. |
ENOMEM | Failed to allocate memory. |
- Description
- The system may impose limits on buffer space alignment or offset alignment.
◆ Read() [2/2]
Reads files. Waits internally for an asynchronous read operation.
- Parameters
-
[out] | read_bytes | The number of bytes read. |
[in,out] | buf | Pointer to the buffer that data is read from. |
[in] | nbytes | Buffer size. |
[in] | ofs | Starting file read offset. |
[in] | ioservice | Pointer to the asynchronous file I/O control object. |
- Return values
-
0 | The file read operation was successful. |
EINVAL | Invalid argument. |
ENOMEM | Failed to allocate memory. |
- Description
- The system may impose limits on buffer space alignment or offset alignment.
◆ Write() [1/2]
Starts an asynchronous file write operation.
- Parameters
-
[in,out] | future | Pointer to the Future used to store the result of the asynchronous file write operation. |
[in] | buf | Pointer to the data to write. |
[in] | nbytes | Buffer size. |
[in] | ofs | File write starting offset. |
[in] | ioservice | Pointer to the asynchronous file I/O control object. |
- Return values
-
0 | The asynchronous file write operation has started. |
EINVAL | Invalid argument. |
ENOMEM | Failed to allocate memory. |
- Description
- The system may impose limits on buffer space alignment or offset alignment.
◆ Write() [2/2]
nn::nlib::threading::AsyncFileIo::Write |
( |
size_t * |
write_bytes, |
|
|
const void * |
buf, |
|
|
size_t |
nbytes, |
|
|
nlib_offset |
ofs, |
|
|
AsyncFileIoService * |
ioservice |
|
) |
| |
|
noexcept |
Writes to file. Waits internally for an asynchronous write operation.
- Parameters
-
[out] | write_bytes | The number of bytes written. |
[in] | buf | Pointer to the data to write. |
[in] | nbytes | Buffer size. |
[in] | ofs | File write starting offset. |
[in] | ioservice | Pointer to the asynchronous file I/O control object. |
- Return values
-
0 | The asynchronous file write operation has started. |
EINVAL | Invalid argument. |
ENOMEM | Failed to allocate memory. |
- Description
- The system may impose limits on buffer space alignment or offset alignment.
The documentation for this class was generated from the following files: