3 {* The SDL thread structure, defined in SDL_thread.c *}
6 {* The SDL thread priority
8 * Note: On many systems you require special privileges to set high priority.
11 TSDL_ThreadPriority = (SDL_THREAD_PRIORITY_LOW,
12 SDL_THREAD_PRIORITY_NORMAL,
13 SDL_THREAD_PRIORITY_HIGH);
15 {* The function passed to SDL_CreateThread()
16 It is passed a void* user context parameter and returns an int.
18 PSDL_ThreadFunction = ^TSDL_ThreadFunction;
19 TSDL_ThreadFunction = function(data: Pointer): Integer; cdecl;
21 {* The SDL thread ID *}
22 TSDL_ThreadID = LongWord;
24 PSDL_Thread = Pointer;
27 PSDL_Thread = ^TSDL_Thread;
29 threadid: TSDL_ThreadID;
37 TSDL_TLSID = Cardinal;
43 * We compile SDL into a DLL. This means, that it's the DLL which
44 * creates a new thread for the calling process with the SDL_CreateThread()
45 * API. There is a problem with this, that only the RTL of the SDL.DLL will
46 * be initialized for those threads, and not the RTL of the calling
49 * To solve this, we make a little hack here.
51 * We'll always use the caller's _beginthread() and _endthread() APIs to
52 * start a new thread. This way, if it's the SDL.DLL which uses this API,
53 * then the RTL of SDL.DLL will be used to create the new thread, and if it's
54 * the application, then the RTL of the application will be used.
57 * Always use the _beginthread() and _endthread() of the calling runtime
60 {$DEFINE SDL_PASSED_BEGINTHREAD_ENDTHREAD}
67 TpfnSDL_CurrentBeginThread = function(SecurityAttributes: Pointer; StackSize: LongWord; ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord; var ThreadId: TThreadID): Integer;
69 TpfnSDL_CurrentEndThread = procedure(ExitCode: Integer);
74 function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar; data: Pointer; pfnBeginThread: TpfnSDL_CurrentBeginThread; pfnEndThread: TpfnSDL_CurrentEndThread): PSDL_Thread; cdecl; overload; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateThread' {$ENDIF} {$ENDIF};
79 function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar; data: Pointer): PSDL_Thread; overload;
86 * Thread naming is a little complicated: Most systems have very small
87 * limits for the string length (BeOS has 32 bytes, Linux currently has 16,
88 * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
89 * have to see what happens with your system's debugger. The name should be
90 * UTF-8 (but using the naming limits of C identifiers is a better bet).
91 * There are no requirements for thread naming conventions, so long as the
92 * string is null-terminated UTF-8, but these guidelines are helpful in
95 * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
97 * If a system imposes requirements, SDL will try to munge the string for
98 * it (truncate, etc), but the original string contents will be available
99 * from SDL_GetThreadName().
101 function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar; data: Pointer): PSDL_Thread; cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateThread' {$ENDIF} {$ENDIF};
106 * Get the thread name, as it was specified in SDL_CreateThread().
107 * This function returns a pointer to a UTF-8 string that names the
108 * specified thread, or NULL if it doesn't have a name. This is internal
109 * memory, not to be free()'d by the caller, and remains valid until the
110 * specified thread is cleaned up by SDL_WaitThread().
112 function SDL_GetThreadName(thread: PSDL_Thread): PAnsiChar cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetThreadName' {$ENDIF}{$ENDIF};
115 * Get the thread identifier for the current thread.
117 function SDL_ThreadID: TSDL_ThreadID cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_ThreadID' {$ENDIF}{$ENDIF};
120 * Get the thread identifier for the specified thread.
122 * Equivalent to SDL_ThreadID() if the specified thread is NULL.
124 function SDL_GetThreadID(thread: PSDL_Thread): TSDL_ThreadID cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_GetThreadID' {$ENDIF}{$ENDIF};
127 * Set the priority for the current thread
129 function SDL_SetThreadPriority(priority: TSDL_ThreadPriority): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SetThreadPriority' {$ENDIF}{$ENDIF};
132 * Wait for a thread to finish.
134 * The return code for the thread function is placed in the area
135 * pointed to by status, if status is not NULL.
137 procedure SDL_WaitThread(thread: PSDL_Thread; status: PInt) cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_WaitThread' {$ENDIF}{$ENDIF};
140 * Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
142 * The newly created thread local storage identifier, or 0 on error
144 * static SDL_SpinLock tls_lock;
145 * static SDL_TLSID thread_local_storage;
147 * void SetMyThreadData(void *value)
149 * if (!thread_local_storage) {
150 * SDL_AtomicLock(&tls_lock);
151 * if (!thread_local_storage) {
152 * thread_local_storage = SDL_TLSCreate();
154 * SDL_AtomicUnLock(&tls_lock);
156 * SDL_TLSSet(thread_local_storage, value);
159 * void *GetMyThreadData(void)
161 * return SDL_TLSGet(thread_local_storage);
167 function SDL_TLSCreate: TSDL_TLSID cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSCreate' {$ENDIF} {$ENDIF};
170 * Get the value associated with a thread local storage ID for the current thread.
172 * id The thread local storage ID
174 * The value associated with the ID for the current thread, or NULL if no value has been set.
179 function SDL_TLSGet(id: TSDL_TLSID): Pointer cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSGet' {$ENDIF} {$ENDIF};
182 * Set the value associated with a thread local storage ID for the current thread.
184 * id The thread local storage ID
185 * value The value to associate with the ID for the current thread
186 * destructor_ A function called when the thread exits, to free the value.
188 * 0 on success, -1 on error
193 function SDL_TLSSet(id: TSDL_TLSID; value: Pointer; destructor_: Pointer): SInt32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSSet' {$ENDIF} {$ENDIF};