Update output queue to not pin the CPU (proper waiting)
This commit is contained in:
parent
751a734016
commit
4d13547ae6
|
@ -17,6 +17,11 @@ void * outputThreadHandler(void * outputQueue)
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
if (queue->count == 0)
|
||||||
|
{
|
||||||
|
pthread_cond_wait(&queue->updated, &queue->waitMutex);
|
||||||
|
}
|
||||||
|
|
||||||
currentMessage = popOutputMessage(queue);
|
currentMessage = popOutputMessage(queue);
|
||||||
if (currentMessage != NULL)
|
if (currentMessage != NULL)
|
||||||
{
|
{
|
||||||
|
@ -46,6 +51,7 @@ struct OutputQueue * const createOutputQueue()
|
||||||
|
|
||||||
// Initialize it:
|
// Initialize it:
|
||||||
pthread_mutex_init(&newQueue->mutex, NULL);
|
pthread_mutex_init(&newQueue->mutex, NULL);
|
||||||
|
pthread_cond_init(&newQueue->updated, NULL);
|
||||||
newQueue->count = 0;
|
newQueue->count = 0;
|
||||||
newQueue->front = NULL;
|
newQueue->front = NULL;
|
||||||
newQueue->back = NULL;
|
newQueue->back = NULL;
|
||||||
|
@ -101,6 +107,8 @@ size_t pushOutputMessage(struct OutputQueue * const queue,
|
||||||
// Leaving critical section - Unlock the queue:
|
// Leaving critical section - Unlock the queue:
|
||||||
pthread_mutex_unlock(&queue->mutex);
|
pthread_mutex_unlock(&queue->mutex);
|
||||||
|
|
||||||
|
pthread_cond_signal(&queue->updated);
|
||||||
|
|
||||||
return queue->count;
|
return queue->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@ struct OutputMessage
|
||||||
struct OutputQueue
|
struct OutputQueue
|
||||||
{
|
{
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
pthread_mutex_t waitMutex;
|
||||||
|
pthread_cond_t updated;
|
||||||
size_t count;
|
size_t count;
|
||||||
struct OutputMessage * front, * back;
|
struct OutputMessage * front, * back;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue