From f9dd2f93eb065e4fd260be7d26394f7ab15d3a12 Mon Sep 17 00:00:00 2001 From: dhewg Date: Thu, 1 Dec 2011 18:01:13 +0100 Subject: [PATCH] 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. --- neo/framework/FileSystem.cpp | 8 ++++---- neo/sys/linux/main.cpp | 4 +++- neo/sys/osx/DOOMController.mm | 4 +++- neo/sys/posix/posix_public.h | 2 +- neo/sys/posix/posix_threads.cpp | 7 +++---- neo/sys/sys_public.h | 12 +++++++++--- neo/sys/win32/win_main.cpp | 12 +++++++----- 7 files changed, 30 insertions(+), 19 deletions(-) diff --git a/neo/framework/FileSystem.cpp b/neo/framework/FileSystem.cpp index ac05201c..fdd12700 100644 --- a/neo/framework/FileSystem.cpp +++ b/neo/framework/FileSystem.cpp @@ -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" ); } diff --git a/neo/sys/linux/main.cpp b/neo/sys/linux/main.cpp index 541379bc..1b05a5ed 100644 --- a/neo/sys/linux/main.cpp +++ b/neo/sys/linux/main.cpp @@ -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; } /* diff --git a/neo/sys/osx/DOOMController.mm b/neo/sys/osx/DOOMController.mm index 30c71341..22c21ce1 100644 --- a/neo/sys/osx/DOOMController.mm +++ b/neo/sys/osx/DOOMController.mm @@ -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; } diff --git a/neo/sys/posix/posix_public.h b/neo/sys/posix/posix_public.h index 97193d0a..01a4c7c1 100644 --- a/neo/sys/posix/posix_public.h +++ b/neo/sys/posix/posix_public.h @@ -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 diff --git a/neo/sys/posix/posix_threads.cpp b/neo/sys/posix/posix_threads.cpp index 75ed045a..cbc78379 100644 --- a/neo/sys/posix/posix_threads.cpp +++ b/neo/sys/posix/posix_threads.cpp @@ -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" ); } diff --git a/neo/sys/sys_public.h b/neo/sys/sys_public.h index ba5d90fb..93a543d9 100644 --- a/neo/sys/sys_public.h +++ b/neo/sys/sys_public.h @@ -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; diff --git a/neo/sys/win32/win_main.cpp b/neo/sys/win32/win_main.cpp index 773f5be4..b703c035 100644 --- a/neo/sys/win32/win_main.cpp +++ b/neo/sys/win32/win_main.cpp @@ -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