Correctly stop link_manager sender and receiver threads
This changes the destroy_link_manager() function to: * call a new function message_get_unlock() to unlock the sending thread from its blocking message_get() * calls pthread_cancel() on the receiving thread, because this unlocks any blocking read from a file descriptor The same message_get_unlock() function has been added ringbuffer_queue to keep the interfaces the same (it does nothing, because ringbuffer_queue is non-blocking).
This commit is contained in:
@@ -56,6 +56,10 @@ message_queue_t *new_message_queue(void)
|
||||
if (pthread_cond_init(ret->cond, NULL))
|
||||
goto error;
|
||||
|
||||
ret->head = NULL;
|
||||
ret->tail = NULL;
|
||||
ret->exit = 0;
|
||||
|
||||
return ret;
|
||||
|
||||
error:
|
||||
@@ -121,10 +125,15 @@ int message_get(message_queue_t *queue, void **data, int *priority)
|
||||
goto error;
|
||||
|
||||
while (queue->count == 0) {
|
||||
if (pthread_cond_wait(queue->cond, queue->mutex)) {
|
||||
int rc = pthread_cond_wait(queue->cond, queue->mutex);
|
||||
if (rc != 0) {
|
||||
pthread_mutex_unlock(queue->mutex);
|
||||
goto error;
|
||||
}
|
||||
if (queue->exit) {
|
||||
pthread_mutex_unlock(queue->mutex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
m = queue->head;
|
||||
@@ -148,6 +157,14 @@ error:
|
||||
return -1;
|
||||
}
|
||||
|
||||
void message_get_unlock(message_queue_t *queue)
|
||||
{
|
||||
pthread_mutex_lock(queue->mutex);
|
||||
queue->exit = 1;
|
||||
pthread_mutex_unlock(queue->mutex);
|
||||
pthread_cond_signal(queue->cond);
|
||||
}
|
||||
|
||||
/* when calling this function, the queue must not be used anymore (we don't lock it) */
|
||||
/* we suppose that the data pointer in messages was allocated by malloc/calloc/realloc */
|
||||
void destroy_message_queue(message_queue_t *queue)
|
||||
|
||||
Reference in New Issue
Block a user