19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Semaphore functions using the OS/2 API */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define INCL_DOS
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define INCL_DOSERRORS
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define INCL_DOSSEMAPHORES
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <os2.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_semaphore {
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HMTX id;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HEV  changed;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Uint32 value;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a semaphore */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value)
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_sem *sem;
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ULONG ulrc;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Allocate sem memory */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( sem ) {
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* Create the mutex semaphore */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( ulrc ) {
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_SetError("Couldn't create semaphore");
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_free(sem);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        sem = NULL;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                } else
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    DosCreateEventSem(NULL, &(sem->changed), 0, FALSE);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    sem->value = initial_value;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    DosReleaseMutexSem(sem->id);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_OutOfMemory();
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(sem);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Free the semaphore */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( sem ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( sem->id ) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosCloseEventSem(sem->changed);
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosCloseMutexSem(sem->id);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        sem->id = 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_free(sem);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ULONG ulrc;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ! sem ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_SetError("Passed a NULL sem");
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( timeout == SDL_MUTEX_MAXWAIT ) {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           while (1) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (ulrc) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 /* if error waiting mutex */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 SDL_SetError("DosRequestMutexSem() failed");
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 return -1;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              } else if (sem->value) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        sem->value--;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosReleaseMutexSem(sem->id);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        return 0;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     } else {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        ULONG ulPostCount;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosResetEventSem(sem->changed, &ulPostCount);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosReleaseMutexSem(sem->id);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        /* continue waiting until somebody posts the semaphore */
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           }
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( timeout == 0 )
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (ulrc==NO_ERROR)
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (sem->value)
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    sem->value--;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    DosReleaseMutexSem(sem->id);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    return 0;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                } else
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    DosReleaseMutexSem(sem->id);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    return SDL_MUTEX_TIMEDOUT;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } else
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_SetError("DosRequestMutexSem() failed");
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (ulrc) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               /* if error waiting mutex */
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               SDL_SetError("DosRequestMutexSem() failed");
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               return -1;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            } else
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (sem->value) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                sem->value--;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DosReleaseMutexSem(sem->id);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return 0;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              } else {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ULONG ulPostCount;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DosResetEventSem(sem->changed, &ulPostCount);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                DosReleaseMutexSem(sem->id);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* continue waiting until somebody posts the semaphore */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ulrc = DosWaitEventSem(sem->changed, timeout);
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (ulrc==NO_ERROR)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  return 0;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  return SDL_MUTEX_TIMEDOUT;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              }
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* never reached */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem)
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return SDL_SemWaitTimeout(sem, 0);
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem)
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Returns the current count of the semaphore */
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ! sem ) {
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_SetError("Passed a NULL sem");
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return 0;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return sem->value;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem)
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ! sem ) {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_SetError("Passed a NULL sem");
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_SetError("DosRequestMutexSem() failed");
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sem->value++;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        DosPostEventSem(sem->changed);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        DosReleaseMutexSem(sem->id);
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
193