src/thread/os2/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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 
    23 /* Mutex functions using the OS/2 API */
    24 
    25 #define INCL_DOSERRORS
    26 #define INCL_DOSSEMAPHORES
    27 #include <os2.h>
    28 
    29 #include "SDL_mutex.h"
    30 
    31 
    32 struct SDL_mutex {
    33 	HMTX hmtxID;
    34 };
    35 
    36 /* Create a mutex */
    37 DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void)
    38 {
    39   SDL_mutex *mutex;
    40   APIRET ulrc;
    41 
    42   /* Allocate mutex memory */
    43   mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
    44   if (mutex)
    45   {
    46     /* Create the mutex, with initial value signaled */
    47     ulrc = DosCreateMutexSem(NULL,                  // Create unnamed semaphore
    48                              &(mutex->hmtxID),      // Pointer to handle
    49                              0L,                    // Flags: create it private (not shared)
    50                              FALSE);                // Initial value: unowned
    51     if (ulrc!=NO_ERROR)
    52     {
    53       SDL_SetError("Couldn't create mutex");
    54       SDL_free(mutex);
    55       mutex = NULL;
    56     }
    57   } else {
    58     SDL_OutOfMemory();
    59   }
    60   return(mutex);
    61 }
    62 
    63 /* Free the mutex */
    64 DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
    65 {
    66   if ( mutex )
    67   {
    68     if ( mutex->hmtxID )
    69     {
    70       DosCloseMutexSem(mutex->hmtxID);
    71       mutex->hmtxID = 0;
    72     }
    73     SDL_free(mutex);
    74   }
    75 }
    76 
    77 /* Lock the mutex */
    78 DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex)
    79 {
    80   if ( mutex == NULL )
    81   {
    82     SDL_SetError("Passed a NULL mutex");
    83     return -1;
    84   }
    85   if ( DosRequestMutexSem(mutex->hmtxID, SEM_INDEFINITE_WAIT) != NO_ERROR )
    86   {
    87     SDL_SetError("Couldn't wait on mutex");
    88     return -1;
    89   }
    90   return(0);
    91 }
    92 
    93 /* Unlock the mutex */
    94 DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex)
    95 {
    96   if ( mutex == NULL )
    97   {
    98     SDL_SetError("Passed a NULL mutex");
    99     return -1;
   100   }
   101   if ( DosReleaseMutexSem(mutex->hmtxID) != NO_ERROR )
   102   {
   103     SDL_SetError("Couldn't release mutex");
   104     return -1;
   105   }
   106   return(0);
   107 }