WinRT: merged with latest, official, SDL 2.x sources (at rev. bea100d73d13)
This commit is contained in:
commit
41ce3814e2
289 changed files with 10187 additions and 10275 deletions
|
@ -85,7 +85,7 @@ SDL_AddThread(SDL_Thread * thread)
|
|||
return;
|
||||
}
|
||||
}
|
||||
SDL_mutexP(thread_lock);
|
||||
SDL_LockMutex(thread_lock);
|
||||
|
||||
/* Expand the list of threads, if necessary */
|
||||
#ifdef DEBUG_THREADS
|
||||
|
@ -118,7 +118,7 @@ SDL_DelThread(SDL_Thread * thread)
|
|||
if (!thread_lock) {
|
||||
return;
|
||||
}
|
||||
SDL_mutexP(thread_lock);
|
||||
SDL_LockMutex(thread_lock);
|
||||
for (i = 0; i < SDL_numthreads; ++i) {
|
||||
if (thread == SDL_Threads[i]) {
|
||||
break;
|
||||
|
@ -164,7 +164,7 @@ SDL_GetErrBuf(void)
|
|||
SDL_threadID this_thread;
|
||||
|
||||
this_thread = SDL_ThreadID();
|
||||
SDL_mutexP(thread_lock);
|
||||
SDL_LockMutex(thread_lock);
|
||||
for (i = 0; i < SDL_numthreads; ++i) {
|
||||
if (this_thread == SDL_Threads[i]->threadid) {
|
||||
errbuf = &SDL_Threads[i]->errbuf;
|
||||
|
|
|
@ -34,8 +34,8 @@
|
|||
#include "pthread/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_WINDOWS
|
||||
#include "windows/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_NDS
|
||||
#include "nds/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_PSP
|
||||
#include "psp/SDL_systhread_c.h"
|
||||
#elif SDL_THREAD_STDCPP
|
||||
#include "stdcpp/SDL_systhread_c.h"
|
||||
#else
|
||||
|
|
|
@ -73,8 +73,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
tryagain:
|
||||
|
@ -97,8 +96,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
break;
|
||||
default:
|
||||
SDL_SetError("acquire_sem() failed");
|
||||
retval = -1;
|
||||
retval = SDL_SetError("acquire_sem() failed");
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -139,13 +137,11 @@ int
|
|||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
if (release_sem(sem->id) != B_NO_ERROR) {
|
||||
SDL_SetError("release_sem() failed");
|
||||
return -1;
|
||||
return SDL_SetError("release_sem() failed");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -77,8 +77,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
thread->handle = spawn_thread(RunThread, name, B_NORMAL_PRIORITY, args);
|
||||
if ((thread->handle == B_NO_MORE_THREADS) ||
|
||||
(thread->handle == B_NO_MEMORY)) {
|
||||
SDL_SetError("Not enough resources to create thread");
|
||||
return (-1);
|
||||
return SDL_SetError("Not enough resources to create thread");
|
||||
}
|
||||
resume_thread(thread->handle);
|
||||
return (0);
|
||||
|
|
|
@ -82,8 +82,7 @@ int
|
|||
SDL_CondSignal(SDL_cond * cond)
|
||||
{
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* If there are waiting threads not already signalled, then
|
||||
|
@ -107,8 +106,7 @@ int
|
|||
SDL_CondBroadcast(SDL_cond * cond)
|
||||
{
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* If there are waiting threads not already signalled, then
|
||||
|
@ -164,8 +162,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
|||
int retval;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* Obtain the protection mutex, and increment the number of waiters.
|
||||
|
|
|
@ -68,9 +68,9 @@ SDL_DestroyMutex(SDL_mutex * mutex)
|
|||
}
|
||||
}
|
||||
|
||||
/* Lock the semaphore */
|
||||
/* Lock the mutex */
|
||||
int
|
||||
SDL_mutexP(SDL_mutex * mutex)
|
||||
SDL_LockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
#if SDL_THREADS_DISABLED
|
||||
return 0;
|
||||
|
@ -78,8 +78,7 @@ SDL_mutexP(SDL_mutex * mutex)
|
|||
SDL_threadID this_thread;
|
||||
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
this_thread = SDL_ThreadID();
|
||||
|
@ -99,6 +98,39 @@ SDL_mutexP(SDL_mutex * mutex)
|
|||
#endif /* SDL_THREADS_DISABLED */
|
||||
}
|
||||
|
||||
/* try Lock the mutex */
|
||||
int
|
||||
SDL_TryLockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
#if SDL_THREADS_DISABLED
|
||||
return 0;
|
||||
#else
|
||||
int retval = 0;
|
||||
SDL_threadID this_thread;
|
||||
|
||||
if (mutex == NULL) {
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
this_thread = SDL_ThreadID();
|
||||
if (mutex->owner == this_thread) {
|
||||
++mutex->recursive;
|
||||
} else {
|
||||
/* The order of operations is important.
|
||||
We set the locking thread id after we obtain the lock
|
||||
so unlocks from other threads will fail.
|
||||
*/
|
||||
retval = SDL_SemWait(mutex->sem);
|
||||
if (retval == 0) {
|
||||
mutex->owner = this_thread;
|
||||
mutex->recursive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
#endif /* SDL_THREADS_DISABLED */
|
||||
}
|
||||
|
||||
/* Unlock the mutex */
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
|
@ -107,14 +139,12 @@ SDL_mutexV(SDL_mutex * mutex)
|
|||
return 0;
|
||||
#else
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
/* If we don't own the mutex, we can't unlock it */
|
||||
if (SDL_ThreadID() != mutex->owner) {
|
||||
SDL_SetError("mutex not owned by this thread");
|
||||
return -1;
|
||||
return SDL_SetError("mutex not owned by this thread");
|
||||
}
|
||||
|
||||
if (mutex->recursive) {
|
||||
|
|
|
@ -39,28 +39,24 @@ SDL_CreateSemaphore(Uint32 initial_value)
|
|||
void
|
||||
SDL_DestroySemaphore(SDL_sem * sem)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemTryWait(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
return SDL_SetError("SDL not configured with thread support");
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
return SDL_SetError("SDL not configured with thread support");
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWait(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
return SDL_SetError("SDL not configured with thread support");
|
||||
}
|
||||
|
||||
Uint32
|
||||
|
@ -72,8 +68,7 @@ SDL_SemValue(SDL_sem * sem)
|
|||
int
|
||||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
return SDL_SetError("SDL not configured with thread support");
|
||||
}
|
||||
|
||||
#else
|
||||
|
@ -123,8 +118,8 @@ SDL_DestroySemaphore(SDL_sem * sem)
|
|||
}
|
||||
SDL_DestroyCond(sem->count_nonzero);
|
||||
if (sem->count_lock) {
|
||||
SDL_mutexP(sem->count_lock);
|
||||
SDL_mutexV(sem->count_lock);
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
SDL_UnlockMutex(sem->count_lock);
|
||||
SDL_DestroyMutex(sem->count_lock);
|
||||
}
|
||||
SDL_free(sem);
|
||||
|
@ -137,8 +132,7 @@ SDL_SemTryWait(SDL_sem * sem)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
|
@ -158,8 +152,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
/* A timeout of 0 is an easy case */
|
||||
|
@ -207,8 +200,7 @@ int
|
|||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
|
|
|
@ -28,8 +28,7 @@
|
|||
int
|
||||
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
||||
{
|
||||
SDL_SetError("Threads are not supported on this platform");
|
||||
return (-1);
|
||||
return SDL_SetError("Threads are not supported on this platform");
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_syscond_c.h,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
|
@ -1,229 +0,0 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_syssem.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
||||
|
||||
/* An implementation of semaphores using mutexes and condition variables */
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_systhread_c.h"
|
||||
|
||||
|
||||
#ifdef DISABLE_THREADS
|
||||
|
||||
SDL_sem *
|
||||
SDL_CreateSemaphore(Uint32 initial_value)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return (SDL_sem *) 0;
|
||||
}
|
||||
|
||||
void
|
||||
SDL_DestroySemaphore(SDL_sem * sem)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemTryWait(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWait(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Uint32
|
||||
SDL_SemValue(SDL_sem * sem)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
SDL_SetError("SDL not configured with thread support");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct SDL_semaphore
|
||||
{
|
||||
Uint32 count;
|
||||
Uint32 waiters_count;
|
||||
SDL_mutex *count_lock;
|
||||
SDL_cond *count_nonzero;
|
||||
};
|
||||
|
||||
SDL_sem *
|
||||
SDL_CreateSemaphore(Uint32 initial_value)
|
||||
{
|
||||
SDL_sem *sem;
|
||||
|
||||
sem = (SDL_sem *) malloc(sizeof(*sem));
|
||||
if (!sem) {
|
||||
SDL_OutOfMemory();
|
||||
return (0);
|
||||
}
|
||||
sem->count = initial_value;
|
||||
sem->waiters_count = 0;
|
||||
|
||||
sem->count_lock = SDL_CreateMutex();
|
||||
sem->count_nonzero = SDL_CreateCond();
|
||||
if (!sem->count_lock || !sem->count_nonzero) {
|
||||
SDL_DestroySemaphore(sem);
|
||||
return (0);
|
||||
}
|
||||
|
||||
return (sem);
|
||||
}
|
||||
|
||||
/* WARNING:
|
||||
You cannot call this function when another thread is using the semaphore.
|
||||
*/
|
||||
void
|
||||
SDL_DestroySemaphore(SDL_sem * sem)
|
||||
{
|
||||
if (sem) {
|
||||
sem->count = 0xFFFFFFFF;
|
||||
while (sem->waiters_count > 0) {
|
||||
SDL_CondSignal(sem->count_nonzero);
|
||||
SDL_Delay(10);
|
||||
}
|
||||
SDL_DestroyCond(sem->count_nonzero);
|
||||
SDL_mutexP(sem->count_lock);
|
||||
SDL_mutexV(sem->count_lock);
|
||||
SDL_DestroyMutex(sem->count_lock);
|
||||
free(sem);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemTryWait(SDL_sem * sem)
|
||||
{
|
||||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
}
|
||||
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
if (sem->count > 0) {
|
||||
--sem->count;
|
||||
retval = 0;
|
||||
}
|
||||
SDL_UnlockMutex(sem->count_lock);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
||||
{
|
||||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* A timeout of 0 is an easy case */
|
||||
if (timeout == 0) {
|
||||
return SDL_SemTryWait(sem);
|
||||
}
|
||||
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
++sem->waiters_count;
|
||||
retval = 0;
|
||||
while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
|
||||
retval = SDL_CondWaitTimeout(sem->count_nonzero,
|
||||
sem->count_lock, timeout);
|
||||
}
|
||||
--sem->waiters_count;
|
||||
--sem->count;
|
||||
SDL_UnlockMutex(sem->count_lock);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemWait(SDL_sem * sem)
|
||||
{
|
||||
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
|
||||
}
|
||||
|
||||
Uint32
|
||||
SDL_SemValue(SDL_sem * sem)
|
||||
{
|
||||
Uint32 value;
|
||||
|
||||
value = 0;
|
||||
if (sem) {
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
value = sem->count;
|
||||
SDL_UnlockMutex(sem->count_lock);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_LockMutex(sem->count_lock);
|
||||
if (sem->waiters_count > 0) {
|
||||
SDL_CondSignal(sem->count_nonzero);
|
||||
}
|
||||
++sem->count;
|
||||
SDL_UnlockMutex(sem->count_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* DISABLE_THREADS */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
|
@ -1,25 +0,0 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_syssem_c.h,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_systhread.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
||||
|
||||
/* Thread management routines for SDL */
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "../SDL_systhread.h"
|
||||
|
||||
int
|
||||
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
||||
{
|
||||
SDL_SetError("Threads are not supported on this platform");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_threadID
|
||||
SDL_ThreadID(void)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
SDL_SYS_WaitThread(SDL_Thread * thread)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
|
@ -18,11 +18,7 @@
|
|||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_syscond.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
||||
#include "SDL_config.h"
|
||||
|
||||
/* An implementation of condition variables using semaphores and mutexes */
|
||||
/*
|
||||
|
@ -30,10 +26,6 @@ static char rcsid =
|
|||
implementation, written by Christopher Tate and Owen Smith. Thanks!
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_thread.h"
|
||||
|
||||
struct SDL_cond
|
||||
|
@ -51,7 +43,7 @@ SDL_CreateCond(void)
|
|||
{
|
||||
SDL_cond *cond;
|
||||
|
||||
cond = (SDL_cond *) malloc(sizeof(SDL_cond));
|
||||
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
|
||||
if (cond) {
|
||||
cond->lock = SDL_CreateMutex();
|
||||
cond->wait_sem = SDL_CreateSemaphore(0);
|
||||
|
@ -81,7 +73,7 @@ SDL_DestroyCond(SDL_cond * cond)
|
|||
if (cond->lock) {
|
||||
SDL_DestroyMutex(cond->lock);
|
||||
}
|
||||
free(cond);
|
||||
SDL_free(cond);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,8 +82,7 @@ int
|
|||
SDL_CondSignal(SDL_cond * cond)
|
||||
{
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* If there are waiting threads not already signalled, then
|
||||
|
@ -115,8 +106,7 @@ int
|
|||
SDL_CondBroadcast(SDL_cond * cond)
|
||||
{
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* If there are waiting threads not already signalled, then
|
||||
|
@ -152,18 +142,19 @@ SDL_CondBroadcast(SDL_cond * cond)
|
|||
Typical use:
|
||||
|
||||
Thread A:
|
||||
SDL_LockMutex(lock);
|
||||
while ( ! condition ) {
|
||||
SDL_CondWait(cond);
|
||||
}
|
||||
SDL_UnlockMutex(lock);
|
||||
SDL_LockMutex(lock);
|
||||
while ( ! condition ) {
|
||||
SDL_CondWait(cond, lock);
|
||||
}
|
||||
SDL_UnlockMutex(lock);
|
||||
|
||||
Thread B:
|
||||
SDL_LockMutex(lock);
|
||||
...
|
||||
condition = true;
|
||||
...
|
||||
SDL_UnlockMutex(lock);
|
||||
SDL_LockMutex(lock);
|
||||
...
|
||||
condition = true;
|
||||
...
|
||||
SDL_CondSignal(cond);
|
||||
SDL_UnlockMutex(lock);
|
||||
*/
|
||||
int
|
||||
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
||||
|
@ -171,8 +162,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
|||
int retval;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
/* Obtain the protection mutex, and increment the number of waiters.
|
|
@ -18,18 +18,10 @@
|
|||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_sysmutex.c,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
||||
#include "SDL_config.h"
|
||||
|
||||
/* An implementation of mutexes using semaphores */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_systhread_c.h"
|
||||
|
||||
|
@ -48,14 +40,14 @@ SDL_CreateMutex(void)
|
|||
SDL_mutex *mutex;
|
||||
|
||||
/* Allocate mutex memory */
|
||||
mutex = (SDL_mutex *) malloc(sizeof(*mutex));
|
||||
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
|
||||
if (mutex) {
|
||||
/* Create the mutex semaphore, with initial value 1 */
|
||||
mutex->sem = SDL_CreateSemaphore(1);
|
||||
mutex->recursive = 0;
|
||||
mutex->owner = 0;
|
||||
if (!mutex->sem) {
|
||||
free(mutex);
|
||||
SDL_free(mutex);
|
||||
mutex = NULL;
|
||||
}
|
||||
} else {
|
||||
|
@ -72,7 +64,7 @@ SDL_DestroyMutex(SDL_mutex * mutex)
|
|||
if (mutex->sem) {
|
||||
SDL_DestroySemaphore(mutex->sem);
|
||||
}
|
||||
free(mutex);
|
||||
SDL_free(mutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,14 +72,13 @@ SDL_DestroyMutex(SDL_mutex * mutex)
|
|||
int
|
||||
SDL_mutexP(SDL_mutex * mutex)
|
||||
{
|
||||
#ifdef DISABLE_THREADS
|
||||
#if SDL_THREADS_DISABLED
|
||||
return 0;
|
||||
#else
|
||||
SDL_threadID this_thread;
|
||||
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
this_thread = SDL_ThreadID();
|
||||
|
@ -104,25 +95,23 @@ SDL_mutexP(SDL_mutex * mutex)
|
|||
}
|
||||
|
||||
return 0;
|
||||
#endif /* DISABLE_THREADS */
|
||||
#endif /* SDL_THREADS_DISABLED */
|
||||
}
|
||||
|
||||
/* Unlock the mutex */
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
{
|
||||
#ifdef DISABLE_THREADS
|
||||
#if SDL_THREADS_DISABLED
|
||||
return 0;
|
||||
#else
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
/* If we don't own the mutex, we can't unlock it */
|
||||
if (SDL_ThreadID() != mutex->owner) {
|
||||
SDL_SetError("mutex not owned by this thread");
|
||||
return -1;
|
||||
return SDL_SetError("mutex not owned by this thread");
|
||||
}
|
||||
|
||||
if (mutex->recursive) {
|
||||
|
@ -137,7 +126,7 @@ SDL_mutexV(SDL_mutex * mutex)
|
|||
SDL_SemPost(mutex->sem);
|
||||
}
|
||||
return 0;
|
||||
#endif /* DISABLE_THREADS */
|
||||
#endif /* SDL_THREADS_DISABLED */
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
|
@ -18,8 +18,5 @@
|
|||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifdef SAVE_RCSID
|
||||
static char rcsid =
|
||||
"@(#) $Id: SDL_sysmutex_c.h,v 1.2 2001/04/26 16:50:18 hercules Exp $";
|
||||
#endif
|
||||
#include "SDL_config.h"
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
156
src/thread/psp/SDL_syssem.c
Normal file
156
src/thread/psp/SDL_syssem.c
Normal file
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
/* Semaphore functions for the PSP. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_thread.h"
|
||||
|
||||
#include <pspthreadman.h>
|
||||
#include <pspkerror.h>
|
||||
|
||||
struct SDL_semaphore {
|
||||
SceUID semid;
|
||||
};
|
||||
|
||||
|
||||
/* Create a semaphore */
|
||||
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
return sem;
|
||||
}
|
||||
|
||||
/* Free the semaphore */
|
||||
void SDL_DestroySemaphore(SDL_sem *sem)
|
||||
{
|
||||
if (sem != NULL) {
|
||||
if (sem->semid > 0) {
|
||||
sceKernelDeleteSema(sem->semid);
|
||||
sem->semid = 0;
|
||||
}
|
||||
|
||||
free(sem);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: This routine is a bit overloaded.
|
||||
* If the timeout is 0 then just poll the semaphore; if it's SDL_MUTEX_MAXWAIT, pass
|
||||
* NULL to sceKernelWaitSema() so that it waits indefinitely; and if the timeout
|
||||
* is specified, convert it to microseconds. */
|
||||
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
|
||||
{
|
||||
Uint32 *pTimeout;
|
||||
unsigned int res;
|
||||
|
||||
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 == SDL_MUTEX_MAXWAIT) {
|
||||
pTimeout = NULL;
|
||||
} else {
|
||||
timeout *= 1000; /* Convert to microseconds. */
|
||||
pTimeout = &timeout;
|
||||
}
|
||||
|
||||
res = sceKernelWaitSema(sem->semid, 1, pTimeout);
|
||||
switch (res) {
|
||||
case SCE_KERNEL_ERROR_OK:
|
||||
return 0;
|
||||
case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
|
||||
return SDL_MUTEX_TIMEDOUT;
|
||||
default:
|
||||
return SDL_SetError("WaitForSingleObject() failed");
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_SemTryWait(SDL_sem *sem)
|
||||
{
|
||||
return SDL_SemWaitTimeout(sem, 0);
|
||||
}
|
||||
|
||||
int SDL_SemWait(SDL_sem *sem)
|
||||
{
|
||||
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
|
||||
}
|
||||
|
||||
/* Returns the current count of the semaphore */
|
||||
Uint32 SDL_SemValue(SDL_sem *sem)
|
||||
{
|
||||
SceKernelSemaInfo info;
|
||||
|
||||
if (sem == NULL) {
|
||||
SDL_SetError("Passed a NULL sem");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sceKernelReferSemaStatus(sem->semid, &info) >= 0) {
|
||||
return info.currentCount;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_SemPost(SDL_sem *sem)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (sem == NULL) {
|
||||
return SDL_SetError("Passed a NULL sem");
|
||||
}
|
||||
|
||||
res = sceKernelSignalSema(sem->semid, 1);
|
||||
if (res < 0) {
|
||||
return SDL_SetError("sceKernelSignalSema() failed");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim: ts=4 sw=4
|
||||
*/
|
102
src/thread/psp/SDL_systhread.c
Normal file
102
src/thread/psp/SDL_systhread.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
|
||||
/* PSP thread management routines for SDL */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_error.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "../SDL_systhread.h"
|
||||
#include "../SDL_thread_c.h"
|
||||
#include <pspkerneltypes.h>
|
||||
#include <pspthreadman.h>
|
||||
|
||||
|
||||
static int ThreadEntry(SceSize args, void *argp)
|
||||
{
|
||||
SDL_RunThread(*(void **) argp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
/* Do nothing. */
|
||||
}
|
||||
|
||||
SDL_threadID SDL_ThreadID(void)
|
||||
{
|
||||
return (SDL_threadID) sceKernelGetThreadId();
|
||||
}
|
||||
|
||||
void SDL_SYS_WaitThread(SDL_Thread *thread)
|
||||
{
|
||||
sceKernelWaitThreadEnd(thread->handle, NULL);
|
||||
sceKernelDeleteThread(thread->handle);
|
||||
}
|
||||
|
||||
void SDL_SYS_KillThread(SDL_Thread *thread)
|
||||
{
|
||||
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) {
|
||||
value = -20;
|
||||
} else {
|
||||
value = 0;
|
||||
}
|
||||
|
||||
return sceKernelChangeThreadPriority(sceKernelGetThreadId(),value);
|
||||
|
||||
}
|
||||
|
||||
/* vim: ts=4 sw=4
|
||||
*/
|
|
@ -19,9 +19,6 @@
|
|||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/* Stub until we implement threads on this platform */
|
||||
typedef int SYS_ThreadHandle;
|
||||
#include <pspkerneltypes.h>
|
||||
|
||||
#ifndef DISABLE_THREADS
|
||||
#define DISABLE_THREADS
|
||||
#endif
|
||||
typedef SceUID SYS_ThreadHandle;
|
|
@ -67,14 +67,12 @@ SDL_CondSignal(SDL_cond * cond)
|
|||
int retval;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
if (pthread_cond_signal(&cond->cond) != 0) {
|
||||
SDL_SetError("pthread_cond_signal() failed");
|
||||
retval = -1;
|
||||
return SDL_SetError("pthread_cond_signal() failed");
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
@ -86,14 +84,12 @@ SDL_CondBroadcast(SDL_cond * cond)
|
|||
int retval;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
if (pthread_cond_broadcast(&cond->cond) != 0) {
|
||||
SDL_SetError("pthread_cond_broadcast() failed");
|
||||
retval = -1;
|
||||
return SDL_SetError("pthread_cond_broadcast() failed");
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
@ -106,8 +102,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
|||
struct timespec abstime;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
}
|
||||
|
||||
gettimeofday(&delta, NULL);
|
||||
|
@ -131,9 +126,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
|||
case 0:
|
||||
break;
|
||||
default:
|
||||
SDL_SetError("pthread_cond_timedwait() failed");
|
||||
retval = -1;
|
||||
break;
|
||||
retval = SDL_SetError("pthread_cond_timedwait() failed");
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
@ -144,19 +137,12 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
|
|||
int
|
||||
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
|
||||
{
|
||||
int retval;
|
||||
|
||||
if (!cond) {
|
||||
SDL_SetError("Passed a NULL condition variable");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL condition variable");
|
||||
} else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
|
||||
return SDL_SetError("pthread_cond_wait() failed");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
|
||||
SDL_SetError("pthread_cond_wait() failed");
|
||||
retval = -1;
|
||||
}
|
||||
return retval;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#define _GNU_SOURCE
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "SDL_thread.h"
|
||||
|
||||
|
@ -78,19 +79,16 @@ SDL_DestroyMutex(SDL_mutex * mutex)
|
|||
|
||||
/* Lock the mutex */
|
||||
int
|
||||
SDL_mutexP(SDL_mutex * mutex)
|
||||
SDL_LockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
int retval;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
pthread_t this_thread;
|
||||
#endif
|
||||
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
this_thread = pthread_self();
|
||||
if (mutex->owner == this_thread) {
|
||||
|
@ -104,30 +102,67 @@ SDL_mutexP(SDL_mutex * mutex)
|
|||
mutex->owner = this_thread;
|
||||
mutex->recursive = 0;
|
||||
} else {
|
||||
SDL_SetError("pthread_mutex_lock() failed");
|
||||
retval = -1;
|
||||
return SDL_SetError("pthread_mutex_lock() failed");
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (pthread_mutex_lock(&mutex->id) < 0) {
|
||||
SDL_SetError("pthread_mutex_lock() failed");
|
||||
retval = -1;
|
||||
return SDL_SetError("pthread_mutex_lock() failed");
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_TryLockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
int retval;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
pthread_t this_thread;
|
||||
#endif
|
||||
|
||||
if (mutex == NULL) {
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
this_thread = pthread_self();
|
||||
if (mutex->owner == this_thread) {
|
||||
++mutex->recursive;
|
||||
} else {
|
||||
/* The order of operations is important.
|
||||
We set the locking thread id after we obtain the lock
|
||||
so unlocks from other threads will fail.
|
||||
*/
|
||||
if (pthread_mutex_lock(&mutex->id) == 0) {
|
||||
mutex->owner = this_thread;
|
||||
mutex->recursive = 0;
|
||||
} else if (errno == EBUSY) {
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
} else {
|
||||
retval = SDL_SetError("pthread_mutex_trylock() failed");
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (pthread_mutex_trylock(&mutex->id) != 0) {
|
||||
if (errno == EBUSY) {
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
} else {
|
||||
retval = SDL_SetError("pthread_mutex_trylock() failed");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
SDL_UnlockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
int retval;
|
||||
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
/* We can only unlock the mutex if we own it */
|
||||
if (pthread_self() == mutex->owner) {
|
||||
|
@ -143,18 +178,16 @@ SDL_mutexV(SDL_mutex * mutex)
|
|||
pthread_mutex_unlock(&mutex->id);
|
||||
}
|
||||
} else {
|
||||
SDL_SetError("mutex not owned by this thread");
|
||||
retval = -1;
|
||||
return SDL_SetError("mutex not owned by this thread");
|
||||
}
|
||||
|
||||
#else
|
||||
if (pthread_mutex_unlock(&mutex->id) < 0) {
|
||||
SDL_SetError("pthread_mutex_unlock() failed");
|
||||
retval = -1;
|
||||
return SDL_SetError("pthread_mutex_unlock() failed");
|
||||
}
|
||||
#endif /* FAKE_RECURSIVE_MUTEX */
|
||||
|
||||
return retval;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -72,8 +72,7 @@ SDL_SemTryWait(SDL_sem * sem)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
if (sem_trywait(&sem->sem) == 0) {
|
||||
|
@ -88,13 +87,12 @@ SDL_SemWait(SDL_sem * sem)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
retval = sem_wait(&sem->sem);
|
||||
if (retval < 0) {
|
||||
SDL_SetError("sem_wait() failed");
|
||||
retval = SDL_SetError("sem_wait() failed");
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
@ -111,8 +109,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
#endif
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
/* Try the easy cases first */
|
||||
|
@ -188,8 +185,7 @@ SDL_SemPost(SDL_sem * sem)
|
|||
int retval;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL semaphore");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL semaphore");
|
||||
}
|
||||
|
||||
retval = sem_post(&sem->sem);
|
||||
|
|
|
@ -97,18 +97,16 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
|
||||
/* Set the thread attributes */
|
||||
if (pthread_attr_init(&type) != 0) {
|
||||
SDL_SetError("Couldn't initialize pthread attributes");
|
||||
return (-1);
|
||||
return SDL_SetError("Couldn't initialize pthread attributes");
|
||||
}
|
||||
pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
/* Create the thread and go! */
|
||||
if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
|
||||
SDL_SetError("Not enough resources to create thread");
|
||||
return (-1);
|
||||
return SDL_SetError("Not enough resources to create thread");
|
||||
}
|
||||
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -173,8 +171,7 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
|||
/* Note that this fails if you're trying to set high priority
|
||||
and you don't have root permission. BUT DON'T RUN AS ROOT!
|
||||
*/
|
||||
SDL_SetError("setpriority() failed");
|
||||
return -1;
|
||||
return SDL_SetError("setpriority() failed");
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
|
@ -183,8 +180,7 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
|||
pthread_t thread = pthread_self();
|
||||
|
||||
if (pthread_getschedparam(thread, &policy, &sched) < 0) {
|
||||
SDL_SetError("pthread_getschedparam() failed");
|
||||
return -1;
|
||||
return SDL_SetError("pthread_getschedparam() failed");
|
||||
}
|
||||
if (priority == SDL_THREAD_PRIORITY_LOW) {
|
||||
sched.sched_priority = sched_get_priority_min(policy);
|
||||
|
@ -196,8 +192,7 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
|||
sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
|
||||
}
|
||||
if (pthread_setschedparam(thread, policy, &sched) < 0) {
|
||||
SDL_SetError("pthread_setschedparam() failed");
|
||||
return -1;
|
||||
return SDL_SetError("pthread_setschedparam() failed");
|
||||
}
|
||||
return 0;
|
||||
#endif /* linux */
|
||||
|
|
|
@ -64,24 +64,37 @@ SDL_DestroyMutex(SDL_mutex * mutex)
|
|||
|
||||
/* Lock the mutex */
|
||||
int
|
||||
SDL_mutexP(SDL_mutex * mutex)
|
||||
SDL_LockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
EnterCriticalSection(&mutex->cs);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* TryLock the mutex */
|
||||
int
|
||||
SDL_TryLockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
int retval = 0;
|
||||
if (mutex == NULL) {
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
if (TryEnterCriticalSection(&mutex->cs) == 0) {
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* Unlock the mutex */
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
SDL_UnlockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL mutex");
|
||||
}
|
||||
|
||||
LeaveCriticalSection(&mutex->cs);
|
||||
|
|
|
@ -78,8 +78,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
DWORD dwMilliseconds;
|
||||
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL sem");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL sem");
|
||||
}
|
||||
|
||||
if (timeout == SDL_MUTEX_MAXWAIT) {
|
||||
|
@ -96,8 +95,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
|
|||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
break;
|
||||
default:
|
||||
SDL_SetError("WaitForSingleObject() failed");
|
||||
retval = -1;
|
||||
retval = SDL_SetError("WaitForSingleObject() failed");
|
||||
break;
|
||||
}
|
||||
return retval;
|
||||
|
@ -130,8 +128,7 @@ int
|
|||
SDL_SemPost(SDL_sem * sem)
|
||||
{
|
||||
if (!sem) {
|
||||
SDL_SetError("Passed a NULL sem");
|
||||
return -1;
|
||||
return SDL_SetError("Passed a NULL sem");
|
||||
}
|
||||
/* Increase the counter in the first place, because
|
||||
* after a successful release the semaphore may
|
||||
|
@ -141,8 +138,7 @@ SDL_SemPost(SDL_sem * sem)
|
|||
InterlockedIncrement(&sem->count);
|
||||
if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
|
||||
InterlockedDecrement(&sem->count); /* restore */
|
||||
SDL_SetError("ReleaseSemaphore() failed");
|
||||
return -1;
|
||||
return SDL_SetError("ReleaseSemaphore() failed");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -116,8 +116,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
pThreadStartParms pThreadParms =
|
||||
(pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
|
||||
if (!pThreadParms) {
|
||||
SDL_OutOfMemory();
|
||||
return (-1);
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
// Save the function which we will have to call to clear the RTL of calling app!
|
||||
pThreadParms->pfnCurrentEndThread = pfnEndThread;
|
||||
|
@ -135,10 +134,9 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
pThreadParms, 0, &threadid);
|
||||
}
|
||||
if (thread->handle == NULL) {
|
||||
SDL_SetError("Not enough resources to create thread");
|
||||
return (-1);
|
||||
return SDL_SetError("Not enough resources to create thread");
|
||||
}
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
@ -198,8 +196,7 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
|
|||
value = THREAD_PRIORITY_NORMAL;
|
||||
}
|
||||
if (!SetThreadPriority(GetCurrentThread(), value)) {
|
||||
WIN_SetError("SetThreadPriority()");
|
||||
return -1;
|
||||
return WIN_SetError("SetThreadPriority()");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue