Skip to content

Commit

Permalink
MorphOS: rollback new thread test
Browse files Browse the repository at this point in the history
With that sdl2.library crash directly.... need to check why.
  • Loading branch information
BeWorld2018 committed Sep 16, 2024
1 parent 2604019 commit 4bf5653
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 125 deletions.
56 changes: 23 additions & 33 deletions src/SDL.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#endif
#if defined(__OS2__)
#include "core/os2/SDL_os2.h"
#ifdef SDL_THREAD_OS2
#include "thread/os2/SDL_systls_c.h"
#endif
#endif

/* this checks for HAVE_DBUS_DBUS_H internally. */
Expand All @@ -49,7 +52,6 @@
#include "haptic/SDL_haptic_c.h"
#include "joystick/SDL_joystick_c.h"
#include "sensor/SDL_sensor_c.h"
#include "thread/SDL_thread_c.h"

/* Initialization/Cleanup routines */
#ifndef SDL_TIMERS_DISABLED
Expand Down Expand Up @@ -117,7 +119,6 @@ static SDL_bool SDL_MainIsReady = SDL_FALSE;
#else
static SDL_bool SDL_MainIsReady = SDL_TRUE;
#endif
static SDL_bool SDL_main_thread_initialized = SDL_FALSE;
static SDL_bool SDL_bInMainQuit = SDL_FALSE;
static Uint8 SDL_SubsystemRefCount[32];

Expand Down Expand Up @@ -183,36 +184,6 @@ void SDL_SetMainReady(void)
SDL_MainIsReady = SDL_TRUE;
}

void SDL_InitMainThread(void)
{
if (SDL_main_thread_initialized) {
return;
}

SDL_InitTLSData();
#ifndef SDL_TIMERS_DISABLED
SDL_TicksInit();
#endif
SDL_LogInit();

SDL_main_thread_initialized = SDL_TRUE;
}

static void SDL_QuitMainThread(void)
{
if (!SDL_main_thread_initialized) {
return;
}

SDL_LogQuit();
#ifndef SDL_TIMERS_DISABLED
SDL_TicksQuit();
#endif
SDL_QuitTLSData();

SDL_main_thread_initialized = SDL_FALSE;
}

int SDL_InitSubSystem(Uint32 flags)
{
Uint32 flags_initialized = 0;
Expand All @@ -221,13 +192,19 @@ int SDL_InitSubSystem(Uint32 flags)
return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
}

SDL_LogInit();

/* Clear the error message */
SDL_ClearError();

#ifdef SDL_USE_LIBDBUS
SDL_DBus_Init();
#endif

#ifdef SDL_THREAD_OS2
SDL_OS2TLSAlloc(); /* thread/os2/SDL_systls.c */
#endif

#ifdef SDL_VIDEO_DRIVER_WINDOWS
if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
if (SDL_HelperWindowCreate() < 0) {
Expand All @@ -236,6 +213,10 @@ int SDL_InitSubSystem(Uint32 flags)
}
#endif

#ifndef SDL_TIMERS_DISABLED
SDL_TicksInit();
#endif

/* Initialize the event subsystem */
if (flags & SDL_INIT_EVENTS) {
#ifndef SDL_EVENTS_DISABLED
Expand Down Expand Up @@ -400,6 +381,9 @@ int SDL_Init(Uint32 flags)
void SDL_QuitSubSystem(Uint32 flags)
{
#if defined(__OS2__)
#ifdef SDL_THREAD_OS2
SDL_OS2TLSFree(); /* thread/os2/SDL_systls.c */
#endif
SDL_OS2Quit();
#endif

Expand Down Expand Up @@ -523,19 +507,25 @@ void SDL_Quit(void)
#endif
SDL_QuitSubSystem(SDL_INIT_EVERYTHING);

#ifndef SDL_TIMERS_DISABLED
SDL_TicksQuit();
#endif

#ifdef SDL_USE_LIBDBUS
SDL_DBus_Quit();
#endif

SDL_ClearHints();
SDL_AssertionsQuit();

SDL_LogQuit();

/* Now that every subsystem has been quit, we reset the subsystem refcount
* and the list of initialized subsystems.
*/
SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));

SDL_QuitMainThread();
SDL_TLSCleanup();

SDL_bInMainQuit = SDL_FALSE;
}
Expand Down
2 changes: 0 additions & 2 deletions src/SDL_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@
#include "SDL_assert.h"
#include "SDL_log.h"

