2017-07-10 16:26:24 +02:00
|
|
|
/* Copyright (C) 2011 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
2016-11-23 14:02:58 +01:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
2016-11-23 14:02:58 +01:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 18:14:48 +02:00
|
|
|
#include "precompiled.h"
|
2010-03-01 15:52:58 +01:00
|
|
|
#include "lib/file/io/io.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
#include "lib/sysdep/rtl.h"
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-05-05 15:03:34 +02:00
|
|
|
static const StatusDefinition ioStatusDefinitions[] = {
|
|
|
|
{ ERR::IO, L"Error during IO", EIO }
|
|
|
|
};
|
|
|
|
STATUS_ADD_DEFINITIONS(ioStatusDefinitions);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
namespace io {
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-30 17:41:19 +02:00
|
|
|
// this is just a thin wrapper on top of lowio and POSIX aio.
|
|
|
|
// note that the Windows aio implementation requires buffers, sizes and
|
|
|
|
// offsets to be sector-aligned.
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status Issue(aiocb& cb, size_t queueDepth)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
#if CONFIG2_FILE_ENABLE_AIO
|
|
|
|
if(queueDepth > 1)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
const int ret = (cb.aio_lio_opcode == LIO_WRITE)? aio_write(&cb): aio_read(&cb);
|
2011-05-03 14:38:42 +02:00
|
|
|
if(ret != 0)
|
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2011-04-29 21:10:34 +02:00
|
|
|
else
|
2011-04-29 22:38:13 +02:00
|
|
|
#else
|
|
|
|
UNUSED2(queueDepth);
|
2011-04-29 21:10:34 +02:00
|
|
|
#endif
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(lseek(cb.aio_fildes, cb.aio_offset, SEEK_SET) == cb.aio_offset);
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
void* buf = (void*)cb.aio_buf; // cast from volatile void*
|
|
|
|
const ssize_t bytesTransferred = (cb.aio_lio_opcode == LIO_WRITE)? write(cb.aio_fildes, buf, cb.aio_nbytes) : read(cb.aio_fildes, buf, cb.aio_nbytes);
|
|
|
|
if(bytesTransferred < 0)
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-04-18 18:14:48 +02:00
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
cb.aio_nbytes = (size_t)bytesTransferred;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
return INFO::OK;
|
|
|
|
}
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status WaitUntilComplete(aiocb& cb, size_t queueDepth)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
#if CONFIG2_FILE_ENABLE_AIO
|
|
|
|
if(queueDepth > 1)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
aiocb* const cbs = &cb;
|
|
|
|
timespec* const timeout = 0; // infinite
|
|
|
|
SUSPEND_AGAIN:
|
|
|
|
errno = 0;
|
|
|
|
const int ret = aio_suspend(&cbs, 1, timeout);
|
|
|
|
if(ret != 0)
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
if(errno == EINTR) // interrupted by signal
|
|
|
|
goto SUSPEND_AGAIN;
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
const int err = aio_error(&cb);
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(err != EINPROGRESS); // else aio_return is undefined
|
2011-04-29 21:10:34 +02:00
|
|
|
ssize_t bytesTransferred = aio_return(&cb);
|
|
|
|
if(bytesTransferred == -1) // transfer failed
|
2009-04-18 18:14:48 +02:00
|
|
|
{
|
2011-04-29 21:10:34 +02:00
|
|
|
errno = err;
|
2011-05-03 14:38:42 +02:00
|
|
|
WARN_RETURN(StatusFromErrno());
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2011-04-29 21:10:34 +02:00
|
|
|
cb.aio_nbytes = (size_t)bytesTransferred;
|
2009-04-18 18:14:48 +02:00
|
|
|
}
|
2011-04-29 22:38:13 +02:00
|
|
|
#else
|
|
|
|
UNUSED2(cb);
|
|
|
|
UNUSED2(queueDepth);
|
2011-04-29 21:10:34 +02:00
|
|
|
#endif
|
2009-04-18 18:14:48 +02:00
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2011-04-29 21:10:34 +02:00
|
|
|
} // namespace io
|