From: havoc Date: Tue, 25 Oct 2011 08:31:10 +0000 (+0000) Subject: added Sys_PrintfToTerminal function for convenience, this replaces X-Git-Tag: xonotic-v0.6.0~163^2~86 X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=commitdiff_plain;h=66a10ad8f7213d8969fc53f69f1ff712f3aea004 added Sys_PrintfToTerminal function for convenience, this replaces printf in a few places changed SV_LockThreadMutex and SV_UnlockThreadMutex to macros so they get the filename and line number for THREADDEBUG prints fixed multiple recursive mutex locks in the console system so glx client works again git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11471 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/cl_input.c b/cl_input.c index 6959b14f..41c93080 100644 --- a/cl_input.c +++ b/cl_input.c @@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" #include "csprogs.h" +#include "thread.h" /* =============================================================================== diff --git a/cmd.c b/cmd.c index d2d688f8..8efe3dc9 100644 --- a/cmd.c +++ b/cmd.c @@ -176,25 +176,9 @@ static void Cmd_Centerprint_f (void) static sizebuf_t cmd_text; static unsigned char cmd_text_buf[CMDBUFSIZE]; void *cmd_text_mutex = NULL; -qboolean cmd_text_mutex_locked = false; -static void Cbuf_LockThreadMutex(void) -{ - if (cmd_text_mutex) - { - Thread_LockMutex(cmd_text_mutex); - cmd_text_mutex_locked = true; - } -} - -static void Cbuf_UnlockThreadMutex(void) -{ - if (cmd_text_mutex) - { - cmd_text_mutex_locked = false; - Thread_UnlockMutex(cmd_text_mutex); - } -} +#define Cbuf_LockThreadMutex() (cmd_text_mutex ? Thread_LockMutex(cmd_text_mutex),1 : 0) +#define Cbuf_UnlockThreadMutex() (cmd_text_mutex ? Thread_UnlockMutex(cmd_text_mutex),1 : 0) /* ============ @@ -213,7 +197,7 @@ void Cbuf_AddText (const char *text) if (cmd_text.cursize + l >= cmd_text.maxsize) Con_Print("Cbuf_AddText: overflow\n"); else - SZ_Write(&cmd_text, (const unsigned char *)text, (int)strlen (text)); + SZ_Write(&cmd_text, (const unsigned char *)text, l); Cbuf_UnlockThreadMutex(); } @@ -229,32 +213,18 @@ FIXME: actually change the command buffer to do less copying */ void Cbuf_InsertText (const char *text) { - char *temp; - int templen; - + size_t l = strlen(text); Cbuf_LockThreadMutex(); - - // copy off any commands still remaining in the exec buffer - templen = cmd_text.cursize; - if (templen) - { - temp = (char *)Mem_Alloc (tempmempool, templen); - memcpy (temp, cmd_text.data, templen); - SZ_Clear (&cmd_text); - } + // we need to memmove the existing text and stuff this in before it... + if (cmd_text.cursize + l >= (size_t)cmd_text.maxsize) + Con_Print("Cbuf_InsertText: overflow\n"); else - temp = NULL; - - // add the entire text of the file - Cbuf_AddText (text); - - // add the copied off data - if (temp != NULL) { - SZ_Write (&cmd_text, (const unsigned char *)temp, templen); - Mem_Free (temp); + // we don't have a SZ_Prepend, so... + memmove(cmd_text.data + l, cmd_text.data, cmd_text.cursize); + cmd_text.cursize += l; + memcpy(cmd_text.data, text, l); } - Cbuf_UnlockThreadMutex(); } @@ -316,9 +286,6 @@ void Cbuf_Execute (void) qboolean quotes; char *comment; - SV_LockThreadMutex(); - Cbuf_LockThreadMutex(); - // LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes cmd_tokenizebufferpos = 0; @@ -409,9 +376,6 @@ void Cbuf_Execute (void) break; } } - - SV_UnlockThreadMutex(); - Cbuf_UnlockThreadMutex(); } /* @@ -1701,8 +1665,6 @@ void Cmd_ExecuteString (const char *text, cmd_source_t src, qboolean lockmutex) cmd_function_t *cmd; cmdalias_t *a; - if (lockmutex) - Cbuf_LockThreadMutex(); oldpos = cmd_tokenizebufferpos; cmd_source = src; found = false; @@ -1778,8 +1740,6 @@ command_found: done: cmd_tokenizebufferpos = oldpos; - if (lockmutex) - Cbuf_UnlockThreadMutex(); } diff --git a/console.c b/console.c index 537cdc72..769ae2f6 100644 --- a/console.c +++ b/console.c @@ -814,7 +814,6 @@ static void Con_PrintToHistory(const char *txt, int mask) if(!con.text) // FIXME uses a non-abstracted property of con return; - if (con_mutex) Thread_LockMutex(con_mutex); for(; *txt; ++txt) { if(cr_pending) @@ -845,7 +844,6 @@ static void Con_PrintToHistory(const char *txt, int mask) break; } } - if (con_mutex) Thread_UnlockMutex(con_mutex); } /*! The translation table between the graphical font and plain ASCII --KB */ @@ -1535,7 +1533,7 @@ static float Con_WordWidthFunc(void *passthrough, const char *w, size_t *length, return DrawQ_TextWidth(w, *length, ti->fontsize, ti->fontsize, false, ti->font); else { - printf("Con_WordWidthFunc: can't get here (maxWidth should never be %f)\n", maxWidth); + Sys_PrintfToTerminal("Con_WordWidthFunc: can't get here (maxWidth should never be %f)\n", maxWidth); // Note: this is NOT a Con_Printf, as it could print recursively return 0; } diff --git a/host.c b/host.c index 88eed4bd..d316fcc4 100644 --- a/host.c +++ b/host.c @@ -755,11 +755,13 @@ void Host_Main(void) // otherwise we execute them on client frames if (sv.active ? sv_timer > 0 : cl_timer > 0) { + SV_LockThreadMutex(); // process console commands // R_TimeReport("preconsole"); CL_VM_PreventInformationLeaks(); Cbuf_Execute(); // R_TimeReport("console"); + SV_UnlockThreadMutex(); } //Con_Printf("%6.0f %6.0f\n", cl_timer * 1000000.0, sv_timer * 1000000.0); @@ -1312,6 +1314,10 @@ void Host_Shutdown(void) // be quiet while shutting down S_StopAllSounds(); + // end the server thread + if (svs.threaded) + SV_StopThread(); + // disconnect client from server if active CL_Disconnect(); @@ -1320,10 +1326,6 @@ void Host_Shutdown(void) Host_ShutdownServer (); SV_UnlockThreadMutex(); - // end the server thread - if (svs.threaded) - SV_StopThread(); - // Shutdown menu if(MR_Shutdown) MR_Shutdown(); diff --git a/server.h b/server.h index 64153e56..5d399607 100644 --- a/server.h +++ b/server.h @@ -589,8 +589,8 @@ void SV_GetEntityMatrix(prvm_prog_t *prog, prvm_edict_t *ent, matrix4x4_t *out, void SV_StartThread(void); void SV_StopThread(void); -void SV_LockThreadMutex(void); -void SV_UnlockThreadMutex(void); +#define SV_LockThreadMutex() (svs.threaded ? Thread_LockMutex(svs.threadmutex),1 : 0) +#define SV_UnlockThreadMutex() (svs.threaded ? Thread_UnlockMutex(svs.threadmutex),1 : 0) void VM_CustomStats_Clear(void); void VM_SV_UpdateCustomStats(client_t *client, prvm_edict_t *ent, sizebuf_t *msg, int *stats); diff --git a/sv_main.c b/sv_main.c index 26d85695..c99273b1 100644 --- a/sv_main.c +++ b/sv_main.c @@ -4009,15 +4009,3 @@ void SV_StopThread(void) Thread_DestroyMutex(svs.threadmutex); svs.threaded = false; } - -void SV_LockThreadMutex(void) -{ - if (svs.threaded) - Thread_LockMutex(svs.threadmutex); -} - -void SV_UnlockThreadMutex(void) -{ - if (svs.threaded) - Thread_UnlockMutex(svs.threadmutex); -} diff --git a/sys.h b/sys.h index 908c9e5f..913b3cdb 100644 --- a/sys.h +++ b/sys.h @@ -72,6 +72,7 @@ void Sys_Error (const char *error, ...) DP_FUNC_PRINTF(1); /// (may) output text to terminal which launched program void Sys_PrintToTerminal(const char *text); +void Sys_PrintfToTerminal(const char *fmt, ...); /// INFO: This is only called by Host_Shutdown so we dont need testing for recursion void Sys_Shutdown (void); diff --git a/sys_shared.c b/sys_shared.c index 34fd2f7a..cb826f17 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -434,10 +434,22 @@ void Sys_Sleep(int microseconds) if(sys_debugsleep.integer) { t = Sys_DirtyTime() - t; - printf("%d %d # debugsleep\n", microseconds, (unsigned int)(t * 1000000)); + Sys_PrintfToTerminal("%d %d # debugsleep\n", microseconds, (unsigned int)(t * 1000000)); } } +void Sys_PrintfToTerminal(const char *fmt, ...) +{ + va_list argptr; + char msg[MAX_INPUTLINE]; + + va_start(argptr,fmt); + dpvsnprintf(msg,sizeof(msg),fmt,argptr); + va_end(argptr); + + Sys_PrintToTerminal(msg); +} + static const char *Sys_FindInPATH(const char *name, char namesep, const char *PATH, char pathsep, char *buf, size_t bufsize) { const char *p = PATH; diff --git a/thread_null.c b/thread_null.c index 8a75e246..58b22bd5 100644 --- a/thread_null.c +++ b/thread_null.c @@ -18,7 +18,7 @@ qboolean Thread_HasThreads(void) void *_Thread_CreateMutex(const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p create %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); #endif return NULL; } @@ -26,14 +26,14 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); #endif } int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); #endif return -1; } @@ -41,7 +41,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); #endif return -1; } diff --git a/thread_pthread.c b/thread_pthread.c index 69558da5..c9a11663 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -21,7 +21,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) Z_Malloc(sizeof(pthread_mutex_t)); #ifdef THREADDEBUG - printf("%p create %s:%i\n" , mutexp, filename, fileline); + Sys_PrintfToTerminal("%p create %s:%i\n" , mutexp, filename, fileline); #endif pthread_mutex_init(mutexp, NULL); return mutexp; @@ -31,7 +31,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - printf("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); #endif pthread_mutex_destroy(mutexp); Z_Free(mutexp); @@ -41,7 +41,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - printf("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); #endif return pthread_mutex_lock(mutexp); } @@ -50,7 +50,7 @@ int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex; #ifdef THREADDEBUG - printf("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); #endif return pthread_mutex_unlock(mutexp); } diff --git a/thread_sdl.c b/thread_sdl.c index d857445a..f817c987 100644 --- a/thread_sdl.c +++ b/thread_sdl.c @@ -21,7 +21,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) { void *mutex = SDL_CreateMutex(); #ifdef THREADDEBUG - printf("%p create %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); #endif return mutex; } @@ -29,7 +29,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); #endif SDL_DestroyMutex((SDL_mutex *)mutex); } @@ -37,7 +37,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); #endif return SDL_LockMutex((SDL_mutex *)mutex); } @@ -45,7 +45,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); #endif return SDL_UnlockMutex((SDL_mutex *)mutex); } diff --git a/thread_win.c b/thread_win.c index a882f80d..a6ae7b3c 100644 --- a/thread_win.c +++ b/thread_win.c @@ -20,7 +20,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) { void *mutex = (void *)CreateMutex(NULL, FALSE, NULL); #ifdef THREADDEBUG - printf("%p create %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline); #endif return mutex; } @@ -28,7 +28,7 @@ void *_Thread_CreateMutex(const char *filename, int fileline) void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p destroy %s:%i\n", mutex, filename, fileline); + Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline); #endif CloseHandle(mutex); } @@ -36,7 +36,7 @@ void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline) int _Thread_LockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p lock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline); #endif return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0; } @@ -44,7 +44,7 @@ int _Thread_LockMutex(void *mutex, const char *filename, int fileline) int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline) { #ifdef THREADDEBUG - printf("%p unlock %s:%i\n" , mutex, filename, fileline); + Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline); #endif return (ReleaseMutex(mutex) == FALSE) ? -1 : 0; } diff --git a/vid_agl.c b/vid_agl.c index d21ffc06..87559d36 100644 --- a/vid_agl.c +++ b/vid_agl.c @@ -312,7 +312,7 @@ int VID_GetGamma(unsigned short *ramps, int rampsize) void signal_handler(int sig) { - printf("Received signal %d, exiting...\n", sig); + Sys_PrintfToTerminal("Received signal %d, exiting...\n", sig); VID_RestoreSystemGamma(); Sys_Quit(1); }