SDL_sysmutex.cpp revision 9682c8870b8ff5e4ac2e4c70b759f791c6f38c1f
1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
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.
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    Library General Public License for more details.
14
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
18
19    Sam Lantinga
20    slouken@devolution.com
21*/
22
23/*
24    SDL_sysmutex.cpp
25
26    Epoc version by Markus Mertama (w@iki.fi)
27*/
28
29
30#ifdef SAVE_RCSID
31static char rcsid =
32 "@(#) $Id: SDL_sysmutex.c,v 1.1.2.3 2000/06/22 15:25:23 hercules Exp $";
33#endif
34
35/* Mutex functions using the Win32 API */
36
37//#include <stdio.h>
38//#include <stdlib.h>
39
40#include <e32std.h>
41
42#include "epoc_sdl.h"
43
44#include "SDL_error.h"
45#include "SDL_mutex.h"
46
47
48#ifdef EKA2 //???
49struct SDL_mutex
50    {
51    TInt handle;
52    };
53#else
54struct _SDL_mutex
55    {
56    TInt handle;
57    };
58#endif
59
60extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
61
62TInt NewMutex(const TDesC& aName, TAny* aPtr1, TAny*)
63    {
64    return ((RMutex*)aPtr1)->CreateGlobal(aName);
65    }
66
67void DeleteMutex(TAny* aMutex)
68    {
69    SDL_DestroyMutex ((SDL_mutex*) aMutex);
70    }
71
72/* Create a mutex */
73SDL_mutex *SDL_CreateMutex(void)
74{
75    RMutex rmutex;
76
77    TInt status = CreateUnique(NewMutex, &rmutex, NULL);
78	if(status != KErrNone)
79	    {
80			SDL_SetError("Couldn't create mutex");
81		}
82    SDL_mutex* mutex = new /*(ELeave)*/ SDL_mutex;
83    mutex->handle = rmutex.Handle();
84    EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem(DeleteMutex, mutex));
85	return(mutex);
86}
87
88/* Free the mutex */
89void SDL_DestroyMutex(SDL_mutex *mutex)
90{
91	if ( mutex )
92	{
93    RMutex rmutex;
94    rmutex.SetHandle(mutex->handle);
95    if(rmutex.IsHeld())
96        {
97	    rmutex.Signal();
98        }
99	rmutex.Close();
100	EpocSdlEnv::RemoveCleanupItem(mutex);
101	delete(mutex);
102    mutex = NULL;
103	}
104}
105
106/* Lock the mutex */
107int SDL_mutexP(SDL_mutex *mutex)
108{
109	if ( mutex == NULL ) {
110		SDL_SetError("Passed a NULL mutex");
111		return -1;
112	}
113    RMutex rmutex;
114    rmutex.SetHandle(mutex->handle);
115	rmutex.Wait();
116	return(0);
117}
118
119/* Unlock the mutex */
120int SDL_mutexV(SDL_mutex *mutex)
121{
122	if ( mutex == NULL ) {
123		SDL_SetError("Passed a NULL mutex");
124		return -1;
125	}
126	RMutex rmutex;
127    rmutex.SetHandle(mutex->handle);
128	rmutex.Signal();
129	return(0);
130}
131