From 876299a73bd4517e070ee3b349d41d37d1fd7957 Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 28 Apr 2005 18:39:31 +0000 Subject: Added locking to the queue and locked queue in encoder to empty it before stopping network stream. --- src/queue.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/queue.h') diff --git a/src/queue.h b/src/queue.h index 5e6d0f8..a18b783 100644 --- a/src/queue.h +++ b/src/queue.h @@ -49,10 +49,15 @@ public: void push(T *t); T *pop(); + T *peek(); + void lock(); + void unlock(); + void plength(); private: + bool locked; int limit; buf_t *head; buf_t *tail; @@ -67,6 +72,7 @@ private: template Queue::Queue(int glimit) { + locked = false; pthread_mutex_init (&mutex, NULL); limit = glimit; count = 0; @@ -87,6 +93,9 @@ Queue::~Queue() pthread_mutex_destroy(&mutex); } + + + /** * Push element on queue. */ @@ -95,6 +104,11 @@ void Queue::push(T *t) { pthread_mutex_lock(&mutex); + if(locked) { + delete t; + return; + } + buf_t *b = (buf_t*)xmalloc(sizeof(*b)); b->data = (void*)t; @@ -166,6 +180,27 @@ T *Queue::_pop() return d; } +/** + * Peek foremost element in queue + * If queue is empty, NULL is returned. + */ +template +T *Queue::peek() +{ + pthread_mutex_lock(&mutex); + T *d; + + assert(count >= 0); + + if(count == 0) { + return NULL; + } + + d = (T*)head->data; + pthread_mutex_unlock(&mutex); + return d; +} + /** * Print current length of queue */ @@ -178,5 +213,27 @@ void Queue::plength() pthread_mutex_unlock(&mutex); } +/** + * Lock the queue (all elements pushed from this point will be deleted.) + */ +template +void Queue::lock() +{ + pthread_mutex_lock(&mutex); + locked = true; + pthread_mutex_unlock(&mutex); +} + +/** + * Unlock the queue. + */ +template +void Queue::unlock() +{ + pthread_mutex_lock(&mutex); + locked = false; + pthread_mutex_unlock(&mutex); +} + #endif -- cgit v1.2.3