extern void SDL_InitMainThread(void);

#endif /* SDL_internal_h_ */

/* vi: set ts=4 sw=4 expandtab: */
6 changes: 0 additions & 6 deletions src/thread/SDL_systhread.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ extern void SDL_SYS_WaitThread(SDL_Thread *thread);
/* Mark thread as cleaned up as soon as it exits, without joining. */
extern void SDL_SYS_DetachThread(SDL_Thread *thread);

/* Initialize the global TLS data */
extern void SDL_SYS_InitTLSData(void);

/* Get the thread local storage for this thread */
extern SDL_TLSData *SDL_SYS_GetTLSData(void);

/* Set the thread local storage for this thread */
extern int SDL_SYS_SetTLSData(SDL_TLSData *data);

/* Quit the global TLS data */
extern void SDL_SYS_QuitTLSData(void);

/* This is for internal SDL use, so we don't need #ifdefs everywhere. */
extern SDL_Thread *
SDL_CreateThreadInternal(int(SDLCALL *fn)(void *), const char *name,
Expand Down
93 changes: 25 additions & 68 deletions src/thread/SDL_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@
#include "SDL_hints.h"
#include "../SDL_error_c.h"

/* The storage is local to the thread, but the IDs are global for the process */

static SDL_atomic_t SDL_tls_allocated;

void SDL_InitTLSData(void)
{
SDL_SYS_InitTLSData();
}

SDL_TLSID SDL_TLSCreate(void)
{
static SDL_atomic_t SDL_tls_id;
Expand All @@ -62,13 +53,6 @@ int SDL_TLSSet(SDL_TLSID id, const void *value, void(SDLCALL *destructor)(void *
return SDL_InvalidParamError("id");
}

/* Make sure TLS is initialized.
* There's a race condition here if you are calling this from non-SDL threads
* and haven't called SDL_Init() on your main thread, but such is life.
*/
SDL_InitTLSData();

/* Get the storage for the current thread */
storage = SDL_SYS_GetTLSData();
if (!storage || (id > storage->limit)) {
unsigned int i, oldlimit, newlimit;
Expand All @@ -85,10 +69,8 @@ int SDL_TLSSet(SDL_TLSID id, const void *value, void(SDLCALL *destructor)(void *
storage->array[i].destructor = NULL;
}
if (SDL_SYS_SetTLSData(storage) != 0) {
SDL_free(storage);
return -1;
}
SDL_AtomicIncRef(&SDL_tls_allocated);
}

storage->array[id - 1].data = SDL_const_cast(void *, value);
Expand All @@ -100,7 +82,6 @@ void SDL_TLSCleanup(void)
{
SDL_TLSData *storage;

/* Cleanup the storage for the current thread */
storage = SDL_SYS_GetTLSData();
if (storage) {
unsigned int i;
Expand All @@ -111,18 +92,6 @@ void SDL_TLSCleanup(void)
}
SDL_SYS_SetTLSData(NULL);
SDL_free(storage);
(void)SDL_AtomicDecRef(&SDL_tls_allocated);
}
}

void SDL_QuitTLSData(void)
{
SDL_TLSCleanup();

if (SDL_AtomicGet(&SDL_tls_allocated) == 0) {
SDL_SYS_QuitTLSData();
} else {
/* Some thread hasn't called SDL_CleanupTLS() */
}
}

Expand All @@ -144,27 +113,40 @@ typedef struct SDL_TLSEntry
static SDL_mutex *SDL_generic_TLS_mutex;
static SDL_TLSEntry *SDL_generic_TLS;

void SDL_Generic_InitTLSData(void)
{
if (!SDL_generic_TLS_mutex) {
SDL_generic_TLS_mutex = SDL_CreateMutex();
}
}

SDL_TLSData *SDL_Generic_GetTLSData(void)
{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *entry;
SDL_TLSData *storage = NULL;

#ifndef SDL_THREADS_DISABLED
if (!SDL_generic_TLS_mutex) {
static SDL_SpinLock tls_lock;
SDL_AtomicLock(&tls_lock);
if (!SDL_generic_TLS_mutex) {
SDL_mutex *mutex = SDL_CreateMutex();
SDL_MemoryBarrierRelease();
SDL_generic_TLS_mutex = mutex;
if (!SDL_generic_TLS_mutex) {
SDL_AtomicUnlock(&tls_lock);
return NULL;
}
}
SDL_AtomicUnlock(&tls_lock);
}
SDL_MemoryBarrierAcquire();
SDL_LockMutex(SDL_generic_TLS_mutex);
#endif /* SDL_THREADS_DISABLED */

for (entry = SDL_generic_TLS; entry; entry = entry->next) {
if (entry->thread == thread) {
storage = entry->storage;
break;
}
}
#ifndef SDL_THREADS_DISABLED
SDL_UnlockMutex(SDL_generic_TLS_mutex);
#endif

return storage;
}
Expand All @@ -173,8 +155,8 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *prev, *entry;
int retval = 0;

/* SDL_Generic_GetTLSData() is always called first, so we can assume SDL_generic_TLS_mutex */
SDL_LockMutex(SDL_generic_TLS_mutex);
prev = NULL;
for (entry = SDL_generic_TLS; entry; entry = entry->next) {
Expand All @@ -193,44 +175,21 @@ int SDL_Generic_SetTLSData(SDL_TLSData *data)
}
prev = entry;
}
if (!entry && data) {
if (!entry) {
entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
if (entry) {
entry->thread = thread;
entry->storage = data;
entry->next = SDL_generic_TLS;
SDL_generic_TLS = entry;
} else {
retval = SDL_OutOfMemory();
}
}
SDL_UnlockMutex(SDL_generic_TLS_mutex);

return retval;
}

void SDL_Generic_QuitTLSData(void)
{
SDL_TLSEntry *entry;

/* This should have been cleaned up by the time we get here */
SDL_assert(!SDL_generic_TLS);
if (SDL_generic_TLS) {
SDL_LockMutex(SDL_generic_TLS_mutex);
for (entry = SDL_generic_TLS; entry; ) {
SDL_TLSEntry *next = entry->next;
SDL_free(entry->storage);
SDL_free(entry);
entry = next;
}
SDL_generic_TLS = NULL;
SDL_UnlockMutex(SDL_generic_TLS_mutex);
}

if (SDL_generic_TLS_mutex) {
SDL_DestroyMutex(SDL_generic_TLS_mutex);
SDL_generic_TLS_mutex = NULL;
if (!entry) {
return SDL_OutOfMemory();
}
return 0;
}

/* Non-thread-safe global error variable */
Expand Down Expand Up @@ -369,8 +328,6 @@ SDL_Thread *SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
SDL_Thread *thread;
int ret;

SDL_InitMainThread();

/* Allocate memory for the thread info structure */
thread = (SDL_Thread *)SDL_calloc(1, sizeof(*thread));
if (!thread) {
Expand Down
12 changes: 6 additions & 6 deletions src/thread/SDL_thread_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ typedef struct
/* This is how many TLS entries we allocate at once */
#define TLS_ALLOC_CHUNKSIZE 4

extern void SDL_InitTLSData(void);
extern void SDL_QuitTLSData(void);

/* Generic TLS support.
/* Get cross-platform, slow, thread local storage for this thread.
This is only intended as a fallback if getting real thread-local
storage fails or isn't supported on this platform.
*/
extern void SDL_Generic_InitTLSData(void);
extern SDL_TLSData *SDL_Generic_GetTLSData(void);

/* Set cross-platform, slow, thread local storage for this thread.
This is only intended as a fallback if getting real thread-local
storage fails or isn't supported on this platform.
*/
extern int SDL_Generic_SetTLSData(SDL_TLSData *data);
extern void SDL_Generic_QuitTLSData(void);

#endif /* SDL_thread_c_h_ */

Expand Down
10 changes: 0 additions & 10 deletions src/thread/generic/SDL_systls.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
#include "../../SDL_internal.h"
#include "../SDL_thread_c.h"

void SDL_SYS_InitTLSData(void)
{
SDL_Generic_InitTLSData();
}

SDL_TLSData *SDL_SYS_GetTLSData(void)
{
return SDL_Generic_GetTLSData();
Expand All @@ -37,9 +32,4 @@ int SDL_SYS_SetTLSData(SDL_TLSData *data)
return SDL_Generic_SetTLSData(data);
}

void SDL_SYS_QuitTLSData(void)
{
SDL_Generic_QuitTLSData();
}

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 4bf5653

Please sign in to comment.