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