SDL_sysasyncio.h 5.3 KB

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