diff --git a/Makefile.common b/Makefile.common index dd422e8451..b16c59f99a 100644 --- a/Makefile.common +++ b/Makefile.common @@ -471,6 +471,7 @@ endif ifeq ($(HAVE_THREADS), 1) OBJ += autosave.o \ libretro-common/rthreads/rthreads.o \ + libretro-common/rthreads/rsemaphore.o \ gfx/video_thread_wrapper.o \ audio/audio_thread_wrapper.o DEFINES += -DHAVE_THREADS @@ -868,7 +869,7 @@ ifeq ($(HAVE_NETWORKING), 1) ifeq ($(HAVE_CHEEVOS), 1) ifeq ($(HAVE_THREADS), 1) DEFINES += -DHAVE_CHEEVOS - OBJ += cheevos.o ra_semaphore.o async_job.o libretro-common/utils/md5.o + OBJ += cheevos.o async_job.o libretro-common/utils/md5.o endif endif endif diff --git a/async_job.c b/async_job.c index 2c4008823d..9e84662e84 100644 --- a/async_job.c +++ b/async_job.c @@ -16,7 +16,7 @@ #include #include -#include +#include #include typedef struct async_job_node async_job_node_t; diff --git a/griffin/griffin.c b/griffin/griffin.c index 0bdb420c7f..0ebb341377 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -90,7 +90,6 @@ ACHIEVEMENTS #include "../libretro-common/formats/json/jsonsax.c" #include "../libretro-common/utils/md5.c" #include "../net_http_special.c" -#include "../ra_semaphore.c" #include "../async_job.c" #include "../cheevos.c" #endif @@ -733,6 +732,7 @@ THREAD #include "../thread/xenon_sdl_threads.c" #elif defined(HAVE_THREADS) #include "../libretro-common/rthreads/rthreads.c" +#include "../libretro-common/rthreads/rsemaphore.c" #include "../gfx/video_thread_wrapper.c" #include "../audio/audio_thread_wrapper.c" #include "../autosave.c" diff --git a/ra_semaphore.h b/libretro-common/include/rthreads/rsemaphore.h similarity index 94% rename from ra_semaphore.h rename to libretro-common/include/rthreads/rsemaphore.h index 5191ee59c9..3373dc84da 100644 --- a/ra_semaphore.h +++ b/libretro-common/include/rthreads/rsemaphore.h @@ -13,8 +13,8 @@ * If not, see . */ -#ifndef __RARCH_SEMAPHORE_H -#define __RARCH_SEMAPHORE_H +#ifndef __LIBRETRO_SDK_SEMAPHORE_H +#define __LIBRETRO_SDK_SEMAPHORE_H typedef struct ssem ssem_t; diff --git a/ra_semaphore.c b/libretro-common/rthreads/rsemaphore.c similarity index 89% rename from ra_semaphore.c rename to libretro-common/rthreads/rsemaphore.c index 5655c7122c..0b0a2ca7ba 100644 --- a/ra_semaphore.c +++ b/libretro-common/rthreads/rsemaphore.c @@ -27,11 +27,12 @@ #include #include -#include +#include struct ssem { - int value, wakeups; + int value; + int wakeups; slock_t *mutex; scond_t *cond; }; @@ -39,52 +40,54 @@ struct ssem ssem_t *ssem_new(int value) { ssem_t *semaphore = (ssem_t*)malloc(sizeof(*semaphore)); - + if (semaphore) { semaphore->value = value; semaphore->wakeups = 0; semaphore->mutex = slock_new(); - + if (semaphore->mutex) { semaphore->cond = scond_new(); - + if (semaphore->cond) return semaphore; - + slock_free(semaphore->mutex); } - + free((void*)semaphore); } - + return NULL; } void ssem_free(ssem_t *semaphore) { - scond_free(semaphore->cond); - slock_free(semaphore->mutex); - free((void*)semaphore); + if (!semaphore) + return; + + scond_free(semaphore->cond); + slock_free(semaphore->mutex); + free((void*)semaphore); } void ssem_wait(ssem_t *semaphore) { slock_lock(semaphore->mutex); semaphore->value--; - + if (semaphore->value < 0) { do { scond_wait(semaphore->cond, semaphore->mutex); - } - while (semaphore->wakeups < 1); - + }while (semaphore->wakeups < 1); + semaphore->wakeups--; } - + slock_unlock(semaphore->mutex); } @@ -92,12 +95,12 @@ void ssem_signal(ssem_t *semaphore) { slock_lock(semaphore->mutex); semaphore->value++; - + if (semaphore->value <= 0) { semaphore->wakeups++; scond_signal(semaphore->cond); } - + slock_unlock(semaphore->mutex); }