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