#include "../../SDL_internal.h"
#if SDL_THREAD_OS2
#include "SDL_thread.h"
#include "SDL_systhread_c.h"
#include "../../core/os2/SDL_os2.h"
#define INCL_DOSSEMAPHORES
#define INCL_DOSERRORS
#include <os2.h>
struct SDL_mutex {
HMTX _handle;
};
SDL_mutex *
SDL_CreateMutex(void)
{
ULONG ulRC;
HMTX hMtx;
ulRC = DosCreateMutexSem(NULL, &hMtx, 0, FALSE);
if (ulRC != NO_ERROR) {
debug_os2("DosCreateMutexSem(), rc = %u", ulRC);
return NULL;
}
return (SDL_mutex *)hMtx;
}
void
SDL_DestroyMutex(SDL_mutex * mutex)
{
HMTX hMtx = (HMTX)mutex;
if (hMtx != NULLHANDLE) {
const ULONG ulRC = DosCloseMutexSem(hMtx);
if (ulRC != NO_ERROR) {
debug_os2("DosCloseMutexSem(), rc = %u", ulRC);
}
}
}
int
SDL_LockMutex(SDL_mutex * mutex)
{
ULONG ulRC;
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
return SDL_InvalidParamError("mutex");
ulRC = DosRequestMutexSem(hMtx, SEM_INDEFINITE_WAIT);
if (ulRC != NO_ERROR) {
debug_os2("DosRequestMutexSem(), rc = %u", ulRC);
return -1;
}
return 0;
}
int
SDL_TryLockMutex(SDL_mutex * mutex)
{
ULONG ulRC;
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
return SDL_InvalidParamError("mutex");
ulRC = DosRequestMutexSem(hMtx, SEM_IMMEDIATE_RETURN);
if (ulRC == ERROR_TIMEOUT)
return SDL_MUTEX_TIMEDOUT;
if (ulRC != NO_ERROR) {
debug_os2("DosRequestMutexSem(), rc = %u", ulRC);
return -1;
}
return 0;
}
int
SDL_UnlockMutex(SDL_mutex * mutex)
{
ULONG ulRC;
HMTX hMtx = (HMTX)mutex;
if (hMtx == NULLHANDLE)
return SDL_InvalidParamError("mutex");
ulRC = DosReleaseMutexSem(hMtx);
if (ulRC != NO_ERROR)
return SDL_SetError("DosReleaseMutexSem(), rc = %u", ulRC);
return 0;
}
#endif