added another thread call and fixed up some thread creation logic

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@2834 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
TimeServ 2007-12-30 20:05:49 +00:00
parent 776980a491
commit 16caf0e1da
6 changed files with 105 additions and 22 deletions

View file

@ -590,17 +590,34 @@ void Sys_SaveClipboard(char *text) {
/* Thread creation calls */ /* Thread creation calls */
typedef void *(*pfunction_t)(void *); typedef void *(*pfunction_t)(void *);
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize)
{ {
pthread_t thread; pthread_t *thread;
pthread_attr_t attr; pthread_attr_t attr;
thread = (pthread_t *)malloc(sizeof(pthread_t));
if (!thread)
return NULL;
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (stacksize < PTHREAD_STACK_MIN) if (stacksize < PTHREAD_STACK_MIN)
stacksize = PTHREAD_STACK_MIN; stacksize = PTHREAD_STACK_MIN;
pthread_attr_setstacksize(&attr, stacksize); pthread_attr_setstacksize(&attr, stacksize);
if (pthread_create(thread, &attr, (pfunction_t)func, args))
{
free(thread);
thread = NULL;
}
pthread_attr_destroy(&attr);
return !pthread_create(&thread, &attr, (pfunction_t)func, args); return (void *)thread;
}
void Sys_WaitOnThread(void *thread)
{
pthread_join((pthread_t *)thread, NULL);
free(thread);
} }
/* Mutex calls */ /* Mutex calls */

View file

@ -437,7 +437,8 @@ void Sys_LowFPPrecision (void)
#ifdef MULTITHREAD #ifdef MULTITHREAD
/* Everything here is stubbed because I don't know MorphOS */ /* Everything here is stubbed because I don't know MorphOS */
/* Thread creation calls */ /* Thread creation calls */
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) { return FALSE; } void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize) { return NULL; }
void Sys_WaitOnThread(void *thread) {}
/* Mutex calls */ /* Mutex calls */
void *Sys_CreateMutex() { return NULL; } void *Sys_CreateMutex() { return NULL; }
qboolean Sys_TryLockMutex(void *mutex) { return FALSE; } qboolean Sys_TryLockMutex(void *mutex) { return FALSE; }

View file

@ -352,12 +352,18 @@ void Sys_SaveClipboard(char *text)
#ifdef MULTITHREAD #ifdef MULTITHREAD
/* Thread creation calls */ /* Thread creation calls */
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize)
{ {
// SDL threads do not support setting thread stack size // SDL threads do not support setting thread stack size
return SDL_CreateThread(func, args) != NULL; return (void *)SDL_CreateThread(func, args);
} }
void Sys_WaitOnThread(void *thread)
{
SDL_WaitThread((SDL_Thread *)thread, NULL);
}
/* Mutex calls */ /* Mutex calls */
// SDL mutexes don't have try-locks for mutexes in the spec so we stick with 1-value semaphores // SDL mutexes don't have try-locks for mutexes in the spec so we stick with 1-value semaphores
void *Sys_CreateMutex() void *Sys_CreateMutex()

View file

@ -33,7 +33,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define DDActive 0 #define DDActive 0
#endif #endif
#ifdef MULTITHREAD
#include <process.h>
#endif
@ -1369,23 +1371,41 @@ DWORD WINAPI threadwrapper(void *args)
free(args); free(args);
tw.func(tw.args); tw.func(tw.args);
#ifndef WIN32CRTDLL
_endthreadex(0);
#endif
return 0; return 0;
} }
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize)
{ {
threadwrap_t *tw = (threadwrap_t *)malloc(sizeof(threadwrap_t)); threadwrap_t *tw = (threadwrap_t *)malloc(sizeof(threadwrap_t));
HANDLE handle;
if (!tw)
return NULL;
stacksize += 128; // wrapper overhead, also prevent default stack size stacksize += 128; // wrapper overhead, also prevent default stack size
tw->func = func; tw->func = func;
tw->args = args; tw->args = args;
if (!CreateThread(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL)) #ifdef WIN32CRTDLL
handle = (HANDLE)CreateThread(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL);
#else
handle = (HANDLE)_beginthreadex(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL);
#endif
if (!handle)
{ {
free(tw); free(tw);
return FALSE; return NULL;
} }
return TRUE; return (void *)handle;
}
void Sys_WaitOnThread(void *thread)
{
WaitForSingleObject((HANDLE)thread, INFINITE);
CloseHandle((HANDLE)thread);
} }
/* Mutex calls */ /* Mutex calls */

View file

@ -866,17 +866,34 @@ void Sys_ServerActivity(void)
/* Thread creation calls */ /* Thread creation calls */
typedef void *(*pfunction_t)(void *); typedef void *(*pfunction_t)(void *);
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize)
{ {
pthread_t thread; pthread_t *thread;
pthread_attr_t attr; pthread_attr_t attr;
thread = (pthread_t *)malloc(sizeof(pthread_t));
if (!thread)
return NULL;
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (stacksize < PTHREAD_STACK_MIN) if (stacksize < PTHREAD_STACK_MIN)
stacksize = PTHREAD_STACK_MIN; stacksize = PTHREAD_STACK_MIN;
pthread_attr_setstacksize(&attr, stacksize); pthread_attr_setstacksize(&attr, stacksize);
if (pthread_create(thread, &attr, (pfunction_t)func, args))
{
free(thread);
thread = NULL;
}
pthread_attr_destroy(&attr);
return !pthread_create(&thread, &attr, (pfunction_t)func, args); return (void *)thread;
}
void Sys_WaitOnThread(void *thread)
{
pthread_join((pthread_t *)thread, NULL);
free(thread);
} }
/* Mutex calls */ /* Mutex calls */

View file

@ -26,6 +26,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <winsock.h> #include <winsock.h>
#include <conio.h> #include <conio.h>
#ifdef MULTITHREAD
#include <process.h>
#endif
#ifndef MINIMAL #ifndef MINIMAL
//#define USESERVICE //#define USESERVICE
#endif #endif
@ -1230,25 +1234,43 @@ DWORD WINAPI threadwrapper(void *args)
tw.args = ((threadwrap_t *)args)->args; tw.args = ((threadwrap_t *)args)->args;
free(args); free(args);
tw->func(tw->args); tw.func(tw.args);
#ifndef WIN32CRTDLL
_endthreadex(0);
#endif
return 0; return 0;
} }
qboolean Sys_CreateThread(int (*func)(void *), void *args, int stacksize) void *Sys_CreateThread(int (*func)(void *), void *args, int stacksize)
{ {
threadwrap_t *tw = (threadwrap_t *)malloc(sizeof(threadwrap_t)); threadwrap_t *tw = (threadwrap_t *)malloc(sizeof(threadwrap_t));
HANDLE handle;
if (!tw)
return NULL;
stacksize += 128; // wrapper overhead, also prevent default stack size stacksize += 128; // wrapper overhead, also prevent default stack size
tw->func = func; tw->func = func;
tw->args = args; tw->args = args;
if (!CreateThread(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL)) #ifdef WIN32CRTDLL
handle = (HANDLE)CreateThread(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL);
#else
handle = (HANDLE)_beginthreadex(NULL, stacksize, &threadwrapper, (void *)tw, 0, NULL);
#endif
if (!handle)
{ {
free(tw); free(tw);
return FALSE; return NULL;
} }
return TRUE; return (void *)handle;
}
void Sys_WaitOnThread(void *thread)
{
WaitForSingleObject((HANDLE)thread, INFINITE);
CloseHandle((HANDLE)thread);
} }
/* Mutex calls */ /* Mutex calls */