SDL  2.0
SDL_systhread.cpp File Reference
#include "../../SDL_internal.h"
#include "SDL_thread.h"
#include "../SDL_thread_c.h"
#include "../SDL_systhread.h"
#include "SDL_log.h"
#include <mutex>
#include <thread>
#include <system_error>
+ Include dependency graph for SDL_systhread.cpp:

Go to the source code of this file.

Functions

static void RunThread (void *args)
 
int SDL_SYS_CreateThread (SDL_Thread *thread, void *args)
 
void SDL_SYS_SetupThread (const char *name)
 
SDL_threadID SDL_ThreadID (void)
 
int SDL_SYS_SetThreadPriority (SDL_ThreadPriority priority)
 
void SDL_SYS_WaitThread (SDL_Thread *thread)
 
void SDL_SYS_DetachThread (SDL_Thread *thread)
 
SDL_TLSDataSDL_SYS_GetTLSData ()
 
int SDL_SYS_SetTLSData (SDL_TLSData *data)
 

Function Documentation

static void RunThread ( void args)
static

Definition at line 41 of file SDL_systhread.cpp.

References SDL_RunThread().

Referenced by SDL_SYS_CreateThread().

42 {
43  SDL_RunThread(args);
44 }
void SDL_RunThread(void *data)
Definition: SDL_thread.c:264
int SDL_SYS_CreateThread ( SDL_Thread thread,
void args 
)

Definition at line 48 of file SDL_systhread.cpp.

References SDL_Thread::handle, RunThread(), SDL_OutOfMemory, and SDL_SetError.

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 &) {
59  return -1;
60  }
61 }
static void RunThread(void *args)
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
#define SDL_SetError
void SDL_SYS_DetachThread ( SDL_Thread thread)

Definition at line 135 of file SDL_systhread.cpp.

References SDL_Thread::handle.

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 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
SDL_TLSData* SDL_SYS_GetTLSData ( )

Definition at line 155 of file SDL_systhread.cpp.

References SDL_Generic_GetTLSData().

156 {
157  return SDL_Generic_GetTLSData();
158 }
SDL_TLSData * SDL_Generic_GetTLSData()
Definition: SDL_thread.c:123
int SDL_SYS_SetThreadPriority ( SDL_ThreadPriority  priority)

Definition at line 96 of file SDL_systhread.cpp.

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 }
int SDL_SYS_SetTLSData ( SDL_TLSData data)

Definition at line 162 of file SDL_systhread.cpp.

References SDL_Generic_SetTLSData().

163 {
164  return SDL_Generic_SetTLSData(data);
165 }
int SDL_Generic_SetTLSData(SDL_TLSData *storage)
Definition: SDL_thread.c:162
void SDL_SYS_SetupThread ( const char *  name)

Definition at line 65 of file SDL_systhread.cpp.

References SDL_ThreadID().

66 {
67  // Make sure a thread ID gets assigned ASAP, for debugging purposes:
68  SDL_ThreadID();
69  return;
70 }
SDL_threadID SDL_ThreadID(void)
void SDL_SYS_WaitThread ( SDL_Thread thread)

Definition at line 115 of file SDL_systhread.cpp.

References SDL_Thread::handle.

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 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
SDL_threadID SDL_ThreadID ( void  )

Get the thread identifier for the current thread.

Definition at line 74 of file SDL_systhread.cpp.

References lock, and mutex.

Referenced by SDL_SYS_SetupThread().

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 }
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_mutex * lock
Definition: SDL_events.c:75
unsigned long SDL_threadID
Definition: SDL_thread.h:49