mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 09:36:46 +00:00
Implement switch_queue_pop_timeout() and refactor sofia_profile_worker_thread_run() to use it so it doesn't wake up too often
This commit is contained in:
@@ -313,6 +313,71 @@ APU_DECLARE(apr_status_t) apr_queue_pop(apr_queue_t *queue, void **data)
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the next item from the queue. If there are no
|
||||
* items available, it will block until one becomes available, or
|
||||
* until timeout is elapsed. Once retrieved, the item is placed into
|
||||
* the address specified by'data'.
|
||||
*/
|
||||
APU_DECLARE(apr_status_t) apr_queue_pop_timeout(apr_queue_t *queue, void **data, apr_interval_time_t timeout)
|
||||
{
|
||||
apr_status_t rv;
|
||||
|
||||
if (queue->terminated) {
|
||||
return APR_EOF; /* no more elements ever again */
|
||||
}
|
||||
|
||||
rv = apr_thread_mutex_lock(queue->one_big_mutex);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* Keep waiting until we wake up and find that the queue is not empty. */
|
||||
if (apr_queue_empty(queue)) {
|
||||
if (!queue->terminated) {
|
||||
queue->empty_waiters++;
|
||||
rv = apr_thread_cond_timedwait(queue->not_empty, queue->one_big_mutex, timeout);
|
||||
queue->empty_waiters--;
|
||||
/* In the event of a timemout, APR_TIMEUP will be returned */
|
||||
if (rv != APR_SUCCESS) {
|
||||
apr_thread_mutex_unlock(queue->one_big_mutex);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
/* If we wake up and it's still empty, then we were interrupted */
|
||||
if (apr_queue_empty(queue)) {
|
||||
Q_DBG("queue empty (intr)", queue);
|
||||
rv = apr_thread_mutex_unlock(queue->one_big_mutex);
|
||||
if (rv != APR_SUCCESS) {
|
||||
return rv;
|
||||
}
|
||||
if (queue->terminated) {
|
||||
return APR_EOF; /* no more elements ever again */
|
||||
}
|
||||
else {
|
||||
return APR_EINTR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*data = queue->data[queue->out];
|
||||
queue->nelts--;
|
||||
|
||||
queue->out = (queue->out + 1) % queue->bounds;
|
||||
if (queue->full_waiters) {
|
||||
Q_DBG("signal !full", queue);
|
||||
rv = apr_thread_cond_signal(queue->not_full);
|
||||
if (rv != APR_SUCCESS) {
|
||||
apr_thread_mutex_unlock(queue->one_big_mutex);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
rv = apr_thread_mutex_unlock(queue->one_big_mutex);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the next item from the queue. If there are no
|
||||
* items available, return APR_EAGAIN. Once retrieved,
|
||||
|
Reference in New Issue
Block a user