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