#include "quakedef.h"
#include "csprogs.h"
+#include "thread.h"
/*
===============================================================================
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)
/*
============
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();
}
*/
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();
}
qboolean quotes;
char *comment;
- SV_LockThreadMutex();
- Cbuf_LockThreadMutex();
-
// LordHavoc: making sure the tokenizebuffer doesn't get filled up by repeated crashes
cmd_tokenizebufferpos = 0;
break;
}
}
-
- SV_UnlockThreadMutex();
- Cbuf_UnlockThreadMutex();
}
/*
cmd_function_t *cmd;
cmdalias_t *a;
- if (lockmutex)
- Cbuf_LockThreadMutex();
oldpos = cmd_tokenizebufferpos;
cmd_source = src;
found = false;
done:
cmd_tokenizebufferpos = oldpos;
- if (lockmutex)
- Cbuf_UnlockThreadMutex();
}
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)
break;
}
}
- if (con_mutex) Thread_UnlockMutex(con_mutex);
}
/*! The translation table between the graphical font and plain ASCII --KB */
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;
}
// 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);
// be quiet while shutting down
S_StopAllSounds();
+ // end the server thread
+ if (svs.threaded)
+ SV_StopThread();
+
// disconnect client from server if active
CL_Disconnect();
Host_ShutdownServer ();
SV_UnlockThreadMutex();
- // end the server thread
- if (svs.threaded)
- SV_StopThread();
-
// Shutdown menu
if(MR_Shutdown)
MR_Shutdown();
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);
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);
-}
/// (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);
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;
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;
}
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;
}
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;
}
{
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;
{
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);
{
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);
}
{
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);
}
{
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;
}
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);
}
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);
}
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);
}
{
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;
}
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);
}
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;
}
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;
}
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);
}