SDL_sysmutex.c (2992B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 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 #if SDL_THREAD_OS2 24 25 /* An implementation of mutexes for OS/2 */ 26 27 #include "SDL_thread.h" 28 #include "SDL_systhread_c.h" 29 #include "../../core/os2/SDL_os2.h" 30 31 #define INCL_DOSSEMAPHORES 32 #define INCL_DOSERRORS 33 #include <os2.h> 34 35 struct SDL_mutex { 36 HMTX _handle; 37 }; 38 39 /* Create a mutex */ 40 SDL_mutex * 41 SDL_CreateMutex(void) 42 { 43 ULONG ulRC; 44 HMTX hMtx; 45 46 ulRC = DosCreateMutexSem(NULL, &hMtx, 0, FALSE); 47 if (ulRC != NO_ERROR) { 48 debug_os2("DosCreateMutexSem(), rc = %u", ulRC); 49 return NULL; 50 } 51 52 return (SDL_mutex *)hMtx; 53 } 54 55 /* Free the mutex */ 56 void 57 SDL_DestroyMutex(SDL_mutex * mutex) 58 { 59 ULONG ulRC; 60 HMTX hMtx = (HMTX)mutex; 61 62 ulRC = DosCloseMutexSem(hMtx); 63 if (ulRC != NO_ERROR) { 64 debug_os2("DosCloseMutexSem(), rc = %u", ulRC); 65 } 66 } 67 68 /* Lock the mutex */ 69 int 70 SDL_LockMutex(SDL_mutex * mutex) 71 { 72 ULONG ulRC; 73 HMTX hMtx = (HMTX)mutex; 74 75 if (hMtx == NULLHANDLE) 76 return SDL_SetError("Passed a NULL mutex"); 77 78 ulRC = DosRequestMutexSem(hMtx, SEM_INDEFINITE_WAIT); 79 if (ulRC != NO_ERROR) { 80 debug_os2("DosRequestMutexSem(), rc = %u", ulRC); 81 return -1; 82 } 83 84 return 0; 85 } 86 87 /* try Lock the mutex */ 88 int 89 SDL_TryLockMutex(SDL_mutex * mutex) 90 { 91 ULONG ulRC; 92 HMTX hMtx = (HMTX)mutex; 93 94 if (hMtx == NULLHANDLE) 95 return SDL_SetError("Passed a NULL mutex"); 96 97 ulRC = DosRequestMutexSem(hMtx, SEM_IMMEDIATE_RETURN); 98 99 if (ulRC == ERROR_TIMEOUT) 100 return SDL_MUTEX_TIMEDOUT; 101 102 if (ulRC != NO_ERROR) { 103 debug_os2("DosRequestMutexSem(), rc = %u", ulRC); 104 return -1; 105 } 106 107 return 0; 108 } 109 110 /* Unlock the mutex */ 111 int 112 SDL_UnlockMutex(SDL_mutex * mutex) 113 { 114 ULONG ulRC; 115 HMTX hMtx = (HMTX)mutex; 116 117 if (hMtx == NULLHANDLE) 118 return SDL_SetError("Passed a NULL mutex"); 119 120 ulRC = DosReleaseMutexSem(hMtx); 121 if (ulRC != NO_ERROR) 122 return SDL_SetError("DosReleaseMutexSem(), rc = %u", ulRC); 123 124 return 0; 125 } 126 127 #endif /* SDL_THREAD_OS2 */ 128 129 /* vi: set ts=4 sw=4 expandtab: */