1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#include "SDL_internal.h"
23
24#ifndef SDL_sysasyncio_h_
25#define SDL_sysasyncio_h_
26
27#if defined(SDL_PLATFORM_WINDOWS) && defined(NTDDI_WIN10_NI)
28#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && NTDDI_VERSION >= NTDDI_WIN10_NI
29#define HAVE_IORINGAPI_H
30#endif
31#endif
32
33// If your platform has an option other than the "generic" code, make sure this
34// is #defined to 0 instead and implement the SDL_SYS_* functions below in your
35// backend (having them maybe call into the SDL_SYS_*_Generic versions as a
36// fallback if the platform has functionality that isn't always available).
37#if defined(HAVE_LIBURING_H) || defined(HAVE_IORINGAPI_H)
38#define SDL_ASYNCIO_ONLY_HAVE_GENERIC 0
39#else
40#define SDL_ASYNCIO_ONLY_HAVE_GENERIC 1
41#endif
42
43// this entire thing is just juggling doubly-linked lists, so make some helper macros.
44#define LINKED_LIST_DECLARE_FIELDS(type, prefix) \
45 type *prefix##prev; \
46 type *prefix##next
47
48#define LINKED_LIST_PREPEND(item, list, prefix) do { \
49 item->prefix##prev = &list; \
50 item->prefix##next = list.prefix##next; \
51 if (item->prefix##next) { \
52 item->prefix##next->prefix##prev = item; \
53 } \
54 list.prefix##next = item; \
55} while (false)
56
57#define LINKED_LIST_UNLINK(item, prefix) do { \
58 if (item->prefix##next) { \
59 item->prefix##next->prefix##prev = item->prefix##prev; \
60 } \
61 item->prefix##prev->prefix##next = task->prefix##next; \
62 item->prefix##prev = item->prefix##next = NULL; \
63} while (false)
64
65#define LINKED_LIST_START(list, prefix) (list.prefix##next)
66#define LINKED_LIST_NEXT(item, prefix) (item->prefix##next)
67#define LINKED_LIST_PREV(item, prefix) (item->prefix##prev)
68
69typedef struct SDL_AsyncIOTask SDL_AsyncIOTask;
70
71struct SDL_AsyncIOTask
72{
73 SDL_AsyncIO *asyncio;
74 SDL_AsyncIOTaskType type;
75 SDL_AsyncIOQueue *queue;
76 Uint64 offset;
77 bool flush;
78 void *buffer;
79 char *error;
80 SDL_AsyncIOResult result;
81 Uint64 requested_size;
82 Uint64 result_size;
83 void *app_userdata;
84 LINKED_LIST_DECLARE_FIELDS(struct SDL_AsyncIOTask, asyncio);
85 LINKED_LIST_DECLARE_FIELDS(struct SDL_AsyncIOTask, queue); // the generic backend uses this, so I've added it here to avoid the extra allocation.
86 LINKED_LIST_DECLARE_FIELDS(struct SDL_AsyncIOTask, threadpool); // the generic backend uses this, so I've added it here to avoid the extra allocation.
87};
88
89typedef struct SDL_AsyncIOQueueInterface
90{
91 bool (*queue_task)(void *userdata, SDL_AsyncIOTask *task);
92 void (*cancel_task)(void *userdata, SDL_AsyncIOTask *task);
93 SDL_AsyncIOTask * (*get_results)(void *userdata);
94 SDL_AsyncIOTask * (*wait_results)(void *userdata, Sint32 timeoutMS);
95 void (*signal)(void *userdata);
96 void (*destroy)(void *userdata);
97} SDL_AsyncIOQueueInterface;
98
99struct SDL_AsyncIOQueue
100{
101 SDL_AsyncIOQueueInterface iface;
102 void *userdata;
103 SDL_AtomicInt tasks_inflight;
104};
105
106// this interface is kept per-object, even though generally it's going to decide
107// on a single interface that is the same for the entire process, but I've kept
108// the abstraction in case we start exposing more types of async i/o, like
109// sockets, in the future.
110typedef struct SDL_AsyncIOInterface
111{
112 Sint64 (*size)(void *userdata);
113 bool (*read)(void *userdata, SDL_AsyncIOTask *task);
114 bool (*write)(void *userdata, SDL_AsyncIOTask *task);
115 bool (*close)(void *userdata, SDL_AsyncIOTask *task);
116 void (*destroy)(void *userdata);
117} SDL_AsyncIOInterface;
118
119struct SDL_AsyncIO
120{
121 SDL_AsyncIOInterface iface;
122 void *userdata;
123 SDL_Mutex *lock;
124 SDL_AsyncIOTask tasks;
125 SDL_AsyncIOTask *closing; // The close task, which isn't queued until all pending work for this file is done.
126 bool oneshot; // true if this is a SDL_LoadFileAsync open.
127};
128
129// This is implemented for various platforms; param validation is done before calling this. Open file, fill in iface and userdata.
130extern bool SDL_SYS_AsyncIOFromFile(const char *file, const char *mode, SDL_AsyncIO *asyncio);
131
132// This is implemented for various platforms. Call SDL_OpenAsyncIOQueue from in here.
133extern bool SDL_SYS_CreateAsyncIOQueue(SDL_AsyncIOQueue *queue);
134
135// This is called during SDL_QuitAsyncIO, after all tasks have completed and all files are closed, to let the platform clean up global backend details.
136extern void SDL_SYS_QuitAsyncIO(void);
137
138// the "generic" version is always available, since it is almost always needed as a fallback even on platforms that might offer something better.
139extern bool SDL_SYS_AsyncIOFromFile_Generic(const char *file, const char *mode, SDL_AsyncIO *asyncio);
140extern bool SDL_SYS_CreateAsyncIOQueue_Generic(SDL_AsyncIOQueue *queue);
141extern void SDL_SYS_QuitAsyncIO_Generic(void);
142
143#endif
144
145