asyncio: Allow file closes to request a flush/fsyncdata.

This commit is contained in:
Ryan C. Gordon
2024-10-06 02:00:45 -04:00
parent 022a6a9f97
commit 3ca629ceb5
7 changed files with 70 additions and 11 deletions

View File

@@ -1224,7 +1224,7 @@ SDL_DYNAPI_PROC(SDL_AsyncIO*,SDL_AsyncIOFromFile,(const char *a, const char *b),
SDL_DYNAPI_PROC(Sint64,SDL_GetAsyncIOSize,(SDL_AsyncIO *a),(a),return)
SDL_DYNAPI_PROC(SDL_AsyncIOTask*,SDL_ReadAsyncIO,(SDL_AsyncIO *a, void *b, Uint64 c, Uint64 d, SDL_AsyncIOQueue *e, void *f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(SDL_AsyncIOTask*,SDL_WriteAsyncIO,(SDL_AsyncIO *a, void *b, Uint64 c, Uint64 d, SDL_AsyncIOQueue *e, void *f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(SDL_AsyncIOTask*,SDL_CloseAsyncIO,(SDL_AsyncIO *a, SDL_AsyncIOQueue *b, void *c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_AsyncIOTask*,SDL_CloseAsyncIO,(SDL_AsyncIO *a, bool b, SDL_AsyncIOQueue *c, void *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_AsyncIOQueue*,SDL_CreateAsyncIOQueue,(void),(),return)
SDL_DYNAPI_PROC(void,SDL_DestroyAsyncIOQueue,(SDL_AsyncIOQueue *a),(a),)
SDL_DYNAPI_PROC(bool,SDL_GetAsyncIOResult,(SDL_AsyncIOQueue *a, SDL_AsyncIOOutcome *b),(a,b),return)

View File

@@ -143,7 +143,7 @@ SDL_AsyncIOTask *SDL_WriteAsyncIO(SDL_AsyncIO *asyncio, void *ptr, Uint64 offset
return RequestAsyncIO(false, asyncio, ptr, offset, size, queue, userdata);
}
SDL_AsyncIOTask *SDL_CloseAsyncIO(SDL_AsyncIO *asyncio, SDL_AsyncIOQueue *queue, void *userdata)
SDL_AsyncIOTask *SDL_CloseAsyncIO(SDL_AsyncIO *asyncio, bool flush, SDL_AsyncIOQueue *queue, void *userdata)
{
if (!asyncio) {
SDL_InvalidParamError("asyncio");
@@ -166,6 +166,7 @@ SDL_AsyncIOTask *SDL_CloseAsyncIO(SDL_AsyncIO *asyncio, SDL_AsyncIOQueue *queue,
task->type = SDL_ASYNCIO_TASK_CLOSE;
task->app_userdata = userdata;
task->queue = queue;
task->flush = flush;
asyncio->closing = task;
@@ -328,7 +329,7 @@ SDL_AsyncIOTask *SDL_LoadFileAsync(const char *file, SDL_AsyncIOQueue *queue, vo
SDL_free(ptr);
}
SDL_CloseAsyncIO(asyncio, queue, userdata); // if this fails, we'll have a resource leak, but this would already be a dramatic system failure.
SDL_CloseAsyncIO(asyncio, false, queue, userdata); // if this fails, we'll have a resource leak, but this would already be a dramatic system failure.
}
return task;

View File

@@ -68,6 +68,7 @@ struct SDL_AsyncIOTask
SDL_AsyncIOTaskType type;
SDL_AsyncIOQueue *queue;
Uint64 offset;
bool flush;
void *buffer;
char *error;
SDL_AsyncIOResult result;

View File

@@ -76,7 +76,12 @@ static void SynchronousIO(SDL_AsyncIOTask *task)
// files will still run in parallel. An app can also open the same file twice to avoid this.
SDL_LockMutex(data->lock);
if (task->type == SDL_ASYNCIO_TASK_CLOSE) {
task->result = SDL_CloseIO(data->io) ? SDL_ASYNCIO_COMPLETE : SDL_ASYNCIO_FAILURE;
bool okay = true;
if (task->flush) {
okay = SDL_FlushIO(data->io);
}
okay = SDL_CloseIO(data->io) && okay;
task->result = okay ? SDL_ASYNCIO_COMPLETE : SDL_ASYNCIO_FAILURE;
} else if (SDL_SeekIO(io, (Sint64) task->offset, SDL_IO_SEEK_SET) < 0) {
task->result = SDL_ASYNCIO_FAILURE;
} else {

View File

@@ -58,6 +58,7 @@ static void *liburing_handle = NULL;
SDL_LIBURING_FUNC(void, io_uring_prep_fsync, (struct io_uring_sqe *sqe, int fd, unsigned fsync_flags)) \
SDL_LIBURING_FUNC(void, io_uring_prep_cancel, (struct io_uring_sqe *sqe, void *user_data, int flags)) \
SDL_LIBURING_FUNC(void, io_uring_prep_timeout, (struct io_uring_sqe *sqe, struct __kernel_timespec *ts, unsigned count, unsigned flags)) \
SDL_LIBURING_FUNC(void, io_uring_prep_nop, (struct io_uring_sqe *sqe)) \
SDL_LIBURING_FUNC(void, io_uring_sqe_set_data, (struct io_uring_sqe *sqe, void *data)) \
SDL_LIBURING_FUNC(void, io_uring_sqe_set_flags, (struct io_uring_sqe *sqe, unsigned flags)) \
SDL_LIBURING_FUNC(int, io_uring_submit, (struct io_uring *ring)) \
@@ -211,7 +212,7 @@ static SDL_AsyncIOTask *ProcessCQE(LibUringAsyncIOQueueData *queuedata, struct i
}
SDL_AsyncIOTask *task = (SDL_AsyncIOTask *) io_uring_cqe_get_data(cqe);
if (task) { // can be NULL if this was just a wakeup message, etc.
if (task) { // can be NULL if this was just a wakeup message, a NOP, etc.
if (!task->queue) { // We leave `queue` blank to signify this was a task cancellation.
SDL_AsyncIOTask *cancel_task = task;
task = (SDL_AsyncIOTask *) cancel_task->app_userdata;
@@ -227,13 +228,19 @@ static SDL_AsyncIOTask *ProcessCQE(LibUringAsyncIOQueueData *queuedata, struct i
} else {
if ((task->type == SDL_ASYNCIO_TASK_WRITE) && (((Uint64) cqe->res) < task->requested_size)) {
task->result = SDL_ASYNCIO_FAILURE; // it's always a failure on short writes.
} else {
task->result = SDL_ASYNCIO_COMPLETE;
}
// don't explicitly mark it as COMPLETE; that's the default value and a linked task might have failed in an earlier operation and this would overwrite it.
if ((task->type == SDL_ASYNCIO_TASK_READ) || (task->type == SDL_ASYNCIO_TASK_WRITE)) {
task->result_size = (Uint64) cqe->res;
}
}
if ((task->type == SDL_ASYNCIO_TASK_CLOSE) && task->flush) {
task->flush = false;
task = NULL; // don't return this one, it's a linked task, so it'll arrive in a later CQE.
}
}
return task;
@@ -358,6 +365,7 @@ static bool SDL_SYS_CreateAsyncIOQueue_liburing(SDL_AsyncIOQueue *queue)
static bool liburing_asyncio_read(void *userdata, SDL_AsyncIOTask *task)
{
LibUringAsyncIOQueueData *queuedata = (LibUringAsyncIOQueueData *) task->queue->userdata;
const int fd = (int) (size_t) userdata;
// !!! FIXME: `unsigned` is likely smaller than requested_size's Uint64. If we overflow it, we could try submitting multiple SQEs
// !!! FIXME: and make a note in the task that there are several in sequence.
@@ -372,7 +380,7 @@ static bool liburing_asyncio_read(void *userdata, SDL_AsyncIOTask *task)
return SDL_SetError("io_uring: submission queue is full");
}
liburing.io_uring_prep_read(sqe, (int) (size_t) userdata, task->buffer, (unsigned) task->requested_size, task->offset);
liburing.io_uring_prep_read(sqe, fd, task->buffer, (unsigned) task->requested_size, task->offset);
liburing.io_uring_sqe_set_data(sqe, task);
const bool retval = task->queue->iface.queue_task(task->queue->userdata, task);
@@ -383,6 +391,7 @@ static bool liburing_asyncio_read(void *userdata, SDL_AsyncIOTask *task)
static bool liburing_asyncio_write(void *userdata, SDL_AsyncIOTask *task)
{
LibUringAsyncIOQueueData *queuedata = (LibUringAsyncIOQueueData *) task->queue->userdata;
const int fd = (int) (size_t) userdata;
// !!! FIXME: `unsigned` is likely smaller than requested_size's Uint64. If we overflow it, we could try submitting multiple SQEs
// !!! FIXME: and make a note in the task that there are several in sequence.
@@ -397,7 +406,7 @@ static bool liburing_asyncio_write(void *userdata, SDL_AsyncIOTask *task)
return SDL_SetError("io_uring: submission queue is full");
}
liburing.io_uring_prep_write(sqe, (int) (size_t) userdata, task->buffer, (unsigned) task->requested_size, task->offset);
liburing.io_uring_prep_write(sqe, fd, task->buffer, (unsigned) task->requested_size, task->offset);
liburing.io_uring_sqe_set_data(sqe, task);
const bool retval = task->queue->iface.queue_task(task->queue->userdata, task);
@@ -408,6 +417,7 @@ static bool liburing_asyncio_write(void *userdata, SDL_AsyncIOTask *task)
static bool liburing_asyncio_close(void *userdata, SDL_AsyncIOTask *task)
{
LibUringAsyncIOQueueData *queuedata = (LibUringAsyncIOQueueData *) task->queue->userdata;
const int fd = (int) (size_t) userdata;
// have to hold a lock because otherwise two threads could get_sqe and submit while one request isn't fully set up.
SDL_LockMutex(queuedata->sqe_lock);
@@ -416,7 +426,22 @@ static bool liburing_asyncio_close(void *userdata, SDL_AsyncIOTask *task)
return SDL_SetError("io_uring: submission queue is full");
}
liburing.io_uring_prep_close(sqe, (int) (size_t) userdata);
if (task->flush) {
struct io_uring_sqe *flush_sqe = sqe;
sqe = liburing.io_uring_get_sqe(&queuedata->ring); // this will be our actual close task.
if (!sqe) {
liburing.io_uring_prep_nop(flush_sqe); // we already have the first sqe, just make it a NOP.
liburing.io_uring_sqe_set_data(flush_sqe, NULL);
task->queue->iface.queue_task(task->queue->userdata, task);
return SDL_SetError("io_uring: submission queue is full");
}
liburing.io_uring_prep_fsync(flush_sqe, fd, IORING_FSYNC_DATASYNC);
liburing.io_uring_sqe_set_data(flush_sqe, task);
liburing.io_uring_sqe_set_flags(flush_sqe, IOSQE_IO_HARDLINK); // must complete before next sqe starts, and next sqe should run even if this fails.
}
liburing.io_uring_prep_close(sqe, fd);
liburing.io_uring_sqe_set_data(sqe, task);
const bool retval = task->queue->iface.queue_task(task->queue->userdata, task);