Commit d7d93ce8 authored by Thomas Guillem's avatar Thomas Guillem

background_worker: simplify, merge locks

These 2 locks were only held for very fast operations. No need to complicate
with 2 locks.
parent b9cc9c42
...@@ -37,9 +37,9 @@ struct background_worker { ...@@ -37,9 +37,9 @@ struct background_worker {
void* owner; void* owner;
struct background_worker_config conf; struct background_worker_config conf;
vlc_mutex_t lock; /**< acquire to inspect members that follow */
struct { struct {
bool probe_request; /**< true if a probe is requested */ bool probe_request; /**< true if a probe is requested */
vlc_mutex_t lock; /**< acquire to inspect members that follow */
vlc_cond_t wait; /**< wait for update in terms of head */ vlc_cond_t wait; /**< wait for update in terms of head */
mtime_t deadline; /**< deadline of the current task */ mtime_t deadline; /**< deadline of the current task */
void* id; /**< id of the current task */ void* id; /**< id of the current task */
...@@ -47,7 +47,6 @@ struct background_worker { ...@@ -47,7 +47,6 @@ struct background_worker {
} head; } head;
struct { struct {
vlc_mutex_t lock; /**< acquire to inspect members that follow */
vlc_array_t data; /**< queue of pending entities to process */ vlc_array_t data; /**< queue of pending entities to process */
} tail; } tail;
}; };
...@@ -61,7 +60,7 @@ static void* Thread( void* data ) ...@@ -61,7 +60,7 @@ static void* Thread( void* data )
struct bg_queued_item* item = NULL; struct bg_queued_item* item = NULL;
void* handle; void* handle;
vlc_mutex_lock( &worker->tail.lock ); vlc_mutex_lock( &worker->lock );
{ {
if( vlc_array_count( &worker->tail.data ) ) if( vlc_array_count( &worker->tail.data ) )
{ {
...@@ -71,19 +70,15 @@ static void* Thread( void* data ) ...@@ -71,19 +70,15 @@ static void* Thread( void* data )
vlc_array_remove( &worker->tail.data, 0 ); vlc_array_remove( &worker->tail.data, 0 );
} }
vlc_mutex_lock( &worker->head.lock ); worker->head.deadline = INT64_MAX;
{ worker->head.active = item != NULL;
worker->head.deadline = INT64_MAX; worker->head.id = item ? item->id : NULL;
worker->head.active = item != NULL;
worker->head.id = item ? item->id : NULL;
if( item && item->timeout > 0 ) if( item && item->timeout > 0 )
worker->head.deadline = mdate() + item->timeout * 1000; worker->head.deadline = mdate() + item->timeout * 1000;
}
vlc_cond_broadcast( &worker->head.wait ); vlc_cond_broadcast( &worker->head.wait );
vlc_mutex_unlock( &worker->head.lock );
} }
vlc_mutex_unlock( &worker->tail.lock ); vlc_mutex_unlock( &worker->lock );
if( item == NULL ) if( item == NULL )
break; break;
...@@ -97,12 +92,12 @@ static void* Thread( void* data ) ...@@ -97,12 +92,12 @@ static void* Thread( void* data )
for( ;; ) for( ;; )
{ {
vlc_mutex_lock( &worker->head.lock ); vlc_mutex_lock( &worker->lock );
bool const b_timeout = worker->head.deadline <= mdate(); bool const b_timeout = worker->head.deadline <= mdate();
worker->head.probe_request = false; worker->head.probe_request = false;
vlc_mutex_unlock( &worker->head.lock ); vlc_mutex_unlock( &worker->lock );
if( b_timeout || if( b_timeout ||
worker->conf.pf_probe( worker->owner, handle ) ) worker->conf.pf_probe( worker->owner, handle ) )
...@@ -113,14 +108,14 @@ static void* Thread( void* data ) ...@@ -113,14 +108,14 @@ static void* Thread( void* data )
break; break;
} }
vlc_mutex_lock( &worker->head.lock ); vlc_mutex_lock( &worker->lock );
if( worker->head.probe_request == false && if( worker->head.probe_request == false &&
worker->head.deadline > mdate() ) worker->head.deadline > mdate() )
{ {
vlc_cond_timedwait( &worker->head.wait, &worker->head.lock, vlc_cond_timedwait( &worker->head.wait, &worker->lock,
worker->head.deadline ); worker->head.deadline );
} }
vlc_mutex_unlock( &worker->head.lock ); vlc_mutex_unlock( &worker->lock );
} }
} }
...@@ -129,7 +124,7 @@ static void* Thread( void* data ) ...@@ -129,7 +124,7 @@ static void* Thread( void* data )
static void BackgroundWorkerCancel( struct background_worker* worker, void* id) static void BackgroundWorkerCancel( struct background_worker* worker, void* id)
{ {
vlc_mutex_lock( &worker->tail.lock ); vlc_mutex_lock( &worker->lock );
for( size_t i = 0; i < vlc_array_count( &worker->tail.data ); ) for( size_t i = 0; i < vlc_array_count( &worker->tail.data ); )
{ {
struct bg_queued_item* item = struct bg_queued_item* item =
...@@ -145,17 +140,15 @@ static void BackgroundWorkerCancel( struct background_worker* worker, void* id) ...@@ -145,17 +140,15 @@ static void BackgroundWorkerCancel( struct background_worker* worker, void* id)
++i; ++i;
} }
vlc_mutex_unlock( &worker->tail.lock );
vlc_mutex_lock( &worker->head.lock );
while( ( id == NULL && worker->head.active ) while( ( id == NULL && worker->head.active )
|| ( id != NULL && worker->head.id == id ) ) || ( id != NULL && worker->head.id == id ) )
{ {
worker->head.deadline = VLC_TS_0; worker->head.deadline = VLC_TS_0;
vlc_cond_broadcast( &worker->head.wait ); vlc_cond_broadcast( &worker->head.wait );
vlc_cond_wait( &worker->head.wait, &worker->head.lock ); vlc_cond_wait( &worker->head.wait, &worker->lock );
} }
vlc_mutex_unlock( &worker->head.lock ); vlc_mutex_unlock( &worker->lock );
} }
struct background_worker* background_worker_New( void* owner, struct background_worker* background_worker_New( void* owner,
...@@ -172,11 +165,10 @@ struct background_worker* background_worker_New( void* owner, ...@@ -172,11 +165,10 @@ struct background_worker* background_worker_New( void* owner,
worker->head.active = false; worker->head.active = false;
worker->head.deadline = VLC_TS_INVALID; worker->head.deadline = VLC_TS_INVALID;
vlc_mutex_init( &worker->head.lock ); vlc_mutex_init( &worker->lock );
vlc_cond_init( &worker->head.wait ); vlc_cond_init( &worker->head.wait );
vlc_array_init( &worker->tail.data ); vlc_array_init( &worker->tail.data );
vlc_mutex_init( &worker->tail.lock );
return worker; return worker;
} }
...@@ -193,11 +185,9 @@ int background_worker_Push( struct background_worker* worker, void* entity, ...@@ -193,11 +185,9 @@ int background_worker_Push( struct background_worker* worker, void* entity,
item->entity = entity; item->entity = entity;
item->timeout = timeout < 0 ? worker->conf.default_timeout : timeout; item->timeout = timeout < 0 ? worker->conf.default_timeout : timeout;
vlc_mutex_lock( &worker->tail.lock ); vlc_mutex_lock( &worker->lock );
vlc_array_append( &worker->tail.data, item ); vlc_array_append( &worker->tail.data, item );
vlc_mutex_unlock( &worker->tail.lock );
vlc_mutex_lock( &worker->head.lock );
if( worker->head.active == false ) if( worker->head.active == false )
{ {
worker->head.probe_request = false; worker->head.probe_request = false;
...@@ -209,7 +199,7 @@ int background_worker_Push( struct background_worker* worker, void* entity, ...@@ -209,7 +199,7 @@ int background_worker_Push( struct background_worker* worker, void* entity,
worker->conf.pf_hold( item->entity ); worker->conf.pf_hold( item->entity );
int ret = worker->head.active ? VLC_SUCCESS : VLC_EGENERIC; int ret = worker->head.active ? VLC_SUCCESS : VLC_EGENERIC;
vlc_mutex_unlock( &worker->head.lock ); vlc_mutex_unlock( &worker->lock );
return ret; return ret;
} }
...@@ -221,10 +211,10 @@ void background_worker_Cancel( struct background_worker* worker, void* id ) ...@@ -221,10 +211,10 @@ void background_worker_Cancel( struct background_worker* worker, void* id )
void background_worker_RequestProbe( struct background_worker* worker ) void background_worker_RequestProbe( struct background_worker* worker )
{ {
vlc_mutex_lock( &worker->head.lock ); vlc_mutex_lock( &worker->lock );
worker->head.probe_request = true; worker->head.probe_request = true;
vlc_cond_broadcast( &worker->head.wait ); vlc_cond_broadcast( &worker->head.wait );
vlc_mutex_unlock( &worker->head.lock ); vlc_mutex_unlock( &worker->lock );
} }
void background_worker_Delete( struct background_worker* worker ) void background_worker_Delete( struct background_worker* worker )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment