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 Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 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    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@devolution.com
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_syssem.cpp
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Epoc version by Markus Mertama  (w@iki.fi)
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SAVE_RCSID
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char rcsid =
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "@(#) $Id: SDL_syssem.c,v 1.1.2.4 2000/06/22 15:24:48 hercules Exp $";
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Semaphore functions using the Win32 API */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#include <stdio.h>
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#include <stdlib.h>
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <e32std.h>
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_MUTEX_TIMEOUT -2
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct SDL_semaphore
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall TInt handle;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall TInt count;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall };
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef EKA2
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallTInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TInt value = *((TInt*) aPtr2);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create a semaphore */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   RSemaphore s;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   TInt status = CreateUnique(NewSema, &s, &initial_value);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if(status != KErrNone)
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't create semaphore");
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sem->handle = s.Handle();
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sem->count = initial_value;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(sem);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Free the semaphore */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DestroySemaphore(SDL_sem *sem)
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sem )
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RSemaphore sema;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.SetHandle(sem->handle);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while(--sem->count)
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    sema.Signal();
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.Close();
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    delete sem;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sem = NULL;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef EKA2
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  struct TInfo
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TInfo(TInt aTime, TInt aHandle) :
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iTime(aTime), iHandle(aHandle), iVal(0) {}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TInt iTime;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TInt iHandle;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TInt iVal;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    };
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallTBool ThreadRun(TAny* aInfo)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TInfo* info = STATIC_CAST(TInfo*, aInfo);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        User::After(info->iTime);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        RSemaphore sema;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sema.SetHandle(info->iHandle);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sema.Signal();
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        info->iVal = SDL_MUTEX_TIMEOUT;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid _WaitAll(SDL_sem *sem)
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       //since SemTryWait may changed the counter.
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       //this may not be atomic, but hopes it works.
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RSemaphore sema;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.SetHandle(sem->handle);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.Wait();
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while(sem->count < 0)
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sema.Wait();
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL sem");
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( timeout == SDL_MUTEX_MAXWAIT )
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    _WaitAll(sem);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_MUTEX_MAXWAIT;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef EKA2
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RSemaphore sema;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.SetHandle(sem->handle);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if(KErrNone == sema.Wait(timeout))
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    	return 0;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	RThread thread;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TInt status = CreateUnique(NewThread, &thread, info);
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(status != KErrNone)
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return status;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread.Resume();
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_WaitAll(sem);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(thread.ExitType() == EExitPending)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    {
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        thread.Kill(SDL_MUTEX_TIMEOUT);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	thread.Close();
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return info->iVal;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemTryWait(SDL_sem *sem)
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if(sem->count > 0)
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sem->count--;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return SDL_MUTEX_TIMEOUT;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemWait(SDL_sem *sem)
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Returns the current count of the semaphore */
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_SemValue(SDL_sem *sem)
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL sem");
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return sem->count;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SemPost(SDL_sem *sem)
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! sem ) {
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Passed a NULL sem");
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sem->count++;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RSemaphore sema;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sema.SetHandle(sem->handle);
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sema.Signal();
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
215