medfall

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

commit 8599e2bb07d07dfb2f6eb5f66cc6c7551d025e71
parent d253905b46a95beb524fd2394ba30d3d45b16692
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Aug 20 20:57:24 +0100

Add queue.h

Diffstat:
queue.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/queue.h b/queue.h @@ -0,0 +1,63 @@ +#ifndef _QUEUE_H_ +#define _QUEUE_H_ + +template< typename T > +struct BlockingQueue { + virtual void enqueue( const T & x ) = 0; + virtual T * dequeue_acquire() = 0; + virtual void dequeue_release() = 0; + + T dequeue() { + T t = *dequeue_acquire(); + dequeue_release(); + return t; + } + +}; + +template< typename T > +struct NonblockingQueue { + virtual bool enqueue( const T & x ) = 0; + virtual T * dequeue_acquire() = 0; + virtual void dequeue_release() = 0; + + bool dequeue( T * x ) { + T * t = dequeue_acquire(); + if( t == NULL ) return false; + *x = *t; + dequeue_release(); + return true; + } +}; + +template< typename T > +class NonblockingQueueReader { +public: + NonblockingQueueReader( NonblockingQueue< T > * Q ) { + q = Q; + } + + bool dequeue( T * x ) { + return q.dequeue( x ); + } + +private: + NonblockingQueue< T > * q; +}; + +template< typename T > +struct NonblockingQueueWriter { +public: + NonblockingQueueWriter( NonblockingQueue< T > * Q ) { + q = Q; + } + + bool enqueue( const T & x ) { + return q.dequeue( x ); + } + +private: + NonblockingQueue< T > * q; +}; + +#endif // _QUEUE_H_