[vlc-commits] background_worker: simplify, merge locks

Thomas Guillem git at videolan.org
Mon Oct 9 09:53:02 CEST 2017


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Fri Oct  6 17:57:00 2017 +0200| [d7d93ce8adfd5d503f4485a092ba08e60312a4e2] | committer: Thomas Guillem

background_worker: simplify, merge locks

These 2 locks were only held for very fast operations. No need to complicate
with 2 locks.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d7d93ce8adfd5d503f4485a092ba08e60312a4e2
---

 src/misc/background_worker.c | 52 ++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 31 deletions(-)

diff --git a/src/misc/background_worker.c b/src/misc/background_worker.c
index be4e768abc..777b494c3e 100644
--- a/src/misc/background_worker.c
+++ b/src/misc/background_worker.c
@@ -37,9 +37,9 @@ struct background_worker {
     void* owner;
     struct background_worker_config conf;
 
+    vlc_mutex_t lock; /**< acquire to inspect members that follow */
     struct {
         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 */
         mtime_t deadline; /**< deadline of the current task */
         void* id; /**< id of the current task */
@@ -47,7 +47,6 @@ struct background_worker {
     } head;
 
     struct {
-        vlc_mutex_t lock; /**< acquire to inspect members that follow */
         vlc_array_t data; /**< queue of pending entities to process */
     } tail;
 };
@@ -61,7 +60,7 @@ static void* Thread( void* data )
         struct bg_queued_item* item = NULL;
         void* handle;
 
-        vlc_mutex_lock( &worker->tail.lock );
+        vlc_mutex_lock( &worker->lock );
         {
             if( vlc_array_count( &worker->tail.data ) )
             {
@@ -71,19 +70,15 @@ static void* Thread( void* data )
                 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.id = item ? item->id : NULL;
+            worker->head.deadline = INT64_MAX;
+            worker->head.active = item != NULL;
+            worker->head.id = item ? item->id : NULL;
 
-                if( item && item->timeout > 0 )
-                    worker->head.deadline = mdate() + item->timeout * 1000;
-            }
+            if( item && item->timeout > 0 )
+                worker->head.deadline = mdate() + item->timeout * 1000;
             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 )
             break;
@@ -97,12 +92,12 @@ static void* Thread( void* data )
 
         for( ;; )
         {
-            vlc_mutex_lock( &worker->head.lock );
+            vlc_mutex_lock( &worker->lock );
 
             bool const b_timeout = worker->head.deadline <= mdate();
             worker->head.probe_request = false;
 
-            vlc_mutex_unlock( &worker->head.lock );
+            vlc_mutex_unlock( &worker->lock );
 
             if( b_timeout ||
                 worker->conf.pf_probe( worker->owner, handle ) )
@@ -113,14 +108,14 @@ static void* Thread( void* data )
                 break;
             }
 
-            vlc_mutex_lock( &worker->head.lock );
+            vlc_mutex_lock( &worker->lock );
             if( worker->head.probe_request == false &&
                 worker->head.deadline > mdate() )
             {
-                vlc_cond_timedwait( &worker->head.wait, &worker->head.lock,
+                vlc_cond_timedwait( &worker->head.wait, &worker->lock,
                                      worker->head.deadline );
             }
-            vlc_mutex_unlock( &worker->head.lock );
+            vlc_mutex_unlock( &worker->lock );
         }
     }
 
@@ -129,7 +124,7 @@ static void* Thread( void* data )
 
 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 ); )
     {
         struct bg_queued_item* item =
@@ -145,17 +140,15 @@ static void BackgroundWorkerCancel( struct background_worker* worker, void* id)
 
         ++i;
     }
-    vlc_mutex_unlock( &worker->tail.lock );
 
-    vlc_mutex_lock( &worker->head.lock );
     while( ( id == NULL && worker->head.active )
         || ( id != NULL && worker->head.id == id ) )
     {
         worker->head.deadline = VLC_TS_0;
         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,
@@ -172,11 +165,10 @@ struct background_worker* background_worker_New( void* owner,
     worker->head.active = false;
     worker->head.deadline = VLC_TS_INVALID;
 
-    vlc_mutex_init( &worker->head.lock );
+    vlc_mutex_init( &worker->lock );
     vlc_cond_init( &worker->head.wait );
 
     vlc_array_init( &worker->tail.data );
-    vlc_mutex_init( &worker->tail.lock );
 
     return worker;
 }
@@ -193,11 +185,9 @@ int background_worker_Push( struct background_worker* worker, void* entity,
     item->entity = entity;
     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_mutex_unlock( &worker->tail.lock );
 
-    vlc_mutex_lock( &worker->head.lock );
     if( worker->head.active == false )
     {
         worker->head.probe_request = false;
@@ -209,7 +199,7 @@ int background_worker_Push( struct background_worker* worker, void* entity,
         worker->conf.pf_hold( item->entity );
 
     int ret = worker->head.active ? VLC_SUCCESS : VLC_EGENERIC;
-    vlc_mutex_unlock( &worker->head.lock );
+    vlc_mutex_unlock( &worker->lock );
 
     return ret;
 }
@@ -221,10 +211,10 @@ void background_worker_Cancel( struct background_worker* worker, void* id )
 
 void background_worker_RequestProbe( struct background_worker* worker )
 {
-    vlc_mutex_lock( &worker->head.lock );
+    vlc_mutex_lock( &worker->lock );
     worker->head.probe_request = true;
     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 )



More information about the vlc-commits mailing list