219c67e931ffa5b0173136141ada6f6d145d5424
[platform/upstream/SDL.git] / src / thread / stdcpp / SDL_systhread.cpp
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 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 /* Thread management routines for SDL */
24
25 extern "C" {
26 #include "SDL_thread.h"
27 #include "../SDL_thread_c.h"
28 #include "../SDL_systhread.h"
29 #include "SDL_log.h"
30 }
31
32 #include <mutex>
33 #include <thread>
34 #include <system_error>
35
36 #ifdef __WINRT__
37 #include <Windows.h>
38 #endif
39
40 static void
41 RunThread(void *args)
42 {
43     SDL_RunThread(args);
44 }
45
46 extern "C"
47 int
48 SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
49 {
50     try {
51         std::thread cpp_thread(RunThread, args);
52         thread->handle = (void *) new std::thread(std::move(cpp_thread));
53         return 0;
54     } catch (std::system_error & ex) {
55         SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
56         return -1;
57     } catch (std::bad_alloc &) {
58         SDL_OutOfMemory();
59         return -1;
60     }
61 }
62
63 extern "C"
64 void
65 SDL_SYS_SetupThread(const char *name)
66 {
67     // Make sure a thread ID gets assigned ASAP, for debugging purposes:
68     SDL_ThreadID();
69     return;
70 }
71
72 extern "C"
73 SDL_threadID
74 SDL_ThreadID(void)
75 {
76 #ifdef __WINRT__
77     return GetCurrentThreadId();
78 #else
79     // HACK: Mimick a thread ID, if one isn't otherwise available.
80     static thread_local SDL_threadID current_thread_id = 0;
81     static SDL_threadID next_thread_id = 1;
82     static std::mutex next_thread_id_mutex;
83
84     if (current_thread_id == 0) {
85         std::lock_guard<std::mutex> lock(next_thread_id_mutex);
86         current_thread_id = next_thread_id;
87         ++next_thread_id;
88     }
89
90     return current_thread_id;
91 #endif
92 }
93
94 extern "C"
95 int
96 SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
97 {
98     // Thread priorities do not look to be settable via C++11's thread
99     // interface, at least as of this writing (Nov 2012).  std::thread does
100     // provide access to the OS' native handle, however, and some form of
101     // priority-setting could, in theory, be done through this interface.
102     //
103     // WinRT: UPDATE (Aug 20, 2013): thread priorities cannot be changed
104     // on WinRT, at least not for any thread that's already been created.
105     // WinRT threads appear to be based off of the WinRT class,
106     // ThreadPool, more info on which can be found at:
107     // http://msdn.microsoft.com/en-us/library/windows/apps/windows.system.threading.threadpool.aspx
108     //
109     // For compatibility sake, 0 will be returned here.
110     return (0);
111 }
112
113 extern "C"
114 void
115 SDL_SYS_WaitThread(SDL_Thread * thread)
116 {
117     if ( ! thread) {
118         return;
119     }
120
121     try {
122         std::thread * cpp_thread = (std::thread *) thread->handle;
123         if (cpp_thread->joinable()) {
124             cpp_thread->join();
125         }
126     } catch (std::system_error &) {
127         // An error occurred when joining the thread.  SDL_WaitThread does not,
128         // however, seem to provide a means to report errors to its callers
129         // though!
130     }
131 }
132
133 extern "C"
134 void
135 SDL_SYS_DetachThread(SDL_Thread * thread)
136 {
137     if ( ! thread) {
138         return;
139     }
140
141     try {
142         std::thread * cpp_thread = (std::thread *) thread->handle;
143         if (cpp_thread->joinable()) {
144             cpp_thread->detach();
145         }
146     } catch (std::system_error &) {
147         // An error occurred when detaching the thread.  SDL_DetachThread does not,
148         // however, seem to provide a means to report errors to its callers
149         // though!
150     }
151 }
152
153 extern "C"
154 SDL_TLSData *
155 SDL_SYS_GetTLSData()
156 {
157     return SDL_Generic_GetTLSData();
158 }
159
160 extern "C"
161 int
162 SDL_SYS_SetTLSData(SDL_TLSData *data)
163 {
164     return SDL_Generic_SetTLSData(data);
165 }
166
167 /* vi: set ts=4 sw=4 expandtab: */