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