Home | History | Annotate | Download | only in epoc
      1 /*
      2     SDL - Simple DirectMedia Layer
      3     Copyright (C) 1997-2006 Sam Lantinga
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Lesser General Public
      7     License as published by the Free Software Foundation; either
      8     version 2.1 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Lesser General Public License for more details.
     14 
     15     You should have received a copy of the GNU Lesser General Public
     16     License along with this library; if not, write to the Free Software
     17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     18 
     19     Sam Lantinga
     20     slouken (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 /*
     25     SDL_sysmutex.cpp
     26 
     27     Epoc version by Markus Mertama (w (at) iki.fi)
     28 */
     29 
     30 
     31 /* Mutex functions using the Win32 API */
     32 
     33 #include<e32std.h>
     34 
     35 #include "SDL_error.h"
     36 #include "SDL_mutex.h"
     37 
     38 
     39 
     40 struct SDL_mutex
     41     {
     42     TInt handle;
     43     };
     44 
     45 extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
     46 
     47 TInt NewMutex(const TDesC& aName, TAny* aPtr1, TAny*)
     48     {
     49     return ((RMutex*)aPtr1)->CreateGlobal(aName);
     50     }
     51 
     52 /* Create a mutex */
     53 SDL_mutex *SDL_CreateMutex(void)
     54 {
     55     RMutex rmutex;
     56 
     57     TInt status = CreateUnique(NewMutex, &rmutex, NULL);
     58 	if(status != KErrNone)
     59 	    {
     60 			SDL_SetError("Couldn't create mutex");
     61 		}
     62     SDL_mutex* mutex = new /*(ELeave)*/ SDL_mutex;
     63     mutex->handle = rmutex.Handle();
     64 	return(mutex);
     65 }
     66 
     67 /* Free the mutex */
     68 void SDL_DestroyMutex(SDL_mutex *mutex)
     69 {
     70 	if ( mutex )
     71 	{
     72     RMutex rmutex;
     73     rmutex.SetHandle(mutex->handle);
     74 	rmutex.Signal();
     75 	rmutex.Close();
     76 	delete(mutex);
     77     mutex = NULL;
     78 	}
     79 }
     80 
     81 /* Lock the mutex */
     82 int SDL_mutexP(SDL_mutex *mutex)
     83 {
     84 	if ( mutex == NULL ) {
     85 		SDL_SetError("Passed a NULL mutex");
     86 		return -1;
     87 	}
     88     RMutex rmutex;
     89     rmutex.SetHandle(mutex->handle);
     90 	rmutex.Wait();
     91 	return(0);
     92 }
     93 
     94 /* Unlock the mutex */
     95 int SDL_mutexV(SDL_mutex *mutex)
     96 {
     97 	if ( mutex == NULL ) {
     98 		SDL_SetError("Passed a NULL mutex");
     99 		return -1;
    100 	}
    101 	RMutex rmutex;
    102     rmutex.SetHandle(mutex->handle);
    103 	rmutex.Signal();
    104 	return(0);
    105 }
    106