medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 7e30adcbc9f27d53dc5bb4f05c2944aaa36356c5
parent 6c14d970d9a78d434be65479582364045c977598
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Sep  2 15:51:25 -0700

Add enqueue_spin for nonblocking queues

Diffstat:
mixer.cc | 6+++---
queue.h | 16++++++++++++++++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/mixer.cc b/mixer.cc @@ -219,18 +219,18 @@ void mixer_volume( PlayingSoundID sound_id, float volume ) { cmd.type = MIXER_VOLUME; cmd.sound_id = sound_id; cmd.volume = clamp01( volume ); - while( !command_queue.enqueue( cmd ) ); + command_queue.enqueue_spin( cmd ); } void mixer_stop( PlayingSoundID sound_id ) { MixerCommand cmd; cmd.type = MIXER_STOP; cmd.sound_id = sound_id; - while( !command_queue.enqueue( cmd ) ); + command_queue.enqueue_spin( cmd ); } void mixer_stop_all() { MixerCommand cmd; cmd.type = MIXER_STOP_ALL; - while( !command_queue.enqueue( cmd ) ); + command_queue.enqueue_spin( cmd ); } diff --git a/queue.h b/queue.h @@ -1,6 +1,8 @@ #ifndef _QUEUE_H_ #define _QUEUE_H_ +#include "platform_thread.h" + template< typename T > struct BlockingQueue { virtual void enqueue( const T & x ) = 0; @@ -15,12 +17,26 @@ struct BlockingQueue { }; +const int ATTEMPTS_BEFORE_YIELD = 128; + template< typename T > struct NonblockingQueue { virtual bool enqueue( const T & x ) = 0; virtual T * dequeue_acquire() = 0; virtual void dequeue_release() = 0; + void enqueue_spin( const T & x ) { + int attempt = 0; + while( !enqueue( x ) ) { + if( attempt < ATTEMPTS_BEFORE_YIELD ) { + attempt++; + } + else { + thread_yield(); + } + } + } + bool dequeue( T * x ) { T * t = dequeue_acquire(); if( t == NULL ) return false;