1/* 2 SDL - Simple DirectMedia Layer 3 Copyright (C) 1997-2012 Sam Lantinga 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Sam Lantinga 20 slouken@libsdl.org 21*/ 22#include "SDL_config.h" 23 24/* Mutex functions using the OS/2 API */ 25 26#define INCL_DOSERRORS 27#define INCL_DOSSEMAPHORES 28#include <os2.h> 29 30#include "SDL_mutex.h" 31 32 33struct SDL_mutex { 34 HMTX hmtxID; 35}; 36 37/* Create a mutex */ 38DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void) 39{ 40 SDL_mutex *mutex; 41 APIRET ulrc; 42 43 /* Allocate mutex memory */ 44 mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); 45 if (mutex) 46 { 47 /* Create the mutex, with initial value signaled */ 48 ulrc = DosCreateMutexSem(NULL, // Create unnamed semaphore 49 &(mutex->hmtxID), // Pointer to handle 50 0L, // Flags: create it private (not shared) 51 FALSE); // Initial value: unowned 52 if (ulrc!=NO_ERROR) 53 { 54 SDL_SetError("Couldn't create mutex"); 55 SDL_free(mutex); 56 mutex = NULL; 57 } 58 } else { 59 SDL_OutOfMemory(); 60 } 61 return(mutex); 62} 63 64/* Free the mutex */ 65DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex) 66{ 67 if ( mutex ) 68 { 69 if ( mutex->hmtxID ) 70 { 71 DosCloseMutexSem(mutex->hmtxID); 72 mutex->hmtxID = 0; 73 } 74 SDL_free(mutex); 75 } 76} 77 78/* Lock the mutex */ 79DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex) 80{ 81 if ( mutex == NULL ) 82 { 83 SDL_SetError("Passed a NULL mutex"); 84 return -1; 85 } 86 if ( DosRequestMutexSem(mutex->hmtxID, SEM_INDEFINITE_WAIT) != NO_ERROR ) 87 { 88 SDL_SetError("Couldn't wait on mutex"); 89 return -1; 90 } 91 return(0); 92} 93 94/* Unlock the mutex */ 95DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex) 96{ 97 if ( mutex == NULL ) 98 { 99 SDL_SetError("Passed a NULL mutex"); 100 return -1; 101 } 102 if ( DosReleaseMutexSem(mutex->hmtxID) != NO_ERROR ) 103 { 104 SDL_SetError("Couldn't release mutex"); 105 return -1; 106 } 107 return(0); 108} 109