Path: blob/master/thirdparty/sdl/thread/pthread/SDL_systls.c
10279 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"21#include "../SDL_systhread.h"22#include "../SDL_thread_c.h"2324#include <pthread.h>2526#define INVALID_PTHREAD_KEY ((pthread_key_t)-1)2728static pthread_key_t thread_local_storage = INVALID_PTHREAD_KEY;29static bool generic_local_storage = false;3031void SDL_SYS_InitTLSData(void)32{33if (thread_local_storage == INVALID_PTHREAD_KEY && !generic_local_storage) {34if (pthread_key_create(&thread_local_storage, NULL) != 0) {35thread_local_storage = INVALID_PTHREAD_KEY;36SDL_Generic_InitTLSData();37generic_local_storage = true;38}39}40}4142SDL_TLSData *SDL_SYS_GetTLSData(void)43{44if (generic_local_storage) {45return SDL_Generic_GetTLSData();46}4748if (thread_local_storage != INVALID_PTHREAD_KEY) {49return (SDL_TLSData *)pthread_getspecific(thread_local_storage);50}51return NULL;52}5354bool SDL_SYS_SetTLSData(SDL_TLSData *data)55{56if (generic_local_storage) {57return SDL_Generic_SetTLSData(data);58}5960if (pthread_setspecific(thread_local_storage, data) != 0) {61return SDL_SetError("pthread_setspecific() failed");62}63return true;64}6566void SDL_SYS_QuitTLSData(void)67{68if (generic_local_storage) {69SDL_Generic_QuitTLSData();70generic_local_storage = false;71} else {72if (thread_local_storage != INVALID_PTHREAD_KEY) {73pthread_key_delete(thread_local_storage);74thread_local_storage = INVALID_PTHREAD_KEY;75}76}77}787980