nlib
nn::nlib::threading::AsyncFileIo Class Referencefinal

Class that wraps asynchronous file I/O. More...

#include "nn/nlib/threading/AsyncFileIo.h"

Public Member Functions

 AsyncFileIo () noexcept
 Instantiates the object.
 
 ~AsyncFileIo () noexcept
 Destructor. Closes a file if it is open.
 
errno_t Init () noexcept
 Performs initialization. More...
 
NLIB_CHECK_RESULT errno_t Open (const char *path, int flags, int mode) noexcept
 Opens a file. More...
 
NLIB_CHECK_RESULT 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...
 

Static Public Member Functions

static bool HasNativeAsyncFileIo () noexcept
 Checks whether the system's asynchronous file I/O is used in an implementation. More...
 

Detailed Description

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::ASYNCFILEIO_READ) != 0) { error }
Future<size_t> readResult;
// The buffer or offset may require alignment.
if (io.Read(&readResult, buf, bufsize, 0, &ioservice) != 0) { error }
.... Perform other processes while reading.
if (readResult.Get(&readsize) != 0) { error }
// buf holds readSize bytes of data.

Definition at line 15 of file AsyncFileIo.h.

Member Function Documentation

§ Cancel()

nn::nlib::threading::AsyncFileIo::Cancel ( AsyncFileIoService ioservice)
noexcept

Cancels the current asynchronous file I/O running against the opened file.

Parameters
[in]ioservicePointer to the asynchronous file I/O control object.
Return values
0Success.
ENOTSUPThe system does not support this function.
EBADFIndicates that the file is not open.

§ Close()

nn::nlib::threading::AsyncFileIo::Close ( )
noexcept

Closes a file.

Return values
0Indicates that the file closed successfully.
EBADFIndicates that the file is not open.

§ FdOpen()

nn::nlib::threading::AsyncFileIo::FdOpen ( nlib_fd  fd)
noexcept

Opens a file using nlib_fd.

Parameters
[in]fdOpened nlib_fd.
Return values
0Success.
EALREADYIndicates 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 31 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]pathPath of the file to open.
[in]flagsFlags to be passed to nlib_fd_open().
[in]modeA permission argument to be passed to nlib_fd_open() (0644 if it is omitted).
Return values
0Indicates that the file opened successfully.
EIOIndicates that the file failed to open.

§ Read() [1/2]

nn::nlib::threading::AsyncFileIo::Read ( Future< size_t > *  future,
void *  buf,
size_t  nbytes,
nlib_offset  ofs,
AsyncFileIoService ioservice 
)
noexcept

Starts an asynchronous file read operation.

Parameters
[in,out]futurePointer to the Future used to store the result of the asynchronous file read operation.
[in,out]bufPointer to the buffer that data is read from.
[in]nbytesBuffer size.
[in]ofsStarting file read offset.
[in]ioservicePointer to the asynchronous file I/O control object.
Return values
0The asynchronous file read operation has started.
EINVALInvalid argument.
ENOMEMFailed to allocate memory.
Description
The system may impose limits on buffer space alignment or offset alignment.

§ Read() [2/2]

nn::nlib::threading::AsyncFileIo::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.

Parameters
[out]read_bytesThe number of bytes read.
[in,out]bufPointer to the buffer that data is read from.
[in]nbytesBuffer size.
[in]ofsStarting file read offset.
[in]ioservicePointer to the asynchronous file I/O control object.
Return values
0The file read operation was successful.
EINVALInvalid argument.
ENOMEMFailed to allocate memory.
Description
The system may impose limits on buffer space alignment or offset alignment.

§ Write() [1/2]

nn::nlib::threading::AsyncFileIo::Write ( Future< size_t > *  future,
const void *  buf,
size_t  nbytes,
nlib_offset  ofs,
AsyncFileIoService ioservice 
)
noexcept

Starts an asynchronous file write operation.

Parameters
[in,out]futurePointer to the Future used to store the result of the asynchronous file write operation.
[in]bufPointer to the data to write.
[in]nbytesBuffer size.
[in]ofsFile write starting offset.
[in]ioservicePointer to the asynchronous file I/O control object.
Return values
0The asynchronous file write operation has started.
EINVALInvalid argument.
ENOMEMFailed 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_bytesThe number of bytes written.
[in]bufPointer to the data to write.
[in]nbytesBuffer size.
[in]ofsFile write starting offset.
[in]ioservicePointer to the asynchronous file I/O control object.
Return values
0The asynchronous file write operation has started.
EINVALInvalid argument.
ENOMEMFailed 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: