Path: blob/master/thirdparty/sdl/thread/pthread/SDL_sysmutex.c
10279 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122#include <errno.h>23#include <pthread.h>2425#include "SDL_sysmutex_c.h"2627SDL_Mutex *SDL_CreateMutex(void)28{29SDL_Mutex *mutex;30pthread_mutexattr_t attr;3132// Allocate the structure33mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex));34if (mutex) {35pthread_mutexattr_init(&attr);36#ifdef SDL_THREAD_PTHREAD_RECURSIVE_MUTEX37pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);38#elif defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP)39pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);40#else41// No extra attributes necessary42#endif43if (pthread_mutex_init(&mutex->id, &attr) != 0) {44SDL_SetError("pthread_mutex_init() failed");45SDL_free(mutex);46mutex = NULL;47}48}49return mutex;50}5152void SDL_DestroyMutex(SDL_Mutex *mutex)53{54if (mutex) {55pthread_mutex_destroy(&mutex->id);56SDL_free(mutex);57}58}5960void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes61{62if (mutex) {63#ifdef FAKE_RECURSIVE_MUTEX64pthread_t this_thread = pthread_self();65if (mutex->owner == this_thread) {66++mutex->recursive;67} else {68/* The order of operations is important.69We set the locking thread id after we obtain the lock70so unlocks from other threads will fail.71*/72const int rc = pthread_mutex_lock(&mutex->id);73SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.74mutex->owner = this_thread;75mutex->recursive = 0;76}77#else78const int rc = pthread_mutex_lock(&mutex->id);79SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.80#endif81}82}8384bool SDL_TryLockMutex(SDL_Mutex *mutex)85{86bool result = true;8788if (mutex) {89#ifdef FAKE_RECURSIVE_MUTEX90pthread_t this_thread = pthread_self();91if (mutex->owner == this_thread) {92++mutex->recursive;93} else {94/* The order of operations is important.95We set the locking thread id after we obtain the lock96so unlocks from other threads will fail.97*/98const int rc = pthread_mutex_trylock(&mutex->id);99if (rc == 0) {100mutex->owner = this_thread;101mutex->recursive = 0;102} else if (rc == EBUSY) {103result = false;104} else {105SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.106result = false;107}108}109#else110const int rc = pthread_mutex_trylock(&mutex->id);111if (rc != 0) {112if (rc == EBUSY) {113result = false;114} else {115SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.116result = false;117}118}119#endif120}121122return result;123}124125void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes126{127if (mutex) {128#ifdef FAKE_RECURSIVE_MUTEX129// We can only unlock the mutex if we own it130if (pthread_self() == mutex->owner) {131if (mutex->recursive) {132--mutex->recursive;133} else {134/* The order of operations is important.135First reset the owner so another thread doesn't lock136the mutex and set the ownership before we reset it,137then release the lock semaphore.138*/139mutex->owner = 0;140pthread_mutex_unlock(&mutex->id);141}142} else {143SDL_SetError("mutex not owned by this thread");144return;145}146147#else148const int rc = pthread_mutex_unlock(&mutex->id);149SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.150#endif // FAKE_RECURSIVE_MUTEX151}152}153154155156