Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
93 lines (77 loc) · 2.19 KB

SDL_sysmutex.c

File metadata and controls

93 lines (77 loc) · 2.19 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Apr 26, 2001
Apr 26, 2001
20
*/
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
22
23
24
/* Mutex functions using the Win32 API */
Jan 25, 2011
Jan 25, 2011
25
#include "../../core/windows/SDL_windows.h"
Apr 26, 2001
Apr 26, 2001
26
27
28
29
#include "SDL_mutex.h"
Jul 10, 2006
Jul 10, 2006
30
31
struct SDL_mutex
{
Feb 17, 2011
Feb 17, 2011
32
CRITICAL_SECTION cs;
Apr 26, 2001
Apr 26, 2001
33
34
35
};
/* Create a mutex */
Jul 10, 2006
Jul 10, 2006
36
37
SDL_mutex *
SDL_CreateMutex(void)
Apr 26, 2001
Apr 26, 2001
38
{
Jul 10, 2006
Jul 10, 2006
39
SDL_mutex *mutex;
Apr 26, 2001
Apr 26, 2001
40
Jul 10, 2006
Jul 10, 2006
41
42
43
/* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if (mutex) {
Feb 17, 2011
Feb 17, 2011
44
/* Initialize */
Feb 17, 2011
Feb 17, 2011
45
#ifdef _WIN32_WCE
Feb 17, 2011
Feb 17, 2011
46
InitializeCriticalSection(&mutex->cs);
Feb 17, 2011
Feb 17, 2011
47
#else
Feb 17, 2011
Feb 17, 2011
48
/* On SMP systems, a non-zero spin count generally helps performance */
Feb 17, 2011
Feb 17, 2011
49
50
InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
#endif
Jul 10, 2006
Jul 10, 2006
51
52
53
54
} else {
SDL_OutOfMemory();
}
return (mutex);
Apr 26, 2001
Apr 26, 2001
55
56
57
}
/* Free the mutex */
Jul 10, 2006
Jul 10, 2006
58
59
void
SDL_DestroyMutex(SDL_mutex * mutex)
Apr 26, 2001
Apr 26, 2001
60
{
Jul 10, 2006
Jul 10, 2006
61
if (mutex) {
Feb 17, 2011
Feb 17, 2011
62
DeleteCriticalSection(&mutex->cs);
Jul 10, 2006
Jul 10, 2006
63
64
SDL_free(mutex);
}
Apr 26, 2001
Apr 26, 2001
65
66
67
}
/* Lock the mutex */
Jul 10, 2006
Jul 10, 2006
68
69
int
SDL_mutexP(SDL_mutex * mutex)
Apr 26, 2001
Apr 26, 2001
70
{
Jul 10, 2006
Jul 10, 2006
71
72
73
74
if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex");
return -1;
}
Feb 17, 2011
Feb 17, 2011
75
76
EnterCriticalSection(&mutex->cs);
Jul 10, 2006
Jul 10, 2006
77
return (0);
Apr 26, 2001
Apr 26, 2001
78
79
80
}
/* Unlock the mutex */
Jul 10, 2006
Jul 10, 2006
81
82
int
SDL_mutexV(SDL_mutex * mutex)
Apr 26, 2001
Apr 26, 2001
83
{
Jul 10, 2006
Jul 10, 2006
84
85
86
87
if (mutex == NULL) {
SDL_SetError("Passed a NULL mutex");
return -1;
}
Feb 17, 2011
Feb 17, 2011
88
89
LeaveCriticalSection(&mutex->cs);
Jul 10, 2006
Jul 10, 2006
90
return (0);
Apr 26, 2001
Apr 26, 2001
91
}
Jul 10, 2006
Jul 10, 2006
92
93
/* vi: set ts=4 sw=4 expandtab: */