diff --git a/libretro-common/include/rthreads/rthreads.h b/libretro-common/include/rthreads/rthreads.h index 93fc16195d..946c3492b5 100644 --- a/libretro-common/include/rthreads/rthreads.h +++ b/libretro-common/include/rthreads/rthreads.h @@ -131,6 +131,15 @@ void slock_free(slock_t *lock); **/ void slock_lock(slock_t *lock); +/** + * slock_try_lock: + * @lock : pointer to mutex object + * + * Attempts to lock a mutex. If a mutex is already locked by + * another thread, return false. If the lock is acquired, return true. +**/ +bool slock_try_lock(slock_t *lock); + /** * slock_unlock: * @lock : pointer to mutex object diff --git a/libretro-common/rthreads/rthreads.c b/libretro-common/rthreads/rthreads.c index 24df8ad5c4..c36e32ff07 100644 --- a/libretro-common/rthreads/rthreads.c +++ b/libretro-common/rthreads/rthreads.c @@ -385,6 +385,24 @@ void slock_lock(slock_t *lock) #endif } +/** + * slock_try_lock: + * @lock : pointer to mutex object + * + * Attempts to lock a mutex. If a mutex is already locked by + * another thread, return false. If the lock is acquired, return true. +**/ +bool slock_try_lock(slock_t *lock) +{ + if (!lock) + return false; +#ifdef USE_WIN32_THREADS + return TryEnterCriticalSection(&lock->lock); +#else + return pthread_mutex_trylock(&lock->lock)==0; +#endif +} + /** * slock_unlock: * @lock : pointer to mutex object diff --git a/libretro-common/streams/file_stream.c b/libretro-common/streams/file_stream.c index ce189f981c..1579c15068 100644 --- a/libretro-common/streams/file_stream.c +++ b/libretro-common/streams/file_stream.c @@ -204,9 +204,9 @@ int filestream_getc(RFILE *stream) { char c = 0; if (!stream) - return 0; - if(filestream_read(stream, &c, 1) == 1) - return (int)c; + return EOF; + if (filestream_read(stream, &c, 1) == 1) + return (int)(unsigned char)c; return EOF; } @@ -427,7 +427,7 @@ int filestream_putc(RFILE *stream, int c) char c_char = (char)c; if (!stream) return EOF; - return filestream_write(stream, &c_char, 1)==1 ? c : EOF; + return filestream_write(stream, &c_char, 1)==1 ? (int)(unsigned char)c : EOF; } int filestream_vprintf(RFILE *stream, const char* format, va_list args)