- Use select() to sleep when idle as opposed to busy waiting.
- Introduce com_busyWait cvar to go back to old behaviour
This commit is contained in:
parent
fa8201c9b6
commit
e5dbce839a
7 changed files with 206 additions and 183 deletions
|
@ -59,7 +59,6 @@ cvar_t *com_developer;
|
|||
cvar_t *com_dedicated;
|
||||
cvar_t *com_timescale;
|
||||
cvar_t *com_fixedtime;
|
||||
cvar_t *com_dropsim; // 0.0 to 1.0, simulated packet drops
|
||||
cvar_t *com_journal;
|
||||
cvar_t *com_maxfps;
|
||||
cvar_t *com_altivec;
|
||||
|
@ -84,6 +83,7 @@ cvar_t *com_minimized;
|
|||
cvar_t *com_maxfpsMinimized;
|
||||
cvar_t *com_abnormalExit;
|
||||
cvar_t *com_standalone;
|
||||
cvar_t *com_busyWait;
|
||||
|
||||
// com_speeds times
|
||||
int time_game;
|
||||
|
@ -91,7 +91,6 @@ int time_frontend; // renderer frontend time
|
|||
int time_backend; // renderer backend time
|
||||
|
||||
int com_frameTime;
|
||||
int com_frameMsec;
|
||||
int com_frameNumber;
|
||||
|
||||
qboolean com_errorEntered = qfalse;
|
||||
|
@ -1945,7 +1944,6 @@ EVENT LOOP
|
|||
static sysEvent_t eventQueue[ MAX_QUEUED_EVENTS ];
|
||||
static int eventHead = 0;
|
||||
static int eventTail = 0;
|
||||
static byte sys_packetReceived[ MAX_MSGLEN ];
|
||||
|
||||
/*
|
||||
================
|
||||
|
@ -1998,8 +1996,6 @@ sysEvent_t Com_GetSystemEvent( void )
|
|||
{
|
||||
sysEvent_t ev;
|
||||
char *s;
|
||||
msg_t netmsg;
|
||||
netadr_t adr;
|
||||
|
||||
// return if we have data
|
||||
if ( eventHead > eventTail )
|
||||
|
@ -2021,21 +2017,6 @@ sysEvent_t Com_GetSystemEvent( void )
|
|||
Com_QueueEvent( 0, SE_CONSOLE, 0, 0, len, b );
|
||||
}
|
||||
|
||||
// check for network packets
|
||||
MSG_Init( &netmsg, sys_packetReceived, sizeof( sys_packetReceived ) );
|
||||
if ( Sys_GetPacket ( &adr, &netmsg ) )
|
||||
{
|
||||
netadr_t *buf;
|
||||
int len;
|
||||
|
||||
// copy out to a seperate buffer for qeueing
|
||||
len = sizeof( netadr_t ) + netmsg.cursize;
|
||||
buf = Z_Malloc( len );
|
||||
*buf = adr;
|
||||
memcpy( buf+1, netmsg.data, netmsg.cursize );
|
||||
Com_QueueEvent( 0, SE_PACKET, 0, 0, len, buf );
|
||||
}
|
||||
|
||||
// return if we have data
|
||||
if ( eventHead > eventTail )
|
||||
{
|
||||
|
@ -2195,7 +2176,6 @@ int Com_EventLoop( void ) {
|
|||
MSG_Init( &buf, bufData, sizeof( bufData ) );
|
||||
|
||||
while ( 1 ) {
|
||||
NET_FlushPacketQueue();
|
||||
ev = Com_GetEvent();
|
||||
|
||||
// if no more events are available
|
||||
|
@ -2216,57 +2196,26 @@ int Com_EventLoop( void ) {
|
|||
}
|
||||
|
||||
|
||||
switch ( ev.evType ) {
|
||||
default:
|
||||
Com_Error( ERR_FATAL, "Com_EventLoop: bad event type %i", ev.evType );
|
||||
switch(ev.evType)
|
||||
{
|
||||
case SE_KEY:
|
||||
CL_KeyEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
break;
|
||||
case SE_NONE:
|
||||
break;
|
||||
case SE_KEY:
|
||||
CL_KeyEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
case SE_CHAR:
|
||||
CL_CharEvent( ev.evValue );
|
||||
break;
|
||||
case SE_CHAR:
|
||||
CL_CharEvent( ev.evValue );
|
||||
case SE_MOUSE:
|
||||
CL_MouseEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
break;
|
||||
case SE_MOUSE:
|
||||
CL_MouseEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
case SE_JOYSTICK_AXIS:
|
||||
CL_JoystickEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
break;
|
||||
case SE_JOYSTICK_AXIS:
|
||||
CL_JoystickEvent( ev.evValue, ev.evValue2, ev.evTime );
|
||||
case SE_CONSOLE:
|
||||
Cbuf_AddText( (char *)ev.evPtr );
|
||||
Cbuf_AddText( "\n" );
|
||||
break;
|
||||
case SE_CONSOLE:
|
||||
Cbuf_AddText( (char *)ev.evPtr );
|
||||
Cbuf_AddText( "\n" );
|
||||
break;
|
||||
case SE_PACKET:
|
||||
// this cvar allows simulation of connections that
|
||||
// drop a lot of packets. Note that loopback connections
|
||||
// don't go through here at all.
|
||||
if ( com_dropsim->value > 0 ) {
|
||||
static int seed;
|
||||
|
||||
if ( Q_random( &seed ) < com_dropsim->value ) {
|
||||
break; // drop this packet
|
||||
}
|
||||
}
|
||||
|
||||
evFrom = *(netadr_t *)ev.evPtr;
|
||||
buf.cursize = ev.evPtrLength - sizeof( evFrom );
|
||||
|
||||
// we must copy the contents of the message out, because
|
||||
// the event buffers are only large enough to hold the
|
||||
// exact payload, but channel messages need to be large
|
||||
// enough to hold fragment reassembly
|
||||
if ( (unsigned)buf.cursize > buf.maxsize ) {
|
||||
Com_Printf("Com_EventLoop: oversize packet\n");
|
||||
continue;
|
||||
}
|
||||
Com_Memcpy( buf.data, (byte *)((netadr_t *)ev.evPtr + 1), buf.cursize );
|
||||
if ( com_sv_running->integer ) {
|
||||
Com_RunAndTimeServerPacket( &evFrom, &buf );
|
||||
} else {
|
||||
CL_PacketEvent( evFrom, &buf );
|
||||
}
|
||||
default:
|
||||
Com_Error( ERR_FATAL, "Com_EventLoop: bad event type %i", ev.evType );
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -2639,7 +2588,6 @@ void Com_Init( char *commandLine ) {
|
|||
|
||||
// Clear queues
|
||||
Com_Memset( &eventQueue[ 0 ], 0, MAX_QUEUED_EVENTS * sizeof( sysEvent_t ) );
|
||||
Com_Memset( &sys_packetReceived[ 0 ], 0, MAX_MSGLEN * sizeof( byte ) );
|
||||
|
||||
// initialize the weak pseudo-random number generator for use later.
|
||||
Com_InitRand();
|
||||
|
@ -2720,7 +2668,6 @@ void Com_Init( char *commandLine ) {
|
|||
com_timescale = Cvar_Get ("timescale", "1", CVAR_CHEAT | CVAR_SYSTEMINFO );
|
||||
com_fixedtime = Cvar_Get ("fixedtime", "0", CVAR_CHEAT);
|
||||
com_showtrace = Cvar_Get ("com_showtrace", "0", CVAR_CHEAT);
|
||||
com_dropsim = Cvar_Get ("com_dropsim", "0", CVAR_CHEAT);
|
||||
com_speeds = Cvar_Get ("com_speeds", "0", 0);
|
||||
com_timedemo = Cvar_Get ("timedemo", "0", CVAR_CHEAT);
|
||||
com_cameraMode = Cvar_Get ("com_cameraMode", "0", CVAR_CHEAT);
|
||||
|
@ -2740,6 +2687,7 @@ void Com_Init( char *commandLine ) {
|
|||
com_maxfpsMinimized = Cvar_Get( "com_maxfpsMinimized", "0", CVAR_ARCHIVE );
|
||||
com_abnormalExit = Cvar_Get( "com_abnormalExit", "0", CVAR_ROM );
|
||||
com_standalone = Cvar_Get( "com_standalone", "0", CVAR_INIT );
|
||||
com_busyWait = Cvar_Get("com_busyWait", "0", CVAR_ARCHIVE);
|
||||
|
||||
com_introPlayed = Cvar_Get( "com_introplayed", "0", CVAR_ARCHIVE);
|
||||
|
||||
|
@ -2947,19 +2895,16 @@ Com_Frame
|
|||
void Com_Frame( void ) {
|
||||
|
||||
int msec, minMsec;
|
||||
static int lastTime;
|
||||
int key;
|
||||
int timeVal;
|
||||
static int lastTime = 0;
|
||||
|
||||
int timeBeforeFirstEvents;
|
||||
int timeBeforeServer;
|
||||
int timeBeforeEvents;
|
||||
int timeBeforeClient;
|
||||
int timeAfter;
|
||||
int timeBeforeServer;
|
||||
int timeBeforeEvents;
|
||||
int timeBeforeClient;
|
||||
int timeAfter;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if ( setjmp (abortframe) ) {
|
||||
return; // an ERR_DROP was thrown
|
||||
}
|
||||
|
@ -2970,10 +2915,6 @@ void Com_Frame( void ) {
|
|||
timeBeforeClient = 0;
|
||||
timeAfter = 0;
|
||||
|
||||
|
||||
// old net chan encryption key
|
||||
key = 0x87243987;
|
||||
|
||||
// write config file if anything changed
|
||||
Com_WriteConfiguration();
|
||||
|
||||
|
@ -2984,37 +2925,62 @@ void Com_Frame( void ) {
|
|||
timeBeforeFirstEvents = Sys_Milliseconds ();
|
||||
}
|
||||
|
||||
// we may want to spin here if things are going too fast
|
||||
if ( !com_dedicated->integer && !com_timedemo->integer ) {
|
||||
if( com_minimized->integer && com_maxfpsMinimized->integer > 0 ) {
|
||||
minMsec = 1000 / com_maxfpsMinimized->integer;
|
||||
} else if( com_unfocused->integer && com_maxfpsUnfocused->integer > 0 ) {
|
||||
minMsec = 1000 / com_maxfpsUnfocused->integer;
|
||||
} else if( com_maxfps->integer > 0 ) {
|
||||
minMsec = 1000 / com_maxfps->integer;
|
||||
} else {
|
||||
minMsec = 1;
|
||||
// Figure out how much time we have
|
||||
if(!com_timedemo->integer)
|
||||
{
|
||||
if(com_dedicated->integer)
|
||||
minMsec = SV_FrameMsec();
|
||||
else
|
||||
{
|
||||
if(com_minimized->integer && com_maxfpsMinimized->integer > 0)
|
||||
minMsec = 1000 / com_maxfpsMinimized->integer;
|
||||
else if(com_unfocused->integer && com_maxfpsUnfocused->integer > 0)
|
||||
minMsec = 1000 / com_maxfpsUnfocused->integer;
|
||||
else if(com_maxfps->integer > 0)
|
||||
minMsec = 1000 / com_maxfps->integer;
|
||||
else
|
||||
minMsec = 1;
|
||||
|
||||
timeVal = com_frameTime - lastTime;
|
||||
if(timeVal > minMsec)
|
||||
{
|
||||
// Adjust minMsec if previous frame took too long to render so
|
||||
// that framerate is stable at the requested value.
|
||||
timeVal -= minMsec;
|
||||
|
||||
if(timeVal > minMsec)
|
||||
minMsec = 0;
|
||||
else
|
||||
minMsec -= timeVal;
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
minMsec = 1;
|
||||
}
|
||||
else
|
||||
minMsec = 1;
|
||||
|
||||
msec = minMsec;
|
||||
do {
|
||||
int timeRemaining = minMsec - msec;
|
||||
timeVal = 0;
|
||||
do
|
||||
{
|
||||
if(com_busyWait->integer)
|
||||
NET_Sleep(0);
|
||||
else
|
||||
NET_Sleep(timeVal);
|
||||
|
||||
// The existing Sys_Sleep implementations aren't really
|
||||
// precise enough to be of use beyond 100fps
|
||||
// FIXME: implement a more precise sleep (RDTSC or something)
|
||||
if( timeRemaining >= 10 )
|
||||
Sys_Sleep( timeRemaining );
|
||||
msec = Sys_Milliseconds() - com_frameTime;
|
||||
|
||||
if(msec >= minMsec)
|
||||
timeVal = 0;
|
||||
else
|
||||
timeVal = minMsec - msec;
|
||||
|
||||
} while(timeVal > 0);
|
||||
|
||||
lastTime = com_frameTime;
|
||||
com_frameTime = Com_EventLoop();
|
||||
|
||||
msec = com_frameTime - lastTime;
|
||||
|
||||
com_frameTime = Com_EventLoop();
|
||||
if ( lastTime > com_frameTime ) {
|
||||
lastTime = com_frameTime; // possible on first frame
|
||||
}
|
||||
msec = com_frameTime - lastTime;
|
||||
} while ( msec < minMsec );
|
||||
Cbuf_Execute ();
|
||||
|
||||
if (com_altivec->modified)
|
||||
|
@ -3023,11 +2989,8 @@ void Com_Frame( void ) {
|
|||
com_altivec->modified = qfalse;
|
||||
}
|
||||
|
||||
lastTime = com_frameTime;
|
||||
|
||||
// mess with msec if needed
|
||||
com_frameMsec = msec;
|
||||
msec = Com_ModifyMsec( msec );
|
||||
msec = Com_ModifyMsec(msec);
|
||||
|
||||
//
|
||||
// server side
|
||||
|
@ -3087,6 +3050,9 @@ void Com_Frame( void ) {
|
|||
}
|
||||
#endif
|
||||
|
||||
|
||||
NET_FlushPacketQueue();
|
||||
|
||||
//
|
||||
// report timing information
|
||||
//
|
||||
|
@ -3120,9 +3086,6 @@ void Com_Frame( void ) {
|
|||
c_pointcontents = 0;
|
||||
}
|
||||
|
||||
// old net chan encryption key
|
||||
key = lastTime * 0x87243987;
|
||||
|
||||
com_frameNumber++;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue