aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xbmc/threads/XBMC_mutex.cpp336
-rw-r--r--xbmc/threads/XBMC_mutex.h13
2 files changed, 146 insertions, 203 deletions
diff --git a/xbmc/threads/XBMC_mutex.cpp b/xbmc/threads/XBMC_mutex.cpp
index 8da355ed09..bc2a48e7d4 100644
--- a/xbmc/threads/XBMC_mutex.cpp
+++ b/xbmc/threads/XBMC_mutex.cpp
@@ -35,70 +35,71 @@
#endif
#include "XBMC_mutex.h"
-#include "log.h"
+#include "utils/log.h"
+
+#if defined(__APPLE__) || defined(_LINUX)
+#define XBCM_PTHREAD_MUTEX_RECURSIVE
+#endif
SDL_mutex *SDL_CreateMutex (void)
{
#if defined(_WIN32)
- SDL_mutex *mutex;
-
- /* Allocate mutex memory */
- mutex = (SDL_mutex *)malloc(sizeof(*mutex));
- if ( mutex ) {
- /* Create the mutex, with initial value signaled */
- mutex->id = CreateMutex(NULL, FALSE, NULL);
- if ( ! mutex->id ) {
+ SDL_mutex *mutex;
+
+ /* Allocate mutex memory */
+ mutex = (SDL_mutex *)malloc(sizeof(*mutex));
+ if ( mutex ) {
+ /* Create the mutex, with initial value signaled */
+ mutex->id = CreateMutex(NULL, FALSE, NULL);
+ if ( ! mutex->id ) {
CLog::Log(LOGERROR, "Couldn't create mutex");
free(mutex);
- mutex = NULL;
- }
- } else {
+ mutex = NULL;
+ }
+ } else {
CLog::Log(LOGERROR, "OutOfMemory");
- }
- return(mutex);
+ }
+ return(mutex);
#else
- SDL_mutex *mutex;
- pthread_mutexattr_t attr;
-
- /* Allocate the structure */
- mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
- if ( mutex ) {
- pthread_mutexattr_init(&attr);
- #if defined(PTHREAD_MUTEX_RECURSIVE)
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- #elif defined(PTHREAD_MUTEX_RECURSIVE_NP)
- pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
- #else
- /* No extra attributes necessary */
- #endif
+ SDL_mutex *mutex;
+ pthread_mutexattr_t attr;
+
+ /* Allocate the structure */
+ mutex = (SDL_mutex *)calloc(1, sizeof(*mutex));
+ if ( mutex ) {
+ pthread_mutexattr_init(&attr);
+#if defined(XBMC_PTHREAD_MUTEX_RECURSIVE)
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+#elif defined(XBMC_PTHREAD_MUTEX_RECURSIVE_NP)
+ pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+#endif
if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
CLog::Log(LOGERROR, "pthread_mutex_init() failed");
- free(mutex);
- mutex = NULL;
- }
- } else {
+ free(mutex);
+ mutex = NULL;
+ }
+ } else {
CLog::Log(LOGERROR, "OutOfMemory");
- }
- return(mutex);
+ }
+ return(mutex);
#endif
}
void SDL_DestroyMutex(SDL_mutex *mutex)
{
#if defined(_WIN32)
- if ( mutex ) {
- if ( mutex->id ) {
- CloseHandle(mutex->id);
- mutex->id = 0;
- }
+ if ( mutex ) {
+ if ( mutex->id ) {
+ CloseHandle(mutex->id);
+ mutex->id = 0;
+ }
free(mutex);
}
#else
-
- if ( mutex ) {
- pthread_mutex_destroy(&mutex->id);
- free(mutex);
- }
+ if ( mutex ) {
+ pthread_mutex_destroy(&mutex->id);
+ free(mutex);
+ }
#endif
}
@@ -106,205 +107,160 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
int SDL_mutexP(SDL_mutex *mutex)
{
#if defined(_WIN32)
- if ( mutex == NULL ) {
+ if ( mutex == NULL ) {
CLog::Log(LOGERROR, "Passed a NULL mutex");
- return -1;
- }
- if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
+ return -1;
+ }
+ if ( WaitForSingleObject(mutex->id, INFINITE) == WAIT_FAILED ) {
CLog::Log(LOGERROR, "Couldn't wait on mutex");
- return -1;
- }
- return(0);
+ return -1;
+ }
+ return(0);
#else
- int retval;
- #if defined(FAKE_RECURSIVE_MUTEX)
- pthread_t this_thread;
- #endif
+ int retval;
- if ( mutex == NULL ) {
+ if ( mutex == NULL ) {
CLog::Log(LOGERROR, "Passed a NULL mutex");
- return -1;
- }
-
- retval = 0;
- #if defined(FAKE_RECURSIVE_MUTEX)
- this_thread = pthread_self();
- if (pthread_equal(mutex->owner, this_thread)) {
- //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 {
- CLog::Log(LOGERROR, "pthread_mutex_lock() failed");
- retval = -1;
- }
- }
- #else
- if ( pthread_mutex_lock(&mutex->id) < 0 ) {
- CLog::Log(LOGERROR, "pthread_mutex_lock() failed");
- retval = -1;
- }
- #endif
- return retval;
+ return -1;
+ }
+
+ retval = 0;
+ if ( pthread_mutex_lock(&mutex->id) < 0 ) {
+ CLog::Log(LOGERROR, "pthread_mutex_lock() failed");
+ retval = -1;
+ }
+ return retval;
#endif
}
int SDL_mutexV(SDL_mutex *mutex)
{
#if defined(_WIN32)
- if ( mutex == NULL ) {
+ if ( mutex == NULL ) {
CLog::Log(LOGERROR, "Passed a NULL mutex");
- return -1;
- }
- if ( ReleaseMutex(mutex->id) == FALSE ) {
+ return -1;
+ }
+ if ( ReleaseMutex(mutex->id) == FALSE ) {
CLog::Log(LOGERROR, "Couldn't release mutex");
- return -1;
+ return -1;
}
- return(0);
+ return(0);
#else
- int retval;
+ int retval;
- if ( mutex == NULL ) {
+ if ( mutex == NULL ) {
CLog::Log(LOGERROR, "Passed a NULL mutex");
return -1;
- }
-
- retval = 0;
- #if defined(FAKE_RECURSIVE_MUTEX)
- /* We can only unlock the mutex if we own it */
- if (pthread_equal(pthread_self(), mutex->owner)) {
- //if ( pthread_self() == mutex->owner ) {
- if ( mutex->recursive ) {
- --mutex->recursive;
- } else {
- /* The order of operations is important.
- First reset the owner so another thread doesn't lock
- the mutex and set the ownership before we reset it,
- then release the lock semaphore.
- */
- mutex->owner = 0;
- pthread_mutex_unlock(&mutex->id);
- }
- } else {
- CLog::Log(LOGERROR, "mutex not owned by this thread");
- retval = -1;
- }
+ }
- #else
- if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
- CLog::Log(LOGERROR, "pthread_mutex_unlock() failed");
- retval = -1;
- }
- #endif /* FAKE_RECURSIVE_MUTEX */
+ retval = 0;
+ if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
+ CLog::Log(LOGERROR, "pthread_mutex_unlock() failed");
+ retval = -1;
+ }
- return retval;
+ return retval;
#endif
}
/* Create a condition variable */
SDL_cond * SDL_CreateCond(void)
{
- SDL_cond *cond;
+ SDL_cond *cond;
- cond = (SDL_cond *) malloc(sizeof(SDL_cond));
- if ( cond ) {
- if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
+ cond = (SDL_cond *) malloc(sizeof(SDL_cond));
+ if ( cond ) {
+ if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
CLog::Log(LOGERROR, "pthread_cond_init() failed");
- free(cond);
- cond = NULL;
- }
- }
- return(cond);
+ free(cond);
+ cond = NULL;
+ }
+ }
+ return(cond);
}
/* Destroy a condition variable */
void SDL_DestroyCond(SDL_cond *cond)
{
- if ( cond ) {
- pthread_cond_destroy(&cond->cond);
- free(cond);
- }
+ if ( cond ) {
+ pthread_cond_destroy(&cond->cond);
+ free(cond);
+ }
}
/* Restart one of the threads that are waiting on the condition variable */
int SDL_CondSignal(SDL_cond *cond)
{
- int retval;
+ int retval;
- if ( ! cond ) {
+ if ( ! cond ) {
CLog::Log(LOGERROR, "Passed a NULL condition variable");
- return -1;
+ return -1;
}
- retval = 0;
- if ( pthread_cond_signal(&cond->cond) != 0 ) {
+ retval = 0;
+ if ( pthread_cond_signal(&cond->cond) != 0 ) {
CLog::Log(LOGERROR, "pthread_cond_signal() failed");
- retval = -1;
- }
- return retval;
+ retval = -1;
+ }
+ return retval;
}
/* Restart all threads that are waiting on the condition variable */
int SDL_CondBroadcast(SDL_cond *cond)
{
- int retval;
+ int retval;
- if ( ! cond ) {
+ if ( ! cond ) {
CLog::Log(LOGERROR, "Passed a NULL condition variable");
- return -1;
- }
+ return -1;
+ }
- retval = 0;
- if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
+ retval = 0;
+ if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
CLog::Log(LOGERROR, "pthread_cond_broadcast() failed");
- retval = -1;
- }
- return retval;
+ retval = -1;
+ }
+ return retval;
}
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, uint32_t ms)
{
- int retval;
- struct timeval delta;
- struct timespec abstime;
+ int retval;
+ struct timeval delta;
+ struct timespec abstime;
- if ( ! cond ) {
+ if ( ! cond ) {
CLog::Log(LOGERROR, "Passed a NULL condition variable");
- return -1;
- }
+ return -1;
+ }
- gettimeofday(&delta, NULL);
+ gettimeofday(&delta, NULL);
- abstime.tv_sec = delta.tv_sec + (ms/1000);
- abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
- if ( abstime.tv_nsec > 1000000000 ) {
- abstime.tv_sec += 1;
- abstime.tv_nsec -= 1000000000;
- }
+ abstime.tv_sec = delta.tv_sec + (ms/1000);
+ abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
+ if ( abstime.tv_nsec > 1000000000 ) {
+ abstime.tv_sec += 1;
+ abstime.tv_nsec -= 1000000000;
+ }
tryagain:
- retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
- switch (retval) {
- case EINTR:
- goto tryagain;
- break;
- case ETIMEDOUT:
- retval = SDL_MUTEX_TIMEDOUT;
- break;
- case 0:
- break;
- default:
- CLog::Log(LOGERROR, "pthread_cond_timedwait() failed");
- retval = -1;
- break;
- }
- return retval;
+ retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
+ switch (retval) {
+ case EINTR:
+ goto tryagain;
+ break;
+ case ETIMEDOUT:
+ retval = SDL_MUTEX_TIMEDOUT;
+ break;
+ case 0:
+ break;
+ default:
+ CLog::Log(LOGERROR, "pthread_cond_timedwait() failed");
+ retval = -1;
+ break;
+ }
+ return retval;
}
/* Wait on the condition variable, unlocking the provided mutex.
@@ -312,19 +268,19 @@ int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, uint32_t ms)
*/
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
- int retval;
+ int retval;
- if ( ! cond ) {
+ if ( ! cond ) {
CLog::Log(LOGERROR, "Passed a NULL condition variable");
- return -1;
- }
+ return -1;
+ }
- retval = 0;
- if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
+ retval = 0;
+ if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
CLog::Log(LOGERROR, "pthread_cond_wait() failed");
- retval = -1;
- }
- return retval;
+ retval = -1;
+ }
+ return retval;
}
#endif
diff --git a/xbmc/threads/XBMC_mutex.h b/xbmc/threads/XBMC_mutex.h
index f465b5530f..393b1c6405 100644
--- a/xbmc/threads/XBMC_mutex.h
+++ b/xbmc/threads/XBMC_mutex.h
@@ -46,24 +46,11 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Mutex functions */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-
-/* The SDL mutex structure, defined in SDL_mutex.c */
-#if !defined(_WIN32)
- #if !PTHREAD_MUTEX_RECURSIVE && \
- !PTHREAD_MUTEX_RECURSIVE_NP
- #define FAKE_RECURSIVE_MUTEX
- #endif
-#endif
-
typedef struct SDL_mutex {
#if defined(_WIN32)
HANDLE id;
#else
pthread_mutex_t id;
- #if defined(FAKE_RECURSIVE_MUTEX)
- int recursive;
- pthread_t owner;
- #endif
#endif
} SDL_mutex;