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