WinRT: merged with SDL 2.0.0 codebase (aka. SDL hg rev d6a8fa507a45)

--HG--
rename : README.iOS => README-ios.txt
This commit is contained in:
David Ludwig 2013-08-12 22:29:55 -04:00
commit 88461d442a
768 changed files with 31941 additions and 111045 deletions

View file

@ -50,6 +50,12 @@ extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
*/
extern void SDL_SYS_WaitThread(SDL_Thread * thread);
/* Get the thread local storage for this thread */
extern SDL_TLSData *SDL_SYS_GetTLSData();
/* Set the thread local storage for this thread */
extern int SDL_SYS_SetTLSData(SDL_TLSData *data);
#endif /* _SDL_systhread_h */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -22,158 +22,227 @@
/* System independent thread management routines for SDL */
#include "SDL_mutex.h"
#include "SDL_thread.h"
#include "SDL_thread_c.h"
#include "SDL_systhread.h"
#include "../SDL_error_c.h"
#define ARRAY_CHUNKSIZE 32
/* The array of threads currently active in the application
(except the main thread)
The manipulation of an array here is safer than using a linked list.
SDL_TLSID
SDL_TLSCreate()
{
static SDL_atomic_t SDL_tls_id;
return SDL_AtomicIncRef(&SDL_tls_id)+1;
}
void *
SDL_TLSGet(SDL_TLSID id)
{
SDL_TLSData *storage;
storage = SDL_SYS_GetTLSData();
if (!storage || id == 0 || id > storage->limit) {
return NULL;
}
return storage->array[id-1].data;
}
int
SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void *))
{
SDL_TLSData *storage;
if (id == 0) {
return SDL_InvalidParamError("id");
}
storage = SDL_SYS_GetTLSData();
if (!storage || (id > storage->limit)) {
unsigned int i, oldlimit, newlimit;
oldlimit = storage ? storage->limit : 0;
newlimit = (id + TLS_ALLOC_CHUNKSIZE);
storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage)+(newlimit-1)*sizeof(storage->array[0]));
if (!storage) {
return SDL_OutOfMemory();
}
storage->limit = newlimit;
for (i = oldlimit; i < newlimit; ++i) {
storage->array[i].data = NULL;
storage->array[i].destructor = NULL;
}
if (SDL_SYS_SetTLSData(storage) != 0) {
return -1;
}
}
storage->array[id-1].data = SDL_const_cast(void*, value);
storage->array[id-1].destructor = destructor;
return 0;
}
static void
SDL_TLSCleanup()
{
SDL_TLSData *storage;
storage = SDL_SYS_GetTLSData();
if (storage) {
unsigned int i;
for (i = 0; i < storage->limit; ++i) {
if (storage->array[i].destructor) {
storage->array[i].destructor(storage->array[i].data);
}
}
SDL_SYS_SetTLSData(NULL);
SDL_free(storage);
}
}
/* This is a generic implementation of thread-local storage which doesn't
require additional OS support.
It is not especially efficient and doesn't clean up thread-local storage
as threads exit. If there is a real OS that doesn't support thread-local
storage this implementation should be improved to be production quality.
*/
static int SDL_maxthreads = 0;
static int SDL_numthreads = 0;
static SDL_Thread **SDL_Threads = NULL;
static SDL_mutex *thread_lock = NULL;
static int
SDL_ThreadsInit(void)
typedef struct SDL_TLSEntry {
SDL_threadID thread;
SDL_TLSData *storage;
struct SDL_TLSEntry *next;
} SDL_TLSEntry;
static SDL_mutex *SDL_generic_TLS_mutex;
static SDL_TLSEntry *SDL_generic_TLS;
SDL_TLSData *
SDL_Generic_GetTLSData()
{
int retval;
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *entry;
SDL_TLSData *storage = NULL;
retval = 0;
thread_lock = SDL_CreateMutex();
if (thread_lock == NULL) {
retval = -1;
}
return (retval);
}
/* This should never be called...
If this is called by SDL_Quit(), we don't know whether or not we should
clean up threads here. If any threads are still running after this call,
they will no longer have access to any per-thread data.
*/
#if 0
static void
SDL_ThreadsQuit(void)
{
SDL_mutex *mutex;
mutex = thread_lock;
thread_lock = NULL;
if (mutex != NULL) {
SDL_DestroyMutex(mutex);
}
}
#endif
/* Routines for manipulating the thread list */
static void
SDL_AddThread(SDL_Thread * thread)
{
/* WARNING:
If the very first threads are created simultaneously, then
there could be a race condition causing memory corruption.
In practice, this isn't a problem because by definition there
is only one thread running the first time this is called.
*/
if (!thread_lock) {
if (SDL_ThreadsInit() < 0) {
return;
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_LockMutex(thread_lock);
/* Expand the list of threads, if necessary */
#ifdef DEBUG_THREADS
printf("Adding thread (%d already - %d max)\n",
SDL_numthreads, SDL_maxthreads);
#endif
if (SDL_numthreads == SDL_maxthreads) {
SDL_Thread **threads;
threads = (SDL_Thread **) SDL_realloc(SDL_Threads,
(SDL_maxthreads +
ARRAY_CHUNKSIZE) *
(sizeof *threads));
if (threads == NULL) {
SDL_OutOfMemory();
goto done;
}
SDL_maxthreads += ARRAY_CHUNKSIZE;
SDL_Threads = threads;
}
SDL_Threads[SDL_numthreads++] = thread;
done:
SDL_mutexV(thread_lock);
}
static void
SDL_DelThread(SDL_Thread * thread)
{
int i;
if (!thread_lock) {
return;
}
SDL_LockMutex(thread_lock);
for (i = 0; i < SDL_numthreads; ++i) {
if (thread == SDL_Threads[i]) {
SDL_MemoryBarrierAcquire();
SDL_LockMutex(SDL_generic_TLS_mutex);
for (entry = SDL_generic_TLS; entry; entry = entry->next) {
if (entry->thread == thread) {
storage = entry->storage;
break;
}
}
if (i < SDL_numthreads) {
if (--SDL_numthreads > 0) {
while (i < SDL_numthreads) {
SDL_Threads[i] = SDL_Threads[i + 1];
++i;
}
} else {
SDL_maxthreads = 0;
SDL_free(SDL_Threads);
SDL_Threads = NULL;
}
#ifdef DEBUG_THREADS
printf("Deleting thread (%d left - %d max)\n",
SDL_numthreads, SDL_maxthreads);
#endif
}
SDL_mutexV(thread_lock);
SDL_UnlockMutex(SDL_generic_TLS_mutex);
#if 0 /* There could be memory corruption if another thread is starting */
if (SDL_Threads == NULL) {
SDL_ThreadsQuit();
}
#endif
return storage;
}
/* The default (non-thread-safe) global error variable */
static SDL_error SDL_global_error;
int
SDL_Generic_SetTLSData(SDL_TLSData *storage)
{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *prev, *entry;
/* 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) {
if (entry->thread == thread) {
if (storage) {
entry->storage = storage;
} else {
if (prev) {
prev->next = entry->next;
} else {
SDL_generic_TLS = entry->next;
}
SDL_free(entry);
}
break;
}
prev = entry;
}
if (!entry) {
entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
if (entry) {
entry->thread = thread;
entry->storage = storage;
entry->next = SDL_generic_TLS;
SDL_generic_TLS = entry;
}
}
SDL_UnlockMutex(SDL_generic_TLS_mutex);
if (!entry) {
return SDL_OutOfMemory();
}
return 0;
}
/* Routine to get the thread-specific error variable */
SDL_error *
SDL_GetErrBuf(void)
{
static SDL_SpinLock tls_lock;
static SDL_bool tls_being_created;
static SDL_TLSID tls_errbuf;
static SDL_error SDL_global_errbuf;
const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
SDL_error *errbuf;
errbuf = &SDL_global_error;
if (SDL_Threads) {
int i;
SDL_threadID this_thread;
this_thread = SDL_ThreadID();
SDL_LockMutex(thread_lock);
for (i = 0; i < SDL_numthreads; ++i) {
if (this_thread == SDL_Threads[i]->threadid) {
errbuf = &SDL_Threads[i]->errbuf;
break;
}
/* tls_being_created is there simply to prevent recursion if SDL_TLSCreate() fails.
It also means it's possible for another thread to also use SDL_global_errbuf,
but that's very unlikely and hopefully won't cause issues.
*/
if (!tls_errbuf && !tls_being_created) {
SDL_AtomicLock(&tls_lock);
if (!tls_errbuf) {
SDL_TLSID slot;
tls_being_created = SDL_TRUE;
slot = SDL_TLSCreate();
tls_being_created = SDL_FALSE;
SDL_MemoryBarrierRelease();
tls_errbuf = slot;
}
SDL_mutexV(thread_lock);
SDL_AtomicUnlock(&tls_lock);
}
return (errbuf);
if (!tls_errbuf) {
return &SDL_global_errbuf;
}
SDL_MemoryBarrierAcquire();
errbuf = (SDL_error *)SDL_TLSGet(tls_errbuf);
if (errbuf == ALLOCATION_IN_PROGRESS) {
return &SDL_global_errbuf;
}
if (!errbuf) {
/* Mark that we're in the middle of allocating our buffer */
SDL_TLSSet(tls_errbuf, ALLOCATION_IN_PROGRESS, NULL);
errbuf = (SDL_error *)SDL_malloc(sizeof(*errbuf));
if (!errbuf) {
SDL_TLSSet(tls_errbuf, NULL, NULL);
return &SDL_global_errbuf;
}
SDL_zerop(errbuf);
SDL_TLSSet(tls_errbuf, errbuf, SDL_free);
}
return errbuf;
}
@ -194,9 +263,7 @@ SDL_RunThread(void *data)
void *userdata = args->data;
int *statusloc = &args->info->status;
/* Perform any system-dependent setup
- this function cannot fail, and cannot use SDL_SetError()
*/
/* Perform any system-dependent setup - this function may not fail */
SDL_SYS_SetupThread(args->info->name);
/* Get the thread id */
@ -207,6 +274,9 @@ SDL_RunThread(void *data)
/* Run the function */
*statusloc = userfunc(userdata);
/* Clean up thread-local storage */
SDL_TLSCleanup();
}
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
@ -249,7 +319,9 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
args = (thread_args *) SDL_malloc(sizeof(*args));
if (args == NULL) {
SDL_OutOfMemory();
SDL_free(thread->name);
if (thread->name) {
SDL_free(thread->name);
}
SDL_free(thread);
return (NULL);
}
@ -258,15 +330,14 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
args->info = thread;
args->wait = SDL_CreateSemaphore(0);
if (args->wait == NULL) {
SDL_free(thread->name);
if (thread->name) {
SDL_free(thread->name);
}
SDL_free(thread);
SDL_free(args);
return (NULL);
}
/* Add the thread to the list of available threads */
SDL_AddThread(thread);
/* Create the thread and go! */
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, pfnEndThread);
@ -278,8 +349,9 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
SDL_SemWait(args->wait);
} else {
/* Oops, failed. Gotta free everything */
SDL_DelThread(thread);
SDL_free(thread->name);
if (thread->name) {
SDL_free(thread->name);
}
SDL_free(thread);
thread = NULL;
}
@ -306,7 +378,11 @@ SDL_GetThreadID(SDL_Thread * thread)
const char *
SDL_GetThreadName(SDL_Thread * thread)
{
return thread->name;
if (thread) {
return thread->name;
} else {
return NULL;
}
}
int
@ -323,8 +399,9 @@ SDL_WaitThread(SDL_Thread * thread, int *status)
if (status) {
*status = thread->status;
}
SDL_DelThread(thread);
SDL_free(thread->name);
if (thread->name) {
SDL_free(thread->name);
}
SDL_free(thread);
}
}

View file

@ -58,6 +58,30 @@ struct SDL_Thread
/* This is the function called to run a thread */
extern void SDL_RunThread(void *data);
/* This is the system-independent thread local storage structure */
typedef struct {
unsigned int limit;
struct {
void *data;
void (*destructor)(void*);
} array[1];
} SDL_TLSData;
/* This is how many TLS entries we allocate at once */
#define TLS_ALLOC_CHUNKSIZE 4
/* 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 SDL_TLSData *SDL_Generic_GetTLSData();
/* 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);
#endif /* _SDL_thread_c_h */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -0,0 +1,70 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include "SDL_thread.h"
#include "../SDL_thread_c.h"
#if SDL_THREAD_BEOS
#include <support/TLS.h>
static int32 thread_local_storage = B_NO_MEMORY;
static SDL_bool generic_local_storage = SDL_FALSE;
SDL_TLSData *
SDL_SYS_GetTLSData()
{
if (thread_local_storage == B_NO_MEMORY && !generic_local_storage) {
static SDL_SpinLock lock;
SDL_AtomicLock(&lock);
if (thread_local_storage == B_NO_MEMORY && !generic_local_storage) {
int32 storage = tls_allocate();
if (storage != B_NO_MEMORY) {
SDL_MemoryBarrierRelease();
thread_local_storage = storage;
} else {
generic_local_storage = SDL_TRUE;
}
}
SDL_AtomicUnlock(&lock);
}
if (generic_local_storage) {
return SDL_Generic_GetTLSData();
}
SDL_MemoryBarrierAcquire();
return (SDL_TLSData *)tls_get(thread_local_storage);
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
{
if (generic_local_storage) {
return SDL_Generic_SetTLSData(data);
}
tls_set(thread_local_storage, data);
return 0;
}
#endif /* SDL_THREAD_BEOS */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -187,7 +187,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
the signaler can race ahead and get the condition semaphore
if we are stopped between the mutex unlock and semaphore wait,
giving a deadlock. See the following URL for details:
http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
http://web.archive.org/web/20010914175514/http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html#Workshop
*/
SDL_LockMutex(cond->lock);
if (cond->signals > 0) {

View file

@ -32,7 +32,7 @@
SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_SetError("SDL not configured with thread support");
SDL_SetError("SDL not built with thread support");
return (SDL_sem *) 0;
}
@ -44,19 +44,19 @@ SDL_DestroySemaphore(SDL_sem * sem)
int
SDL_SemTryWait(SDL_sem * sem)
{
return SDL_SetError("SDL not configured with thread support");
return SDL_SetError("SDL not built with thread support");
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
{
return SDL_SetError("SDL not configured with thread support");
return SDL_SetError("SDL not built with thread support");
}
int
SDL_SemWait(SDL_sem * sem)
{
return SDL_SetError("SDL not configured with thread support");
return SDL_SetError("SDL not built with thread support");
}
Uint32
@ -68,7 +68,7 @@ SDL_SemValue(SDL_sem * sem)
int
SDL_SemPost(SDL_sem * sem)
{
return SDL_SetError("SDL not configured with thread support");
return SDL_SetError("SDL not built with thread support");
}
#else

View file

@ -25,8 +25,15 @@
#include "SDL_thread.h"
#include "../SDL_systhread.h"
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
#else
int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
{
return SDL_SetError("Threads are not supported on this platform");
}

View file

@ -0,0 +1,38 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include "../SDL_thread_c.h"
SDL_TLSData *
SDL_SYS_GetTLSData()
{
return SDL_Generic_GetTLSData();
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
{
return SDL_Generic_SetTLSData(data);
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -31,42 +31,42 @@
#include <pspkerror.h>
struct SDL_semaphore {
SceUID semid;
SceUID semid;
};
/* Create a semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
SDL_sem *sem;
sem = (SDL_sem *) malloc(sizeof(*sem));
if (sem != NULL) {
/* TODO: Figure out the limit on the maximum value. */
sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
if (sem->semid < 0) {
SDL_SetError("Couldn't create semaphore");
free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
sem = (SDL_sem *) malloc(sizeof(*sem));
if (sem != NULL) {
/* TODO: Figure out the limit on the maximum value. */
sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
if (sem->semid < 0) {
SDL_SetError("Couldn't create semaphore");
free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;
return sem;
}
/* Free the semaphore */
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem != NULL) {
if (sem->semid > 0) {
sceKernelDeleteSema(sem->semid);
sem->semid = 0;
}
if (sem != NULL) {
if (sem->semid > 0) {
sceKernelDeleteSema(sem->semid);
sem->semid = 0;
}
free(sem);
}
free(sem);
}
}
/* TODO: This routine is a bit overloaded.
@ -75,30 +75,30 @@ void SDL_DestroySemaphore(SDL_sem *sem)
* is specified, convert it to microseconds. */
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
Uint32 *pTimeout;
Uint32 *pTimeout;
unsigned int res;
if (sem == NULL) {
SDL_SetError("Passed a NULL sem");
return 0;
}
if (sem == NULL) {
SDL_SetError("Passed a NULL sem");
return 0;
}
if (timeout == 0) {
res = sceKernelPollSema(sem->semid, 1);
if (res < 0) {
return SDL_MUTEX_TIMEDOUT;
}
return 0;
}
if (timeout == 0) {
res = sceKernelPollSema(sem->semid, 1);
if (res < 0) {
return SDL_MUTEX_TIMEDOUT;
}
return 0;
}
if (timeout == SDL_MUTEX_MAXWAIT) {
pTimeout = NULL;
} else {
timeout *= 1000; /* Convert to microseconds. */
pTimeout = &timeout;
}
if (timeout == SDL_MUTEX_MAXWAIT) {
pTimeout = NULL;
} else {
timeout *= 1000; /* Convert to microseconds. */
pTimeout = &timeout;
}
res = sceKernelWaitSema(sem->semid, 1, pTimeout);
res = sceKernelWaitSema(sem->semid, 1, pTimeout);
switch (res) {
case SCE_KERNEL_ERROR_OK:
return 0;
@ -106,50 +106,50 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
return SDL_MUTEX_TIMEDOUT;
default:
return SDL_SetError("WaitForSingleObject() failed");
}
}
}
int SDL_SemTryWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, 0);
return SDL_SemWaitTimeout(sem, 0);
}
int SDL_SemWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
/* Returns the current count of the semaphore */
Uint32 SDL_SemValue(SDL_sem *sem)
{
SceKernelSemaInfo info;
SceKernelSemaInfo info;
if (sem == NULL) {
SDL_SetError("Passed a NULL sem");
return 0;
}
if (sem == NULL) {
SDL_SetError("Passed a NULL sem");
return 0;
}
if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) {
return info.currentCount;
}
if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) {
return info.currentCount;
}
return 0;
return 0;
}
int SDL_SemPost(SDL_sem *sem)
{
int res;
int res;
if (sem == NULL) {
return SDL_SetError("Passed a NULL sem");
}
if (sem == NULL) {
return SDL_SetError("Passed a NULL sem");
}
res = sceKernelSignalSema(sem->semid, 1);
if (res < 0) {
return SDL_SetError("sceKernelSignalSema() failed");
}
res = sceKernelSignalSema(sem->semid, 1);
if (res < 0) {
return SDL_SetError("sceKernelSignalSema() failed");
}
return 0;
return 0;
}
/* vim: ts=4 sw=4

View file

@ -35,57 +35,57 @@
static int ThreadEntry(SceSize args, void *argp)
{
SDL_RunThread(*(void **) argp);
return 0;
SDL_RunThread(*(void **) argp);
return 0;
}
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
{
SceKernelThreadInfo status;
int priority = 32;
SceKernelThreadInfo status;
int priority = 32;
/* Set priority of new thread to the same as the current thread */
status.size = sizeof(SceKernelThreadInfo);
if (sceKernelReferThreadStatus(sceKernelGetThreadId(), &status) == 0) {
priority = status.currentPriority;
}
/* Set priority of new thread to the same as the current thread */
status.size = sizeof(SceKernelThreadInfo);
if (sceKernelReferThreadStatus(sceKernelGetThreadId(), &status) == 0) {
priority = status.currentPriority;
}
thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
priority, 0x8000,
PSP_THREAD_ATTR_VFPU, NULL);
if (thread->handle < 0) {
return SDL_SetError("sceKernelCreateThread() failed");
}
thread->handle = sceKernelCreateThread("SDL thread", ThreadEntry,
priority, 0x8000,
PSP_THREAD_ATTR_VFPU, NULL);
if (thread->handle < 0) {
return SDL_SetError("sceKernelCreateThread() failed");
}
sceKernelStartThread(thread->handle, 4, &args);
return 0;
sceKernelStartThread(thread->handle, 4, &args);
return 0;
}
void SDL_SYS_SetupThread(const char *name)
{
/* Do nothing. */
/* Do nothing. */
}
SDL_threadID SDL_ThreadID(void)
{
return (SDL_threadID) sceKernelGetThreadId();
return (SDL_threadID) sceKernelGetThreadId();
}
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
sceKernelWaitThreadEnd(thread->handle, NULL);
sceKernelDeleteThread(thread->handle);
sceKernelWaitThreadEnd(thread->handle, NULL);
sceKernelDeleteThread(thread->handle);
}
void SDL_SYS_KillThread(SDL_Thread *thread)
{
sceKernelTerminateDeleteThread(thread->handle);
{
sceKernelTerminateDeleteThread(thread->handle);
}
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
{
int value;
if (priority == SDL_THREAD_PRIORITY_LOW) {
value = 19;
} else if (priority == SDL_THREAD_PRIORITY_HIGH) {
@ -93,7 +93,7 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
} else {
value = 0;
}
return sceKernelChangeThreadPriority(sceKernelGetThreadId(),value);
}

View file

@ -34,7 +34,7 @@
#include <sys/resource.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif // __LINUX__
#endif /* __LINUX__ */
#if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
#include <dlfcn.h>

View file

@ -0,0 +1,69 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include "SDL_thread.h"
#include "../SDL_thread_c.h"
#include <pthread.h>
#define INVALID_PTHREAD_KEY ((pthread_key_t)-1)
static pthread_key_t thread_local_storage = INVALID_PTHREAD_KEY;
static SDL_bool generic_local_storage = SDL_FALSE;
SDL_TLSData *
SDL_SYS_GetTLSData()
{
if (thread_local_storage == INVALID_PTHREAD_KEY && !generic_local_storage) {
static SDL_SpinLock lock;
SDL_AtomicLock(&lock);
if (thread_local_storage == INVALID_PTHREAD_KEY && !generic_local_storage) {
pthread_key_t storage;
if (pthread_key_create(&storage, NULL) == 0) {
SDL_MemoryBarrierRelease();
thread_local_storage = storage;
} else {
generic_local_storage = SDL_TRUE;
}
}
SDL_AtomicUnlock(&lock);
}
if (generic_local_storage) {
return SDL_Generic_GetTLSData();
}
SDL_MemoryBarrierAcquire();
return (SDL_TLSData *)pthread_getspecific(thread_local_storage);
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
{
if (generic_local_storage) {
return SDL_Generic_SetTLSData(data);
}
if (pthread_setspecific(thread_local_storage, data) != 0) {
return SDL_SetError("pthread_setspecific() failed");
}
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -36,7 +36,7 @@
/* Cygwin gcc-3 ... MingW64 (even with a i386 host) does this like MSVC. */
#if (defined(__MINGW32__) && (__GNUC__ < 4))
typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
unsigned (__stdcall *func)(void *), void *arg,
unsigned (__stdcall *func)(void *), void *arg,
unsigned, unsigned *threadID);
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
@ -106,6 +106,12 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
{
#elif defined(__CYGWIN__)
int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
{
pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
pfnSDL_CurrentEndThread pfnEndThread = NULL;
#else
int
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
@ -118,9 +124,9 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
if (!pThreadParms) {
return SDL_OutOfMemory();
}
// Save the function which we will have to call to clear the RTL of calling app!
/* Save the function which we will have to call to clear the RTL of calling app! */
pThreadParms->pfnCurrentEndThread = pfnEndThread;
// Also save the real parameters we have to pass to thread function
/* Also save the real parameters we have to pass to thread function */
pThreadParms->args = args;
if (pfnBeginThread) {

View file

@ -0,0 +1,72 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include "SDL_thread.h"
#include "../SDL_thread_c.h"
#if SDL_THREAD_WINDOWS
#include "../../core/windows/SDL_windows.h"
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES;
static SDL_bool generic_local_storage = SDL_FALSE;
SDL_TLSData *
SDL_SYS_GetTLSData()
{
if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
static SDL_SpinLock lock;
SDL_AtomicLock(&lock);
if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) {
DWORD storage = TlsAlloc();
if (storage != TLS_OUT_OF_INDEXES) {
SDL_MemoryBarrierRelease();
thread_local_storage = storage;
} else {
generic_local_storage = SDL_TRUE;
}
}
SDL_AtomicUnlock(&lock);
}
if (generic_local_storage) {
return SDL_Generic_GetTLSData();
}
SDL_MemoryBarrierAcquire();
return (SDL_TLSData *)TlsGetValue(thread_local_storage);
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
{
if (generic_local_storage) {
return SDL_Generic_SetTLSData(data);
}
if (!TlsSetValue(thread_local_storage, data)) {
return SDL_SetError("TlsSetValue() failed");
}
return 0;
}
#endif /* SDL_THREAD_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */