SDL  2.0
SDL_sysmutex.cpp
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 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_internal.h"
22 
23 extern "C" {
24 #include "SDL_thread.h"
25 #include "SDL_systhread_c.h"
26 #include "SDL_log.h"
27 }
28 
29 #include <system_error>
30 
31 #include "SDL_sysmutex_c.h"
32 #include <Windows.h>
33 
34 
35 /* Create a mutex */
36 extern "C"
37 SDL_mutex *
39 {
40  /* Allocate and initialize the mutex */
41  try {
42  SDL_mutex * mutex = new SDL_mutex;
43  return mutex;
44  } catch (std::system_error & ex) {
45  SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
46  return NULL;
47  } catch (std::bad_alloc &) {
49  return NULL;
50  }
51 }
52 
53 /* Free the mutex */
54 extern "C"
55 void
57 {
58  if (mutex) {
59  delete mutex;
60  }
61 }
62 
63 /* Lock the semaphore */
64 extern "C"
65 int
67 {
68  if (mutex == NULL) {
69  SDL_SetError("Passed a NULL mutex");
70  return -1;
71  }
72 
73  try {
74  mutex->cpp_mutex.lock();
75  return 0;
76  } catch (std::system_error & ex) {
77  SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
78  return -1;
79  }
80 }
81 
82 /* TryLock the mutex */
83 int
85 {
86  int retval = 0;
87  if (mutex == NULL) {
88  return SDL_SetError("Passed a NULL mutex");
89  }
90 
91  if (mutex->cpp_mutex.try_lock() == false) {
93  }
94  return retval;
95 }
96 
97 /* Unlock the mutex */
98 extern "C"
99 int
101 {
102  if (mutex == NULL) {
103  SDL_SetError("Passed a NULL mutex");
104  return -1;
105  }
106 
107  mutex->cpp_mutex.unlock();
108  return 0;
109 }
110 
111 /* vi: set ts=4 sw=4 expandtab: */
SDL_systhread_c.h
NULL
#define NULL
Definition: begin_code.h:167
SDL_mutexP
int SDL_mutexP(SDL_mutex *mutex)
Definition: SDL_sysmutex.cpp:66
SDL_log.h
SDL_mutex
Definition: SDL_sysmutex.c:29
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_DestroyMutex
void SDL_DestroyMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.cpp:56
SDL_MUTEX_TIMEDOUT
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
SDL_mutexV
int SDL_mutexV(SDL_mutex *mutex)
Definition: SDL_sysmutex.cpp:100
SDL_thread.h
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_CreateMutex
SDL_mutex * SDL_CreateMutex(void)
Definition: SDL_sysmutex.cpp:38
SDL_mutex::cpp_mutex
std::recursive_mutex cpp_mutex
Definition: SDL_sysmutex_c.h:27
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_TryLockMutex
int SDL_TryLockMutex(SDL_mutex *mutex)
Definition: SDL_sysmutex.cpp:84
SDL_sysmutex_c.h