zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_sysmutex.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "SDL_config.h"
22 
23 #if SDL_THREAD_WINDOWS
24 
25 /* Mutex functions using the Win32 API */
26 
27 #include "../../core/windows/SDL_windows.h"
28 
29 #include "SDL_mutex.h"
30 
31 
32 struct SDL_mutex
33 {
35 };
36 
37 /* Create a mutex */
38 SDL_mutex *
39 SDL_CreateMutex(void)
40 {
41  SDL_mutex *mutex;
42 
43  /* Allocate mutex memory */
44  mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
45  if (mutex) {
46  /* Initialize */
47  /* On SMP systems, a non-zero spin count generally helps performance */
48  InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
49  } else {
51  }
52  return (mutex);
53 }
54 
55 /* Free the mutex */
56 void
58 {
59  if (mutex) {
60  DeleteCriticalSection(&mutex->cs);
61  SDL_free(mutex);
62  }
63 }
64 
65 /* Lock the mutex */
66 int
67 SDL_LockMutex(SDL_mutex * mutex)
68 {
69  if (mutex == NULL) {
70  return SDL_SetError("Passed a NULL mutex");
71  }
72 
73  EnterCriticalSection(&mutex->cs);
74  return (0);
75 }
76 
77 /* TryLock the mutex */
78 int
80 {
81  int retval = 0;
82  if (mutex == NULL) {
83  return SDL_SetError("Passed a NULL mutex");
84  }
85 
86  if (TryEnterCriticalSection(&mutex->cs) == 0) {
87  retval = SDL_MUTEX_TIMEDOUT;
88  }
89  return retval;
90 }
91 
92 /* Unlock the mutex */
93 int
95 {
96  if (mutex == NULL) {
97  return SDL_SetError("Passed a NULL mutex");
98  }
99 
100  LeaveCriticalSection(&mutex->cs);
101  return (0);
102 }
103 
104 #endif /* SDL_THREAD_WINDOWS */
105 
106 /* vi: set ts=4 sw=4 expandtab: */
#define NULL
Definition: ftobjs.h:61
DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:73
DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void)
Definition: SDL_sysmutex.c:38
DECLSPEC void SDLCALL SDL_free(void *mem)
DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:160
DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:103
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
Definition: SDL_error.c:53
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
pthread_mutex_t CRITICAL_SECTION
Definition: alMain.h:101
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
void EnterCriticalSection(CRITICAL_SECTION *cs)
Definition: helpers.c:359
struct SDL_mutex SDL_mutex
Definition: SDL_mutex.h:59
void DeleteCriticalSection(CRITICAL_SECTION *cs)
Definition: helpers.c:353
DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.c:61
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
void LeaveCriticalSection(CRITICAL_SECTION *cs)
Definition: helpers.c:365