Update output queue to not pin the CPU (proper waiting)
This commit is contained in:
parent
2e813ae29c
commit
3309c034c4
|
@ -17,25 +17,30 @@ void * outputThreadHandler(void * outputQueue)
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
currentMessage = popOutputMessage(queue);
|
if (queue->count == 0)
|
||||||
if (currentMessage != NULL)
|
{
|
||||||
|
pthread_cond_wait(&queue->updated, &queue->waitMutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
currentMessage = popOutputMessage(queue);
|
||||||
|
if (currentMessage != NULL)
|
||||||
|
{
|
||||||
|
struct PlayerListNode * currentPlayerNode = currentMessage->recepients->head;
|
||||||
|
|
||||||
|
while (currentPlayerNode != NULL)
|
||||||
{
|
{
|
||||||
struct PlayerListNode * currentPlayerNode = currentMessage->recepients->head;
|
gnutls_record_send(*currentPlayerNode->player->connection->tlsSession,
|
||||||
|
currentMessage->message, sizeof(struct ServerToClientMessage));
|
||||||
while (currentPlayerNode != NULL)
|
currentPlayerNode = currentPlayerNode->next;
|
||||||
{
|
|
||||||
gnutls_record_send(*currentPlayerNode->player->connection->tlsSession,
|
|
||||||
currentMessage->message, sizeof(struct ServerToClientMessage));
|
|
||||||
currentPlayerNode = currentPlayerNode->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (currentMessage->deallocatePlayerList == true)
|
|
||||||
{
|
|
||||||
deallocatePlayerList(¤tMessage->recepients);
|
|
||||||
}
|
|
||||||
|
|
||||||
deallocateOutputMessage(¤tMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (currentMessage->deallocatePlayerList == true)
|
||||||
|
{
|
||||||
|
deallocatePlayerList(¤tMessage->recepients);
|
||||||
|
}
|
||||||
|
|
||||||
|
deallocateOutputMessage(¤tMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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