module lib.sdl2.thread

Code Map

module lib.sdl2.thread;


enum SDL_THREAD_PRIORITY_LOW;
enum SDL_THREAD_PRIORITY_NORMAL;
enum SDL_THREAD_PRIORITY_HIGH;

alias SDL_threadID = u32;
alias SDL_TLSID = u32;
alias SDL_ThreadPriority = i32;
alias SDL_ThreadFunction = fn (void*) (i32);

struct SDL_Thread
{
}

//! Create a thread.
fn SDL_CreateThread(func: SDL_ThreadFunction, name: const(const(char)*), data: void*) SDL_Thread*;
//! Get the thread name, as it was specified in SDL_CreateThread(). This
//! function returns a pointer to a UTF-8 string that names the specified
//! thread, or NULL if it doesn't have a name. This is internal memory, not
//! to be free()'d by the caller, and remains valid until the specified
//! thread is cleaned up by SDL_WaitThread().
fn SDL_GetThreadName(thread: SDL_Thread*) char*;
//! Get the thread identifier for the current thread.
fn SDL_ThreadID() SDL_threadID;
//! Get the thread identifier for the specified thread.
fn SDL_GetThreadID(thread: SDL_Thread*) SDL_threadID;
//! Set the priority for the current thread
fn SDL_SetThreadPriority(priority: SDL_ThreadPriority) i32;
//! Wait for a thread to finish.
fn SDL_WaitThread(thread: SDL_Thread*, status: i32*);
//! \brief Create an identifier that is globally visible to all threads
//! but refers to data that is thread-specific.
fn SDL_TLSCreate() SDL_TLSID;
//! \brief Get the value associated with a thread local storage ID for the
//! current thread.
fn SDL_TLSGet(id: SDL_TLSID) void*;
//! \brief Set the value associated with a thread local storage ID for the
//! current thread.
fn SDL_TLSSet(id: SDL_TLSID, value: const(const(void)*), destructor: fn (void*) (void)) i32;
fn SDL_CreateThread(func: SDL_ThreadFunction, name: const(const(char)*), data: void*) SDL_Thread*

Create a thread.

Thread naming is a little complicated: Most systems have very small limits for the string length (BeOS has 32 bytes, Linux currently has 16, Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll have to see what happens with your system's debugger. The name should be UTF-8 (but using the naming limits of C identifiers is a better bet). There are no requirements for thread naming conventions, so long as the string is null-terminated UTF-8, but these guidelines are helpful in choosing a name:

http://stackoverflow.com/questions/149932/naming-conventions-for-threads

If a system imposes requirements, SDL will try to munge the string for it (truncate, etc), but the original string contents will be available from SDL_GetThreadName().

fn SDL_GetThreadName(thread: SDL_Thread*) char*

Get the thread name, as it was specified in SDL_CreateThread(). This function returns a pointer to a UTF-8 string that names the specified thread, or NULL if it doesn't have a name. This is internal memory, not to be free()'d by the caller, and remains valid until the specified thread is cleaned up by SDL_WaitThread().

fn SDL_ThreadID() SDL_threadID

Get the thread identifier for the current thread.

fn SDL_GetThreadID(thread: SDL_Thread*) SDL_threadID

Get the thread identifier for the specified thread.

Equivalent to SDL_ThreadID() if the specified thread is NULL.

fn SDL_SetThreadPriority(priority: SDL_ThreadPriority) i32

Set the priority for the current thread

fn SDL_WaitThread(thread: SDL_Thread*, status: i32*)

Wait for a thread to finish.

The return code for the thread function is placed in the area pointed to by \c status, if \c status is not NULL.

fn SDL_TLSCreate() SDL_TLSID

\brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.

\return The newly created thread local storage identifier, or 0 on error

\code static SDL_SpinLock tls_lock; static SDL_TLSID thread_local_storage;

void SetMyThreadData(void *value) { if (!thread_local_storage) { SDL_AtomicLock(&tls_lock); if (!thread_local_storage) { thread_local_storage = SDL_TLSCreate(); } SDL_AtomicUnLock(&tls_lock); } SDL_TLSSet(thread_local_storage, value); }

void *GetMyThreadData() { return SDL_TLSGet(thread_local_storage); } \endcode

\sa SDL_TLSGet() \sa SDL_TLSSet()

fn SDL_TLSGet(id: SDL_TLSID) void*

\brief Get the value associated with a thread local storage ID for the current thread.

\param id The thread local storage ID

\return The value associated with the ID for the current thread, or NULL if no value has been set.

\sa SDL_TLSCreate() \sa SDL_TLSSet()

fn SDL_TLSSet(id: SDL_TLSID, value: const(const(void)*), destructor: fn (void*) (void)) i32

\brief Set the value associated with a thread local storage ID for the current thread.

\param id The thread local storage ID \param value The value to associate with the ID for the current thread \param destructor A function called when the thread exits, to free the value.

\return 0 on success, -1 on error

\sa SDL_TLSCreate() \sa SDL_TLSGet()