summaryrefslogtreecommitdiff
path: root/src/queue.h
diff options
context:
space:
mode:
authordeva <deva>2005-04-28 18:11:58 +0000
committerdeva <deva>2005-04-28 18:11:58 +0000
commita420994fe53199747e6d0ba0af473ecc4f940258 (patch)
treee00c5233f66e6dfc2964e203bc6529fea9374626 /src/queue.h
parent5c756301afdcad05d3b880544b3ea88e50986e21 (diff)
Moved mutex into queue (made it thread safe), to avoid mutex locking in player, encoder and decoder.
Diffstat (limited to 'src/queue.h')
-rw-r--r--src/queue.h69
1 files changed, 56 insertions, 13 deletions
diff --git a/src/queue.h b/src/queue.h
index 1cd0a3c..5e6d0f8 100644
--- a/src/queue.h
+++ b/src/queue.h
@@ -1,3 +1,4 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* RTVideoRec Realtime video recoder and encoder for Linux
*
@@ -30,6 +31,7 @@
#include <avformat.h>
#include <avcodec.h>
+#include "thread.h"
#include "util.h"
typedef struct __buf_t {
@@ -41,7 +43,7 @@ typedef struct __buf_t {
template<typename T>
class Queue {
- public:
+public:
Queue(int glimit = 0);
~Queue();
@@ -50,49 +52,56 @@ class Queue {
void plength();
- private:
+private:
int limit;
buf_t *head;
buf_t *tail;
int count;
pthread_mutex_t mutex;
+ T *_pop();
};
-template<typename T>
-void Queue<T>::plength()
-{
- fprintf(stderr, "[ql: %d]", count);
- fflush(stderr);
-}
-
+/**
+ * Initialize queue
+ */
template<typename T>
Queue<T>::Queue(int glimit)
{
+ pthread_mutex_init (&mutex, NULL);
limit = glimit;
count = 0;
head = NULL;
tail = NULL;
}
+/**
+ * Clean up queue.
+ */
template<typename T>
Queue<T>::~Queue()
-{
+{
if(count != 0) {
fprintf(stderr, "Queue not empty (%d)\n", count);
- while(T *t = pop()) delete t;
+ while(T *t = _pop()) delete t;
}
+ pthread_mutex_destroy(&mutex);
}
+/**
+ * Push element on queue.
+ */
template<typename T>
void Queue<T>::push(T *t)
{
+ pthread_mutex_lock(&mutex);
+
buf_t *b = (buf_t*)xmalloc(sizeof(*b));
b->data = (void*)t;
assert(b != NULL);
if(limit && count > 0) {
- T* tmp = (T*)pop();
+ T* tmp = (T*)_pop();
delete tmp;
}
@@ -100,6 +109,7 @@ void Queue<T>::push(T *t)
head = tail = b;
b->next = b->prev = NULL;
count = 1;
+ pthread_mutex_unlock(&mutex);
return;
}
@@ -109,18 +119,38 @@ void Queue<T>::push(T *t)
tail->prev = b;
tail = b;
count++;
+
+ pthread_mutex_unlock(&mutex);
}
+/**
+ * Pop element from queue.
+ * If queue is empty, NULL is returned.
+ */
template<typename T>
T *Queue<T>::pop()
{
+ pthread_mutex_lock(&mutex);
+ T *d = _pop();
+ pthread_mutex_unlock(&mutex);
+ return d;
+}
+
+/**
+ * Pop helper method
+ * If queue is empty, NULL is returned.
+ */
+template<typename T>
+T *Queue<T>::_pop()
+{
T *d;
buf_t *b;
assert(count >= 0);
- if(count == 0)
+ if(count == 0) {
return NULL;
+ }
b = head;
if(b->prev)
@@ -132,8 +162,21 @@ T *Queue<T>::pop()
d = (T*)b->data;
free(b);
+
return d;
}
+/**
+ * Print current length of queue
+ */
+template<typename T>
+void Queue<T>::plength()
+{
+ pthread_mutex_lock(&mutex);
+ fprintf(stderr, "[ql: %d]", count);
+ fflush(stderr);
+ pthread_mutex_unlock(&mutex);
+}
+
#endif