src/thread/epoc/SDL_syssem.cpp
changeset 2126 9c9c49b18693
parent 2125 fab12febc0f9
child 2127 3bcc26b74e42
     1.1 --- a/src/thread/epoc/SDL_syssem.cpp	Fri Jun 15 12:27:43 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,193 +0,0 @@
     1.4 -/*
     1.5 -    SDL - Simple DirectMedia Layer
     1.6 -    Copyright (C) 1997-2006 Sam Lantinga
     1.7 -
     1.8 -    This library is free software; you can redistribute it and/or
     1.9 -    modify it under the terms of the GNU Lesser General Public
    1.10 -    License as published by the Free Software Foundation; either
    1.11 -    version 2.1 of the License, or (at your option) any later version.
    1.12 -
    1.13 -    This library is distributed in the hope that it will be useful,
    1.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 -    Lesser General Public License for more details.
    1.17 -
    1.18 -    You should have received a copy of the GNU Lesser General Public
    1.19 -    License along with this library; if not, write to the Free Software
    1.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 -
    1.22 -    Sam Lantinga
    1.23 -    slouken@libsdl.org
    1.24 -*/
    1.25 -#include "SDL_config.h"
    1.26 -
    1.27 -/*
    1.28 -    SDL_syssem.cpp
    1.29 -
    1.30 -    Epoc version by Markus Mertama  (w@iki.fi)
    1.31 -*/
    1.32 -
    1.33 -/* Semaphore functions using the Win32 API */
    1.34 -
    1.35 -#include <e32std.h>
    1.36 -
    1.37 -#include "SDL_error.h"
    1.38 -#include "SDL_thread.h"
    1.39 -
    1.40 -
    1.41 -#define SDL_MUTEX_TIMEOUT -2
    1.42 -
    1.43 -struct SDL_semaphore
    1.44 - {
    1.45 - TInt handle;
    1.46 - TInt count;
    1.47 - };
    1.48 -
    1.49 -extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
    1.50 -extern TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2);
    1.51 -
    1.52 -TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2) 
    1.53 -    {
    1.54 -    TInt value = *((TInt*) aPtr2);
    1.55 -    return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
    1.56 -    }
    1.57 -
    1.58 -/* Create a semaphore */
    1.59 -SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
    1.60 -{
    1.61 -   RSemaphore s;
    1.62 -   TInt status = CreateUnique(NewSema, &s, &initial_value);
    1.63 -   if(status != KErrNone)
    1.64 -	 {
    1.65 -			SDL_SetError("Couldn't create semaphore");
    1.66 -	}
    1.67 -    SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;  
    1.68 -    sem->handle = s.Handle();
    1.69 -	sem->count = initial_value;
    1.70 -	return(sem);
    1.71 -}
    1.72 -
    1.73 -/* Free the semaphore */
    1.74 -void SDL_DestroySemaphore(SDL_sem *sem)
    1.75 -{
    1.76 -	if ( sem ) 
    1.77 -	{
    1.78 -    RSemaphore sema;
    1.79 -    sema.SetHandle(sem->handle);
    1.80 -	sema.Signal(sema.Count());
    1.81 -    sema.Close();
    1.82 -    delete sem;
    1.83 -	sem = NULL;
    1.84 -	}
    1.85 -}
    1.86 -
    1.87 -
    1.88 -  struct TInfo
    1.89 -    {
    1.90 -        TInfo(TInt aTime, TInt aHandle) : 
    1.91 -        iTime(aTime), iHandle(aHandle), iVal(0) {}
    1.92 -        TInt iTime;
    1.93 -        TInt iHandle;
    1.94 -        TInt iVal; 
    1.95 -    };
    1.96 -
    1.97 -TBool ThreadRun(TAny* aInfo)
    1.98 -    {
    1.99 -        TInfo* info = STATIC_CAST(TInfo*, aInfo);
   1.100 -        User::After(info->iTime);
   1.101 -        RSemaphore sema;
   1.102 -        sema.SetHandle(info->iHandle);
   1.103 -        sema.Signal();
   1.104 -        info->iVal = SDL_MUTEX_TIMEOUT;
   1.105 -        return 0;
   1.106 -    }
   1.107 -    
   1.108 -  
   1.109 -    
   1.110 -    
   1.111 -void _WaitAll(SDL_sem *sem)
   1.112 -    {
   1.113 -       //since SemTryWait may changed the counter.
   1.114 -       //this may not be atomic, but hopes it works.
   1.115 -    RSemaphore sema;
   1.116 -    sema.SetHandle(sem->handle);
   1.117 -    sema.Wait();
   1.118 -    while(sem->count < 0)
   1.119 -        {
   1.120 -        sema.Wait();
   1.121 -        }    
   1.122 -    }
   1.123 -
   1.124 -int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
   1.125 -{
   1.126 -	if ( ! sem ) {
   1.127 -		SDL_SetError("Passed a NULL sem");
   1.128 -		return -1;
   1.129 -	}
   1.130 -
   1.131 -	if ( timeout == SDL_MUTEX_MAXWAIT )
   1.132 -	    {
   1.133 -	    _WaitAll(sem);
   1.134 -		return SDL_MUTEX_MAXWAIT;
   1.135 -	    } 
   1.136 -	
   1.137 -	
   1.138 -	RThread thread;
   1.139 -	
   1.140 -	TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
   1.141 -	
   1.142 -    TInt status = CreateUnique(NewThread, &thread, info);
   1.143 -  
   1.144 -	if(status != KErrNone)
   1.145 -	    return status;
   1.146 -	
   1.147 -	thread.Resume();
   1.148 -	
   1.149 -	_WaitAll(sem);
   1.150 -	
   1.151 -	if(thread.ExitType() == EExitPending)
   1.152 -	    {
   1.153 -	        thread.Kill(SDL_MUTEX_TIMEOUT);
   1.154 -	    }
   1.155 -	
   1.156 -	thread.Close();
   1.157 -	
   1.158 -	return info->iVal;
   1.159 -}
   1.160 -
   1.161 -int SDL_SemTryWait(SDL_sem *sem)
   1.162 -{
   1.163 -    if(sem->count > 0)
   1.164 -        {
   1.165 -        sem->count--;
   1.166 -        }
   1.167 -    return SDL_MUTEX_TIMEOUT;
   1.168 -}
   1.169 -
   1.170 -int SDL_SemWait(SDL_sem *sem)
   1.171 -{
   1.172 -	return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
   1.173 -}
   1.174 -
   1.175 -/* Returns the current count of the semaphore */
   1.176 -Uint32 SDL_SemValue(SDL_sem *sem)
   1.177 -{
   1.178 -	if ( ! sem ) {
   1.179 -		SDL_SetError("Passed a NULL sem");
   1.180 -		return 0;
   1.181 -	}
   1.182 -	return sem->count;
   1.183 -}
   1.184 -
   1.185 -int SDL_SemPost(SDL_sem *sem)
   1.186 -{
   1.187 -	if ( ! sem ) {
   1.188 -		SDL_SetError("Passed a NULL sem");
   1.189 -		return -1;
   1.190 -	}
   1.191 -	sem->count++;
   1.192 -    RSemaphore sema;
   1.193 -    sema.SetHandle(sem->handle);
   1.194 -	sema.Signal();
   1.195 -	return 0;
   1.196 -}