src/thread/os2/SDL_sysmutex.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1358 c71e05b4dc2e
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
icculus@1190
     1
/*
icculus@1190
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1190
     4
icculus@1190
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1190
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 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
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1190
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1190
    18
icculus@1190
    19
    Sam Lantinga
icculus@1190
    20
    slouken@libsdl.org
icculus@1190
    21
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1190
    23
icculus@1190
    24
/* Mutex functions using the OS/2 API */
icculus@1190
    25
icculus@1190
    26
#define INCL_DOSERRORS
icculus@1190
    27
#define INCL_DOSSEMAPHORES
icculus@1190
    28
#include <os2.h>
icculus@1190
    29
icculus@1190
    30
#include "SDL_mutex.h"
icculus@1190
    31
icculus@1190
    32
icculus@1190
    33
struct SDL_mutex {
icculus@1190
    34
	HMTX hmtxID;
icculus@1190
    35
};
icculus@1190
    36
icculus@1190
    37
/* Create a mutex */
icculus@1190
    38
DECLSPEC SDL_mutex * SDLCALL SDL_CreateMutex(void)
icculus@1190
    39
{
icculus@1190
    40
  SDL_mutex *mutex;
icculus@1190
    41
  APIRET ulrc;
icculus@1190
    42
icculus@1190
    43
  /* Allocate mutex memory */
slouken@1336
    44
  mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
icculus@1190
    45
  if (mutex)
icculus@1190
    46
  {
icculus@1190
    47
    /* Create the mutex, with initial value signaled */
icculus@1190
    48
    ulrc = DosCreateMutexSem(NULL,                  // Create unnamed semaphore
icculus@1190
    49
                             &(mutex->hmtxID),      // Pointer to handle
icculus@1190
    50
                             0L,                    // Flags: create it private (not shared)
icculus@1190
    51
                             FALSE);                // Initial value: unowned
icculus@1190
    52
    if (ulrc!=NO_ERROR)
icculus@1190
    53
    {
icculus@1190
    54
      SDL_SetError("Couldn't create mutex");
slouken@1336
    55
      SDL_free(mutex);
icculus@1190
    56
      mutex = NULL;
icculus@1190
    57
    }
icculus@1190
    58
  } else {
icculus@1190
    59
    SDL_OutOfMemory();
icculus@1190
    60
  }
icculus@1190
    61
  return(mutex);
icculus@1190
    62
}
icculus@1190
    63
icculus@1190
    64
/* Free the mutex */
icculus@1190
    65
DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
icculus@1190
    66
{
icculus@1190
    67
  if ( mutex )
icculus@1190
    68
  {
icculus@1190
    69
    if ( mutex->hmtxID )
icculus@1190
    70
    {
icculus@1190
    71
      DosCloseMutexSem(mutex->hmtxID);
icculus@1190
    72
      mutex->hmtxID = 0;
icculus@1190
    73
    }
slouken@1336
    74
    SDL_free(mutex);
icculus@1190
    75
  }
icculus@1190
    76
}
icculus@1190
    77
icculus@1190
    78
/* Lock the mutex */
icculus@1190
    79
DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex *mutex)
icculus@1190
    80
{
icculus@1190
    81
  if ( mutex == NULL )
icculus@1190
    82
  {
icculus@1190
    83
    SDL_SetError("Passed a NULL mutex");
icculus@1190
    84
    return -1;
icculus@1190
    85
  }
icculus@1190
    86
  if ( DosRequestMutexSem(mutex->hmtxID, SEM_INDEFINITE_WAIT) != NO_ERROR )
icculus@1190
    87
  {
icculus@1190
    88
    SDL_SetError("Couldn't wait on mutex");
icculus@1190
    89
    return -1;
icculus@1190
    90
  }
icculus@1190
    91
  return(0);
icculus@1190
    92
}
icculus@1190
    93
icculus@1190
    94
/* Unlock the mutex */
icculus@1190
    95
DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex *mutex)
icculus@1190
    96
{
icculus@1190
    97
  if ( mutex == NULL )
icculus@1190
    98
  {
icculus@1190
    99
    SDL_SetError("Passed a NULL mutex");
icculus@1190
   100
    return -1;
icculus@1190
   101
  }
icculus@1190
   102
  if ( DosReleaseMutexSem(mutex->hmtxID) != NO_ERROR )
icculus@1190
   103
  {
icculus@1190
   104
    SDL_SetError("Couldn't release mutex");
icculus@1190
   105
    return -1;
icculus@1190
   106
  }
icculus@1190
   107
  return(0);
icculus@1190
   108
}