SDL  2.0
SDL_error_c.h File Reference
#include "./SDL_internal.h"
+ Include dependency graph for SDL_error_c.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_error
 

Macros

#define ERR_MAX_STRLEN   128
 

Functions

SDL_errorSDL_GetErrBuf (void)
 

Macro Definition Documentation

◆ ERR_MAX_STRLEN

#define ERR_MAX_STRLEN   128

Definition at line 30 of file SDL_error_c.h.

Function Documentation

◆ SDL_GetErrBuf()

SDL_error* SDL_GetErrBuf ( void  )

Definition at line 205 of file SDL_thread.c.

206 {
207 #if SDL_THREADS_DISABLED
208  /* Non-thread-safe global error variable */
209  static SDL_error SDL_global_error;
210  return &SDL_global_error;
211 #else
212  static SDL_SpinLock tls_lock;
213  static SDL_bool tls_being_created;
214  static SDL_TLSID tls_errbuf;
215  static SDL_error SDL_global_errbuf;
216  const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
217  SDL_error *errbuf;
218 
219  /* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
220  It also means it's possible for another thread to also use SDL_global_errbuf,
221  but that's very unlikely and hopefully won't cause issues.
222  */
223  if (!tls_errbuf && !tls_being_created) {
224  SDL_AtomicLock(&tls_lock);
225  if (!tls_errbuf) {
226  SDL_TLSID slot;
227  tls_being_created = SDL_TRUE;
228  slot = SDL_TLSCreate();
229  tls_being_created = SDL_FALSE;
231  tls_errbuf = slot;
232  }
233  SDL_AtomicUnlock(&tls_lock);
234  }
235  if (!tls_errbuf) {
236  return &SDL_global_errbuf;
237  }
238 
240  errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
241  if (errbuf == ALLOCATION_IN_PROGRESS) {
242  return &SDL_global_errbuf;
243  }
244  if (!errbuf) {
245  /* Mark that we're in the middle of allocating our buffer */
246  SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
247  errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
248  if (!errbuf) {
249  SDL_TLSSet(tls_errbuf, NULL, NULL);
250  return &SDL_global_errbuf;
251  }
252  SDL_zerop(errbuf);
253  SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
254  }
255  return errbuf;
256 #endif /* SDL_THREADS_DISABLED */
257 }
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:207
int SDL_SpinLock
Definition: SDL_atomic.h:89
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:208
#define SDL_AtomicLock
#define SDL_malloc
#define SDL_AtomicUnlock
#define SDL_free
SDL_bool
Definition: SDL_stdinc.h:168
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
#define SDL_zerop(x)
Definition: SDL_stdinc.h:427
SDL_TLSID SDL_TLSCreate()
Create an identifier that is globally visible to all threads but refers to data that is thread-specif...
Definition: SDL_thread.c:33
void * SDL_TLSGet(SDL_TLSID id)
Get the value associated with a thread local storage ID for the current thread.
Definition: SDL_thread.c:40
int SDL_TLSSet(SDL_TLSID id, const void *value, void(*destructor)(void *))
Set the value associated with a thread local storage ID for the current thread.
Definition: SDL_thread.c:52
unsigned int SDL_TLSID
Definition: SDL_thread.h:52
#define NULL
Definition: begin_code.h:163

References NULL, SDL_AtomicLock, SDL_AtomicUnlock, SDL_FALSE, SDL_free, SDL_malloc, SDL_MemoryBarrierAcquire, SDL_MemoryBarrierRelease, SDL_TLSCreate(), SDL_TLSGet(), SDL_TLSSet(), SDL_TRUE, and SDL_zerop.

Referenced by SDL_ClearError(), SDL_GetError(), SDL_GetErrorMsg(), and SDL_SetError().