Big cleanup part 1.
[SDL.s60v3.git] / src / thread / symbian / SDL_sysmutex.cpp
blob4ddef4c20a1143ffbc500152a1ff6a71c648968e
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Sam Lantinga
20 slouken@devolution.com
24 SDL_sysmutex.cpp
26 Epoc version by Markus Mertama (w@iki.fi)
29 /* Mutex functions using the Win32 API */
31 //#include <stdio.h>
32 //#include <stdlib.h>
34 #include <e32std.h>
36 #include "epoc_sdl.h"
38 #include "SDL_error.h"
39 #include "SDL_mutex.h"
41 struct SDL_mutex
43 TInt handle;
46 extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
48 TInt NewMutex(const TDesC& aName, TAny* aPtr1, TAny*)
50 return ((RMutex*)aPtr1)->CreateGlobal(aName);
53 void DeleteMutex(TAny* aMutex)
55 SDL_DestroyMutex ((SDL_mutex*) aMutex);
58 /* Create a mutex */
59 SDL_mutex *SDL_CreateMutex(void)
61 RMutex rmutex;
63 TInt status = CreateUnique(NewMutex, &rmutex, NULL);
64 if(status != KErrNone)
66 SDL_SetError("Couldn't create mutex");
68 SDL_mutex* mutex = new SDL_mutex;
69 mutex->handle = rmutex.Handle();
70 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem(DeleteMutex, mutex));
71 return(mutex);
74 /* Free the mutex */
75 void SDL_DestroyMutex(SDL_mutex *mutex)
77 if ( mutex )
79 RMutex rmutex;
80 rmutex.SetHandle(mutex->handle);
81 if(rmutex.IsHeld())
83 rmutex.Signal();
85 rmutex.Close();
86 EpocSdlEnv::RemoveCleanupItem(mutex);
87 delete(mutex);
88 mutex = NULL;
92 /* Lock the mutex */
93 int SDL_mutexP(SDL_mutex *mutex)
95 if ( mutex == NULL ) {
96 SDL_SetError("Passed a NULL mutex");
97 return -1;
99 RMutex rmutex;
100 rmutex.SetHandle(mutex->handle);
101 rmutex.Wait();
102 return(0);
105 /* Unlock the mutex */
106 int SDL_mutexV(SDL_mutex *mutex)
108 if ( mutex == NULL ) {
109 SDL_SetError("Passed a NULL mutex");
110 return -1;
112 RMutex rmutex;
113 rmutex.SetHandle(mutex->handle);
114 rmutex.Signal();
115 return(0);