src/thread/os2/SDL_sysmutex.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 23 Nov 2005 07:29:56 +0000
changeset 1190 173c063d4f55
child 1312 c9b51268668f
permissions -rw-r--r--
OS/2 port!

This was mostly, if not entirely, written by "Doodle" and "Caetano":
doodle@scenergy.dfmk.hu
daniel@caetano.eng.br

--ryan.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Mutex functions using the OS/2 API */
    29 
    30 #include <stdio.h>
    31 #include <stdlib.h>
    32 #define INCL_DOSERRORS
    33 #define INCL_DOSSEMAPHORES
    34 #include <os2.h>
    35 
    36 #include "SDL_error.h"
    37 #include "SDL_mutex.h"
    38 
    39 
    40 struct SDL_mutex {
    41 	HMTX hmtxID;
    42 };
    43 
    44 /* Create a mutex */
    45 DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void)
    46 {
    47   SDL_mutex *mutex;
    48   APIRET ulrc;
    49 
    50   /* Allocate mutex memory */
    51   mutex = (SDL_mutex *)malloc(sizeof(*mutex));
    52   if (mutex)
    53   {
    54     /* Create the mutex, with initial value signaled */
    55     ulrc = DosCreateMutexSem(NULL,                  // Create unnamed semaphore
    56                              &(mutex->hmtxID),      // Pointer to handle
    57                              0L,                    // Flags: create it private (not shared)
    58                              FALSE);                // Initial value: unowned
    59     if (ulrc!=NO_ERROR)
    60     {
    61       SDL_SetError("Couldn't create mutex");
    62       free(mutex);
    63       mutex = NULL;
    64     }
    65   } else {
    66     SDL_OutOfMemory();
    67   }
    68   return(mutex);
    69 }
    70 
    71 /* Free the mutex */
    72 DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
    73 {
    74   if ( mutex )
    75   {
    76     if ( mutex->hmtxID )
    77     {
    78       DosCloseMutexSem(mutex->hmtxID);
    79       mutex->hmtxID = 0;
    80     }
    81     free(mutex);
    82   }
    83 }
    84 
    85 /* Lock the mutex */
    86 DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex)
    87 {
    88   if ( mutex == NULL )
    89   {
    90     SDL_SetError("Passed a NULL mutex");
    91     return -1;
    92   }
    93   if ( DosRequestMutexSem(mutex->hmtxID, SEM_INDEFINITE_WAIT) != NO_ERROR )
    94   {
    95     SDL_SetError("Couldn't wait on mutex");
    96     return -1;
    97   }
    98   return(0);
    99 }
   100 
   101 /* Unlock the mutex */
   102 DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex)
   103 {
   104   if ( mutex == NULL )
   105   {
   106     SDL_SetError("Passed a NULL mutex");
   107     return -1;
   108   }
   109   if ( DosReleaseMutexSem(mutex->hmtxID) != NO_ERROR )
   110   {
   111     SDL_SetError("Couldn't release mutex");
   112     return -1;
   113   }
   114   return(0);
   115 }