Path: blob/master/thirdparty/sdl/thread/pthread/SDL_sysrwlock.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>2425struct SDL_RWLock26{27pthread_rwlock_t id;28};293031SDL_RWLock *SDL_CreateRWLock(void)32{33SDL_RWLock *rwlock;3435// Allocate the structure36rwlock = (SDL_RWLock *)SDL_calloc(1, sizeof(*rwlock));37if (rwlock) {38if (pthread_rwlock_init(&rwlock->id, NULL) != 0) {39SDL_SetError("pthread_rwlock_init() failed");40SDL_free(rwlock);41rwlock = NULL;42}43}44return rwlock;45}4647void SDL_DestroyRWLock(SDL_RWLock *rwlock)48{49if (rwlock) {50pthread_rwlock_destroy(&rwlock->id);51SDL_free(rwlock);52}53}5455void SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes56{57if (rwlock) {58const int rc = pthread_rwlock_rdlock(&rwlock->id);59SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.60}61}6263void SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes64{65if (rwlock) {66const int rc = pthread_rwlock_wrlock(&rwlock->id);67SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.68}69}7071bool SDL_TryLockRWLockForReading(SDL_RWLock *rwlock)72{73bool result = true;7475if (rwlock) {76const int rc = pthread_rwlock_tryrdlock(&rwlock->id);77if (rc != 0) {78result = false;79if (rc != EBUSY) {80SDL_assert(!"Error trying to lock rwlock for reading"); // assume we're in a lot of trouble if this assert fails.81}82}83}8485return result;86}8788bool SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock)89{90bool result = true;9192if (rwlock) {93const int rc = pthread_rwlock_trywrlock(&rwlock->id);94if (rc != 0) {95result = false;96if (rc != EBUSY) {97SDL_assert(!"Error trying to lock rwlock for writing"); // assume we're in a lot of trouble if this assert fails.98}99}100}101102return result;103}104105void SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes106{107if (rwlock) {108const int rc = pthread_rwlock_unlock(&rwlock->id);109SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.110}111}112113114115