Skip to content

Commit

Permalink
Merge pull request #288 from alanzhao1/feature/nanosleep
Browse files Browse the repository at this point in the history
Use nanosleep(2) instead of usleep(3)
  • Loading branch information
ec- authored Jun 7, 2024
2 parents f8e7b42 + a740cfc commit 769b69a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion code/qcommon/net_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,10 @@ qboolean NET_Sleep( int timeout )
Sleep( timeout / 1000 );
return qtrue;
#else
usleep( timeout );
struct timespec req;
req.tv_sec = timeout / 1000000;
req.tv_nsec = ( timeout % 1000000 ) * 1000;
nanosleep( &req, NULL );
return qtrue;
#endif
}
Expand Down
5 changes: 4 additions & 1 deletion code/unix/linux_qgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ void QGL_Shutdown( qboolean unloadDLL )
// huh?), and it defaults to 0. For me, 500 seems to work.
//if( r_GLlibCoolDownMsec->integer )
// usleep( r_GLlibCoolDownMsec->integer * 1000 );
usleep( 250 * 1000 );
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = 250 * 1000000;
nanosleep( &req, NULL );

dlclose( glw_state.OpenGLLib );

Expand Down
6 changes: 5 additions & 1 deletion code/unix/linux_snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,14 @@ static int xrun_recovery( snd_pcm_t *handle, int err )
else if ( err == -ESTRPIPE )
{
int tries = 0;
struct timespec req;
req.tv_sec = period_time / 1000000;
req.tv_nsec = ( period_time % 1000000 ) * 1000;

/* wait until the suspend flag is released */
while ( ( err = _snd_pcm_resume( handle ) ) == -EAGAIN )
{
usleep( period_time );
nanosleep( &req, NULL );
if ( tries++ < 16 )
{
break;
Expand Down
5 changes: 4 additions & 1 deletion code/unix/unix_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,10 @@ void Sys_Sleep( int msec ) {
return;
}
#if 1
usleep( msec * 1000 );
struct timespec req;
req.tv_sec = msec / 1000;
req.tv_nsec = ( msec % 1000 ) * 1000000;
nanosleep( &req, NULL );
#else
if ( com_dedicated->integer && stdin_active ) {
FD_ZERO( &fdset );
Expand Down

0 comments on commit 769b69a

Please sign in to comment.