Fix platform dependent thread function definitions

Thread return types are different between platforms, and its
probably not a good idea to return something of a different size,
cast the callback and expect it to not crash.
This commit is contained in:
dhewg 2011-12-01 18:01:13 +01:00
parent 962c0e6032
commit f9dd2f93eb
7 changed files with 30 additions and 19 deletions

View file

@ -401,7 +401,7 @@ public:
static void TouchFileList_f( const idCmdArgs &args );
private:
friend dword BackgroundDownloadThread( void *parms );
friend THREAD_RETURN_TYPE BackgroundDownloadThread( void *parms );
searchpath_t * searchPaths;
int readCount; // total bytes read
@ -3623,7 +3623,7 @@ BackgroundDownload
Reads part of a file from a background thread.
===================
*/
dword BackgroundDownloadThread( void *parms ) {
THREAD_RETURN_TYPE BackgroundDownloadThread( void *parms ) {
while( 1 ) {
Sys_EnterCriticalSection();
backgroundDownload_t *bgl = fileSystemLocal.backgroundDownloads;
@ -3736,7 +3736,7 @@ dword BackgroundDownloadThread( void *parms ) {
#endif
}
}
return 0;
return (THREAD_RETURN_TYPE) 0;
}
/*
@ -3746,7 +3746,7 @@ idFileSystemLocal::StartBackgroundReadThread
*/
void idFileSystemLocal::StartBackgroundDownloadThread() {
if ( !backgroundThread.threadHandle ) {
Sys_CreateThread( (xthread_t)BackgroundDownloadThread, NULL, THREAD_NORMAL, backgroundThread, "backgroundDownload", g_threads, &g_thread_count );
Sys_CreateThread( BackgroundDownloadThread, NULL, THREAD_NORMAL, backgroundThread, "backgroundDownload", g_threads, &g_thread_count );
if ( !backgroundThread.threadHandle ) {
common->Warning( "idFileSystemLocal::StartBackgroundDownloadThread: failed" );
}

View file

@ -58,7 +58,7 @@ void Sys_InitScanTable( void ) {
Sys_AsyncThread
=================
*/
void Sys_AsyncThread( void ) {
THREAD_RETURN_TYPE Sys_AsyncThread( void * ) {
int now;
int next;
int want_sleep;
@ -109,6 +109,8 @@ void Sys_AsyncThread( void ) {
// thread exit
pthread_testcancel();
}
return (THREAD_RETURN_TYPE) 0;
}
/*

View file

@ -983,13 +983,15 @@ static OSErr DoRegCodeDialog( char* ioRegCode1 )
Sys_AsyncThread
=================
*/
void Sys_AsyncThread( void ) {
THREAD_RETURN_TYPE Sys_AsyncThread( void * ) {
while ( 1 ) {
usleep( 16666 );
common->Async();
Sys_TriggerEvent( TRIGGER_EVENT_ONE );
pthread_testcancel();
}
return (THREAD_RETURN_TYPE) 0;
}

View file

@ -60,6 +60,6 @@ void Posix_Shutdown( void );
void Sys_FPE_handler( int signum, siginfo_t *info, void *context );
void Sys_DoStartProcess( const char *exeName, bool dofork = true ); // if not forking, current process gets replaced
void Sys_AsyncThread( void );
THREAD_RETURN_TYPE Sys_AsyncThread( void * );
#endif

View file

@ -168,7 +168,7 @@ void Sys_CreateThread( xthread_t function, void *parms, xthreadPriority priority
if ( pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ) != 0 ) {
common->Error( "ERROR: pthread_attr_setdetachstate %s failed\n", name );
}
if ( pthread_create( ( pthread_t* )&info.threadHandle, &attr, ( pthread_function_t )function, parms ) != 0 ) {
if ( pthread_create( ( pthread_t* )&info.threadHandle, &attr, function, parms ) != 0 ) {
common->Error( "ERROR: pthread_create %s failed\n", name );
}
pthread_attr_destroy( &attr );
@ -206,8 +206,7 @@ void Sys_DestroyThread( xthreadInfo& info ) {
}
g_threads[ j-1 ] = NULL;
g_thread_count--;
Sys_LeaveCriticalSection( );
return;
break;
}
}
Sys_LeaveCriticalSection( );
@ -253,7 +252,7 @@ Posix_StartAsyncThread
*/
void Posix_StartAsyncThread() {
if ( asyncThread.threadHandle == 0 ) {
Sys_CreateThread( (xthread_t)Sys_AsyncThread, NULL, THREAD_NORMAL, asyncThread, "Async", g_threads, &g_thread_count );
Sys_CreateThread( Sys_AsyncThread, NULL, THREAD_NORMAL, asyncThread, "Async", g_threads, &g_thread_count );
} else {
common->Printf( "Async thread already running\n" );
}

View file

@ -59,6 +59,8 @@ If you have questions concerning this license or the applicable additional terms
#define assertmem( x, y ) assert( _CrtIsValidPointer( x, y, true ) )
#define THREAD_RETURN_TYPE dword
#endif
// Mac OSX
@ -97,6 +99,8 @@ If you have questions concerning this license or the applicable additional terms
#define assertmem( x, y )
#define THREAD_RETURN_TYPE void *
#endif
@ -136,6 +140,8 @@ If you have questions concerning this license or the applicable additional terms
#define assertmem( x, y )
#define THREAD_RETURN_TYPE void *
#endif
#ifdef __GNUC__
@ -482,7 +488,7 @@ void Sys_ShutdownNetworking( void );
==============================================================
*/
typedef unsigned int (*xthread_t)( void * );
typedef THREAD_RETURN_TYPE (*xthread_t)( void * );
typedef enum {
THREAD_NORMAL,
@ -492,8 +498,8 @@ typedef enum {
typedef struct {
const char * name;
int threadHandle;
unsigned long threadId;
intptr_t threadHandle;
size_t threadId;
} xthreadInfo;
const int MAX_THREADS = 10;

View file

@ -93,11 +93,11 @@ Sys_Createthread
void Sys_CreateThread( xthread_t function, void *parms, xthreadPriority priority, xthreadInfo &info, const char *name, xthreadInfo *threads[MAX_THREADS], int *thread_count ) {
HANDLE temp = CreateThread( NULL, // LPSECURITY_ATTRIBUTES lpsa,
0, // DWORD cbStack,
(LPTHREAD_START_ROUTINE)function, // LPTHREAD_START_ROUTINE lpStartAddr,
function, // LPTHREAD_START_ROUTINE lpStartAddr,
parms, // LPVOID lpvThreadParm,
0, // DWORD fdwCreate,
&info.threadId);
info.threadHandle = (int) temp;
info.threadHandle = (intptr_t) temp;
if (priority == THREAD_HIGHEST) {
SetThreadPriority( (HANDLE)info.threadHandle, THREAD_PRIORITY_HIGHEST ); // we better sleep enough to do this
} else if (priority == THREAD_ABOVE_NORMAL ) {
@ -137,7 +137,7 @@ Sys_GetThreadName
==================
*/
const char* Sys_GetThreadName(int *index) {
int id = GetCurrentThreadId();
size_t id = GetCurrentThreadId();
for( int i = 0; i < g_thread_count; i++ ) {
if ( id == g_threads[i]->threadId ) {
if ( index ) {
@ -870,7 +870,7 @@ void Sys_In_Restart_f( const idCmdArgs &args ) {
Sys_AsyncThread
==================
*/
static void Sys_AsyncThread( void *parm ) {
static THREAD_RETURN_TYPE Sys_AsyncThread( void *parm ) {
int wakeNumber;
int startTime;
@ -900,6 +900,8 @@ static void Sys_AsyncThread( void *parm ) {
common->Async();
}
return (THREAD_RETURN_TYPE) 0;
}
/*
@ -920,7 +922,7 @@ void Sys_StartAsyncThread( void ) {
t.HighPart = t.LowPart = 0;
SetWaitableTimer( hTimer, &t, USERCMD_MSEC, NULL, NULL, TRUE );
Sys_CreateThread( (xthread_t)Sys_AsyncThread, NULL, THREAD_ABOVE_NORMAL, threadInfo, "Async", g_threads, &g_thread_count );
Sys_CreateThread( Sys_AsyncThread, NULL, THREAD_ABOVE_NORMAL, threadInfo, "Async", g_threads, &g_thread_count );
#ifdef SET_THREAD_AFFINITY
// give the async thread an affinity for the second cpu