Skip to content

Commit

Permalink
Document that video and input functions should be called on the main …
Browse files Browse the repository at this point in the history
…thread.

This is a hard requirement on Apple platforms and while most other platforms don't have a concept of main thread, all video and input functions should be called on the same thread.
  • Loading branch information
slouken committed Dec 5, 2024
1 parent fffa6c7 commit bc4185c
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 103 deletions.
22 changes: 11 additions & 11 deletions include/SDL3/SDL_clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C" {
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -68,7 +68,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardText(const char *text);
* SDL_GetError() for more information. This should be freed with
* SDL_free() when it is no longer needed.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -82,7 +82,7 @@ extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
*
* \returns true if the clipboard has text, or false if it does not.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -98,7 +98,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardText(void);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -117,7 +117,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetPrimarySelectionText(const char *text);
* failure; call SDL_GetError() for more information. This should be
* freed with SDL_free() when it is no longer needed.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -132,7 +132,7 @@ extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void);
*
* \returns true if the primary selection has text, or false if it does not.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand Down Expand Up @@ -199,7 +199,7 @@ typedef void (SDLCALL *SDL_ClipboardCleanupCallback)(void *userdata);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -215,7 +215,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetClipboardData(SDL_ClipboardDataCallback
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -235,7 +235,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_ClearClipboardData(void);
* for more information. This should be freed with SDL_free() when it
* is no longer needed.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -251,7 +251,7 @@ extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, s
* \returns true if there exists data in clipboard for the provided mime type,
* false if it does not.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -269,7 +269,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_HasClipboardData(const char *mime_type);
* failure; call SDL_GetError() for more information. This should be
* freed with SDL_free() when it is no longer needed.
*
* \threadsafety You may only call this function from the main thread.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand Down
16 changes: 4 additions & 12 deletions include/SDL3/SDL_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,7 @@ SDL_COMPILE_TIME_ASSERT(SDL_Event, sizeof(SDL_Event) == sizeof(((SDL_Event *)NUL
* polling or waiting for events (e.g. you are filtering them), then you must
* call SDL_PumpEvents() to force an event queue update.
*
* \threadsafety This should only be run in the thread that initialized the
* video subsystem, and for extra safety, you should consider
* only doing those things on the main thread in any case.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand Down Expand Up @@ -1234,9 +1232,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_FlushEvents(Uint32 minType, Uint32 maxType)
* the queue, or NULL.
* \returns true if this got an event or false if there are none available.
*
* \threadsafety This should only be run in the thread that initialized the
* video subsystem, and for extra safety, you should consider
* only doing those things on the main thread in any case.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand All @@ -1260,9 +1256,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_PollEvent(SDL_Event *event);
* \returns true on success or false if there was an error while waiting for
* events; call SDL_GetError() for more information.
*
* \threadsafety This should only be run in the thread that initialized the
* video subsystem, and for extra safety, you should consider
* only doing those things on the main thread in any case.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand Down Expand Up @@ -1292,9 +1286,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_WaitEvent(SDL_Event *event);
* \returns true if this got an event or false if the timeout elapsed without
* any events available.
*
* \threadsafety This should only be run in the thread that initialized the
* video subsystem, and for extra safety, you should consider
* only doing those things on the main thread in any case.
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
Expand Down
4 changes: 2 additions & 2 deletions include/SDL3/SDL_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extern "C" {
typedef Uint32 SDL_InitFlags;

#define SDL_INIT_AUDIO 0x00000010u /**< `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` */
#define SDL_INIT_VIDEO 0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS` */
#define SDL_INIT_VIDEO 0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS`, should be initialized on the main thread */
#define SDL_INIT_JOYSTICK 0x00000200u /**< `SDL_INIT_JOYSTICK` implies `SDL_INIT_EVENTS`, should be initialized on the same thread as SDL_INIT_VIDEO on Windows if you don't set SDL_HINT_JOYSTICK_THREAD */
#define SDL_INIT_HAPTIC 0x00001000u
#define SDL_INIT_GAMEPAD 0x00002000u /**< `SDL_INIT_GAMEPAD` implies `SDL_INIT_JOYSTICK` */
Expand Down Expand Up @@ -139,7 +139,7 @@ typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate, SDL_AppResult result);
* - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
* subsystem
* - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
* subsystem
* subsystem, should be initialized on the main thread.
* - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
* events subsystem
* - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
Expand Down
48 changes: 48 additions & 0 deletions include/SDL3/SDL_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef Uint32 SDL_KeyboardID;
*
* \returns true if a keyboard is connected, false otherwise.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyboards
Expand All @@ -81,6 +83,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_HasKeyboard(void);
* call SDL_GetError() for more information. This should be freed
* with SDL_free() when it is no longer needed.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyboardNameForID
Expand All @@ -97,6 +101,8 @@ extern SDL_DECLSPEC SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count);
* \returns the name of the selected keyboard or NULL on failure; call
* SDL_GetError() for more information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyboards
Expand All @@ -108,6 +114,8 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyboardNameForID(SDL_KeyboardID
*
* \returns the window with keyboard focus.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*/
extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
Expand Down Expand Up @@ -136,6 +144,8 @@ extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
* \param numkeys if non-NULL, receives the length of the returned array.
* \returns a pointer to an array of key states.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_PumpEvents
Expand All @@ -148,6 +158,8 @@ extern SDL_DECLSPEC const bool * SDLCALL SDL_GetKeyboardState(int *numkeys);
*
* This function will generate key up events for all pressed keys.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyboardState
Expand All @@ -160,6 +172,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
* \returns an OR'd combination of the modifier keys for the keyboard. See
* SDL_Keymod for details.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyboardState
Expand All @@ -180,6 +194,8 @@ extern SDL_DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
*
* \param modstate the desired SDL_Keymod for the keyboard.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetModState
Expand All @@ -201,6 +217,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
* \param key_event true if the keycode will be used in key events.
* \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyName
Expand All @@ -220,6 +238,8 @@ extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scan
* scancode generates this key, may be NULL.
* \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyFromScancode
Expand All @@ -237,6 +257,8 @@ extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key,
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetScancodeName
Expand All @@ -259,6 +281,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetScancodeName(SDL_Scancode scancode, cons
* \returns a pointer to the name for the scancode. If the scancode doesn't
* have a name this function returns an empty string ("").
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetScancodeFromKey
Expand All @@ -274,6 +298,8 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetScancodeName(SDL_Scancode scanco
* \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
* recognized; call SDL_GetError() for more information.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyFromName
Expand All @@ -290,6 +316,8 @@ extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *nam
* \param key the desired SDL_Keycode to query.
* \returns a UTF-8 encoded string of the key name.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyFromName
Expand All @@ -305,6 +333,8 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyName(SDL_Keycode key);
* \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
* SDL_GetError() for more information.
*
* \threadsafety This function is not thread safe.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetKeyFromScancode
Expand All @@ -330,6 +360,8 @@ extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_SetTextInputArea
Expand Down Expand Up @@ -423,6 +455,8 @@ typedef enum SDL_Capitalization
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_SetTextInputArea
Expand All @@ -444,6 +478,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_StartTextInputWithProperties(SDL_Window *wi
* \param window the window to check.
* \returns true if text input events are enabled else false.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_StartTextInput
Expand All @@ -460,6 +496,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_TextInputActive(SDL_Window *window);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_StartTextInput
Expand All @@ -473,6 +511,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_StopTextInput(SDL_Window *window);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_StartTextInput
Expand All @@ -494,6 +534,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_ClearComposition(SDL_Window *window);
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_GetTextInputArea
Expand All @@ -514,6 +556,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetTextInputArea(SDL_Window *window, const
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_SetTextInputArea
Expand All @@ -526,6 +570,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_GetTextInputArea(SDL_Window *window, SDL_Re
* \returns true if the platform has some screen keyboard support or false if
* not.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_StartTextInput
Expand All @@ -539,6 +585,8 @@ extern SDL_DECLSPEC bool SDLCALL SDL_HasScreenKeyboardSupport(void);
* \param window the window for which screen keyboard should be queried.
* \returns true if screen keyboard is shown or false if not.
*
* \threadsafety This function should only be called on the main thread.
*
* \since This function is available since SDL 3.1.3.
*
* \sa SDL_HasScreenKeyboardSupport
Expand Down
Loading

0 comments on commit bc4185c

Please sign in to comment.