Atlas - SDL_sysmutex.c
Home / ext / SDL / src / thread / dos Lines: 1 | Size: 3095 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#ifdef SDL_THREAD_DOS 24 25/* Mutex implementation for DOS cooperative threading. 26 Uses cli/sti for atomicity and cooperative yielding for contention. */ 27 28#include "../../core/dos/SDL_dos.h" 29#include "../../core/dos/SDL_dos_scheduler.h" 30 31#define MUTEX_NO_OWNER -1 32 33struct SDL_Mutex 34{ 35 volatile int owner; /* Thread ID of owner, or MUTEX_NO_OWNER if unlocked */ 36 volatile int recursive; /* Recursion count */ 37}; 38 39SDL_Mutex *SDL_CreateMutex(void) 40{ 41 SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex)); 42 if (mutex) { 43 mutex->owner = MUTEX_NO_OWNER; 44 mutex->recursive = 0; 45 } 46 return mutex; 47} 48 49void SDL_DestroyMutex(SDL_Mutex *mutex) 50{ 51 if (mutex) { 52 SDL_free(mutex); 53 } 54} 55 56void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS 57{ 58 if (!mutex) { 59 return; 60 } 61 62 int tid = DOS_GetCurrentThreadID(); 63 64 for (;;) { 65 DOS_DisableInterrupts(); 66 if (mutex->owner == MUTEX_NO_OWNER) { 67 mutex->owner = tid; 68 mutex->recursive = 1; 69 DOS_EnableInterrupts(); 70 return; 71 } 72 if (mutex->owner == tid) { 73 mutex->recursive++; 74 DOS_EnableInterrupts(); 75 return; 76 } 77 DOS_EnableInterrupts(); 78 DOS_Yield(); 79 } 80} 81 82bool SDL_TryLockMutex(SDL_Mutex *mutex) 83{ 84 if (!mutex) { 85 return true; 86 } 87 88 int tid = DOS_GetCurrentThreadID(); 89 90 DOS_DisableInterrupts(); 91 if (mutex->owner == MUTEX_NO_OWNER) { 92 mutex->owner = tid; 93 mutex->recursive = 1; 94 DOS_EnableInterrupts(); 95 return true; 96 } 97 if (mutex->owner == tid) { 98 mutex->recursive++; 99 DOS_EnableInterrupts(); 100 return true; 101 } 102 DOS_EnableInterrupts(); 103 return false; 104} 105 106void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS 107{ 108 if (!mutex) { 109 return; 110 } 111 112 DOS_DisableInterrupts(); 113 if (mutex->recursive > 1) { 114 mutex->recursive--; 115 } else { 116 mutex->owner = MUTEX_NO_OWNER; 117 mutex->recursive = 0; 118 } 119 DOS_EnableInterrupts(); 120} 121 122#endif /* SDL_THREAD_DOS */ 123[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.