Atlas - SDL_sysmutex.c
Home / ext / SDL / src / thread / pthread Lines: 1 | Size: 5107 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#include <errno.h> 24#include <pthread.h> 25 26#include "SDL_sysmutex_c.h" 27 28SDL_Mutex *SDL_CreateMutex(void) 29{ 30 SDL_Mutex *mutex; 31 pthread_mutexattr_t attr; 32 33 // Allocate the structure 34 mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex)); 35 if (mutex) { 36 pthread_mutexattr_init(&attr); 37#ifdef SDL_THREAD_PTHREAD_RECURSIVE_MUTEX 38 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 39#elif defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP) 40 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP); 41#else 42 // No extra attributes necessary 43#endif 44 if (pthread_mutex_init(&mutex->id, &attr) != 0) { 45 SDL_SetError("pthread_mutex_init() failed"); 46 SDL_free(mutex); 47 mutex = NULL; 48 } 49 pthread_mutexattr_destroy(&attr); 50 } 51 return mutex; 52} 53 54void SDL_DestroyMutex(SDL_Mutex *mutex) 55{ 56 if (mutex) { 57 pthread_mutex_destroy(&mutex->id); 58 SDL_free(mutex); 59 } 60} 61 62void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 63{ 64 if (mutex) { 65#ifdef FAKE_RECURSIVE_MUTEX 66 pthread_t this_thread = pthread_self(); 67 if (mutex->owner == this_thread) { 68 ++mutex->recursive; 69 } else { 70 /* The order of operations is important. 71 We set the locking thread id after we obtain the lock 72 so unlocks from other threads will fail. 73 */ 74 const int rc = pthread_mutex_lock(&mutex->id); 75 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails. 76 mutex->owner = this_thread; 77 mutex->recursive = 0; 78 } 79#else 80 const int rc = pthread_mutex_lock(&mutex->id); 81 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails. 82#endif 83 } 84} 85 86bool SDL_TryLockMutex(SDL_Mutex *mutex) 87{ 88 bool result = true; 89 90 if (mutex) { 91#ifdef FAKE_RECURSIVE_MUTEX 92 pthread_t this_thread = pthread_self(); 93 if (mutex->owner == this_thread) { 94 ++mutex->recursive; 95 } else { 96 /* The order of operations is important. 97 We set the locking thread id after we obtain the lock 98 so unlocks from other threads will fail. 99 */ 100 const int rc = pthread_mutex_trylock(&mutex->id); 101 if (rc == 0) { 102 mutex->owner = this_thread; 103 mutex->recursive = 0; 104 } else if (rc == EBUSY) { 105 result = false; 106 } else { 107 SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails. 108 result = false; 109 } 110 } 111#else 112 const int rc = pthread_mutex_trylock(&mutex->id); 113 if (rc != 0) { 114 if (rc == EBUSY) { 115 result = false; 116 } else { 117 SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails. 118 result = false; 119 } 120 } 121#endif 122 } 123 124 return result; 125} 126 127void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes 128{ 129 if (mutex) { 130#ifdef FAKE_RECURSIVE_MUTEX 131 // We can only unlock the mutex if we own it 132 if (pthread_self() == mutex->owner) { 133 if (mutex->recursive) { 134 --mutex->recursive; 135 } else { 136 /* The order of operations is important. 137 First reset the owner so another thread doesn't lock 138 the mutex and set the ownership before we reset it, 139 then release the lock semaphore. 140 */ 141 mutex->owner = 0; 142 pthread_mutex_unlock(&mutex->id); 143 } 144 } else { 145 SDL_SetError("mutex not owned by this thread"); 146 return; 147 } 148 149#else 150 const int rc = pthread_mutex_unlock(&mutex->id); 151 SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails. 152#endif // FAKE_RECURSIVE_MUTEX 153 } 154} 155 156[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.