1.3 API CHANGE: Add support for naming threads.
--HG-- extra : rebase_source : ae532d4b4d68ef86de0fc2cb6794a622e0841bce
This commit is contained in:
parent
dead491b27
commit
0863dee582
22 changed files with 183 additions and 45 deletions
|
@ -40,7 +40,7 @@ extern int SDL_SYS_CreateThread(SDL_Thread * thread, void *args);
|
|||
#endif
|
||||
|
||||
/* This function does any necessary setup in the child thread */
|
||||
extern void SDL_SYS_SetupThread(void);
|
||||
extern void SDL_SYS_SetupThread(const char *name);
|
||||
|
||||
/* This function sets the current thread priority */
|
||||
extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
|
||||
|
|
|
@ -188,25 +188,19 @@ typedef struct
|
|||
void
|
||||
SDL_RunThread(void *data)
|
||||
{
|
||||
thread_args *args;
|
||||
int (SDLCALL * userfunc) (void *);
|
||||
void *userdata;
|
||||
int *statusloc;
|
||||
thread_args *args = (thread_args *) data;
|
||||
int (SDLCALL * userfunc) (void *) = args->func;
|
||||
void *userdata = args->data;
|
||||
int *statusloc = &args->info->status;
|
||||
|
||||
/* Perform any system-dependent setup
|
||||
- this function cannot fail, and cannot use SDL_SetError()
|
||||
*/
|
||||
SDL_SYS_SetupThread();
|
||||
SDL_SYS_SetupThread(args->info->name);
|
||||
|
||||
/* Get the thread id */
|
||||
args = (thread_args *) data;
|
||||
args->info->threadid = SDL_ThreadID();
|
||||
|
||||
/* Figure out what function to run */
|
||||
userfunc = args->func;
|
||||
userdata = args->data;
|
||||
statusloc = &args->info->status;
|
||||
|
||||
/* Wake up the parent thread */
|
||||
SDL_SemPost(args->wait);
|
||||
|
||||
|
@ -217,12 +211,14 @@ SDL_RunThread(void *data)
|
|||
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
|
||||
#undef SDL_CreateThread
|
||||
DECLSPEC SDL_Thread *SDLCALL
|
||||
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data,
|
||||
SDL_CreateThread(int (SDLCALL * fn) (void *),
|
||||
const char *name, void *data,
|
||||
pfnSDL_CurrentBeginThread pfnBeginThread,
|
||||
pfnSDL_CurrentEndThread pfnEndThread)
|
||||
#else
|
||||
DECLSPEC SDL_Thread *SDLCALL
|
||||
SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
|
||||
SDL_CreateThread(int (SDLCALL * fn) (void *),
|
||||
const char *name, void *data)
|
||||
#endif
|
||||
{
|
||||
SDL_Thread *thread;
|
||||
|
@ -238,10 +234,21 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
|
|||
SDL_memset(thread, 0, (sizeof *thread));
|
||||
thread->status = -1;
|
||||
|
||||
/* Set up the arguments for the thread */
|
||||
if (name != NULL) {
|
||||
thread->name = SDL_strdup(name);
|
||||
if (thread->name == NULL) {
|
||||
SDL_OutOfMemory();
|
||||
SDL_free(thread);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up the arguments for the thread */
|
||||
args = (thread_args *) SDL_malloc(sizeof(*args));
|
||||
if (args == NULL) {
|
||||
SDL_OutOfMemory();
|
||||
SDL_free(thread->name);
|
||||
SDL_free(thread);
|
||||
return (NULL);
|
||||
}
|
||||
|
@ -250,6 +257,7 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
|
|||
args->info = thread;
|
||||
args->wait = SDL_CreateSemaphore(0);
|
||||
if (args->wait == NULL) {
|
||||
SDL_free(thread->name);
|
||||
SDL_free(thread);
|
||||
SDL_free(args);
|
||||
return (NULL);
|
||||
|
@ -270,6 +278,7 @@ SDL_CreateThread(int (SDLCALL * fn) (void *), void *data)
|
|||
} else {
|
||||
/* Oops, failed. Gotta free everything */
|
||||
SDL_DelThread(thread);
|
||||
SDL_free(thread->name);
|
||||
SDL_free(thread);
|
||||
thread = NULL;
|
||||
}
|
||||
|
@ -293,6 +302,12 @@ SDL_GetThreadID(SDL_Thread * thread)
|
|||
return id;
|
||||
}
|
||||
|
||||
const char *
|
||||
SDL_GetThreadName(SDL_Thread * thread)
|
||||
{
|
||||
return thread->name;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SetThreadPriority(SDL_ThreadPriority priority)
|
||||
{
|
||||
|
@ -308,6 +323,7 @@ SDL_WaitThread(SDL_Thread * thread, int *status)
|
|||
*status = thread->status;
|
||||
}
|
||||
SDL_DelThread(thread);
|
||||
SDL_free(thread->name);
|
||||
SDL_free(thread);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ struct SDL_Thread
|
|||
SYS_ThreadHandle handle;
|
||||
int status;
|
||||
SDL_error errbuf;
|
||||
char *name;
|
||||
void *data;
|
||||
};
|
||||
|
||||
|
|
|
@ -65,8 +65,13 @@ RunThread(void *data)
|
|||
int
|
||||
SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
||||
{
|
||||
/* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
|
||||
char name[B_OS_NAME_LENGTH];
|
||||
SDL_snprintf(name, sizeof (name), "%s", thread->name);
|
||||
name[sizeof (name) - 1] = '\0';
|
||||
|
||||
/* Create the thread and go! */
|
||||
thread->handle = spawn_thread(RunThread, "SDL", B_NORMAL_PRIORITY, args);
|
||||
thread->handle = spawn_thread(RunThread, name, B_NORMAL_PRIORITY, args);
|
||||
if ((thread->handle == B_NO_MORE_THREADS) ||
|
||||
(thread->handle == B_NO_MEMORY)) {
|
||||
SDL_SetError("Not enough resources to create thread");
|
||||
|
@ -77,8 +82,9 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
/* We set the thread name during SDL_SYS_CreateThread(). */
|
||||
/* Mask asynchronous signals for this thread */
|
||||
SDL_MaskSignals(NULL);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
int i;
|
||||
sigset_t mask;
|
||||
|
|
|
@ -38,7 +38,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,11 @@
|
|||
#include "SDL_config.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#if HAVE_PTHREAD_NP_H
|
||||
#include <pthread_np.h>
|
||||
#endif
|
||||
|
||||
#include <signal.h>
|
||||
#ifdef __LINUX__
|
||||
#include <sys/time.h>
|
||||
|
@ -28,6 +33,7 @@
|
|||
#include <sys/syscall.h>
|
||||
#endif
|
||||
|
||||
#include "SDL_platform.h"
|
||||
#include "SDL_thread.h"
|
||||
#include "../SDL_thread_c.h"
|
||||
#include "../SDL_systhread.h"
|
||||
|
@ -67,12 +73,27 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
return (0);
|
||||
}
|
||||
|
||||
/* make pthread_setname_np() a weak reference even without SDK support. */
|
||||
#if __MACOSX__ && (MAC_OS_X_VERSION_MAX_ALLOWED < 1060)
|
||||
int pthread_setname_np(const char*) __attribute__((weak_import,visibility("default")));
|
||||
#elif __IPHONEOS__ && (__IPHONE_OS_VERSION_MAX_ALLOWED < 30200)
|
||||
int pthread_setname_np(const char*) __attribute__((weak_import));
|
||||
#endif
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
int i;
|
||||
sigset_t mask;
|
||||
|
||||
#if __MACOSX__ || __IPHONEOS__
|
||||
if (pthread_setname_np != NULL) { pthread_setname_np(name); }
|
||||
#elif HAVE_PTHREAD_SETNAME_NP
|
||||
pthread_setname_np(pthread_self(), name);
|
||||
#elif HAVE_PTHREAD_SET_NAME_NP
|
||||
pthread_set_name_np(pthread_self(), name);
|
||||
#endif
|
||||
|
||||
/* Mask asynchronous signals for this thread */
|
||||
sigemptyset(&mask);
|
||||
for (i = 0; sig_list[i]; ++i) {
|
||||
|
|
|
@ -146,10 +146,38 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
|
|||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(void)
|
||||
#ifdef _MSC_VER
|
||||
#pragma pack(push,8)
|
||||
typedef struct tagTHREADNAME_INFO
|
||||
{
|
||||
return;
|
||||
DWORD dwType; /* must be 0x1000 */
|
||||
LPCSTR szName; /* pointer to name (in user addr space) */
|
||||
DWORD dwThreadID; /* thread ID (-1=caller thread) */
|
||||
DWORD dwFlags; /* reserved for future use, must be zero */
|
||||
} THREADNAME_INFO;
|
||||
#pragma pack(pop)
|
||||
#endif
|
||||
|
||||
void
|
||||
SDL_SYS_SetupThread(const char *name)
|
||||
{
|
||||
#ifdef _MSC_VER /* !!! FIXME: can we do SEH on other compilers yet? */
|
||||
/* This magic tells the debugger to name a thread if it's listening. */
|
||||
THREADNAME_INFO inf;
|
||||
info.dwType = 0x1000;
|
||||
info.szName = name;
|
||||
info.dwThreadID = (DWORD) -1;
|
||||
info.dwFlags = 0;
|
||||
|
||||
__try
|
||||
{
|
||||
RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
|
||||
}
|
||||
except(EXCEPTION_CONTINUE_EXECUTION)
|
||||
{
|
||||
/* The program itself should ignore this bogus exception. */
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
SDL_threadID
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue