diff mbox

[FFmpeg-devel,2/3,v2] avformat: use mutexes instead of atomics in av_register_{input, output}_format()

Message ID 20180105014138.10156-1-jamrial@gmail.com
State Accepted
Commit 57960b1f2800e59a46de2b03f7b37ef6ef1c1e52
Headers show

Commit Message

James Almer Jan. 5, 2018, 1:41 a.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/format.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

Comments

wm4 Jan. 5, 2018, 4:03 p.m. UTC | #1
On Thu,  4 Jan 2018 22:41:38 -0300
James Almer <jamrial@gmail.com> wrote:

> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavformat/format.c | 34 +++++++++++++++++++++++-----------
>  1 file changed, 23 insertions(+), 11 deletions(-)
> 
> diff --git a/libavformat/format.c b/libavformat/format.c
> index 38ca2a3465..759b5b1ab4 100644
> --- a/libavformat/format.c
> +++ b/libavformat/format.c
> @@ -19,10 +19,10 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>  
> -#include "libavutil/atomic.h"
>  #include "libavutil/avstring.h"
>  #include "libavutil/bprint.h"
>  #include "libavutil/opt.h"
> +#include "libavutil/thread.h"
>  
>  #include "avio_internal.h"
>  #include "avformat.h"
> @@ -58,28 +58,40 @@ AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
>          return first_oformat;
>  }
>  
> +static AVMutex iformat_register_mutex = AV_MUTEX_INITIALIZER;
> +
>  void av_register_input_format(AVInputFormat *format)
>  {
> -    AVInputFormat **p = last_iformat;
> +    AVInputFormat **p;
> +
> +    ff_mutex_lock(&iformat_register_mutex);
> +    p = last_iformat;
>  
> -    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
> -    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
> +    while (*p)
>          p = &(*p)->next;
> +    *p = format;
> +    format->next = NULL;
> +    last_iformat = &format->next;
>  
> -    if (!format->next)
> -        last_iformat = &format->next;
> +    ff_mutex_unlock(&iformat_register_mutex);
>  }
>  
> +static AVMutex oformat_register_mutex = AV_MUTEX_INITIALIZER;
> +
>  void av_register_output_format(AVOutputFormat *format)
>  {
> -    AVOutputFormat **p = last_oformat;
> +    AVOutputFormat **p;
> +
> +    ff_mutex_lock(&oformat_register_mutex);
> +    p = last_oformat;
>  
> -    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
> -    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
> +    while (*p)
>          p = &(*p)->next;
> +    *p = format;
> +    format->next = NULL;
> +    last_oformat = &format->next;
>  
> -    if (!format->next)
> -        last_oformat = &format->next;
> +    ff_mutex_unlock(&oformat_register_mutex);
>  }
>  
>  int av_match_ext(const char *filename, const char *extensions)

Can't see anything wrong with it.
James Almer Jan. 5, 2018, 4:31 p.m. UTC | #2
On 1/5/2018 1:03 PM, wm4 wrote:
> On Thu,  4 Jan 2018 22:41:38 -0300
> James Almer <jamrial@gmail.com> wrote:
> 
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>  libavformat/format.c | 34 +++++++++++++++++++++++-----------
>>  1 file changed, 23 insertions(+), 11 deletions(-)
>>
>> diff --git a/libavformat/format.c b/libavformat/format.c
>> index 38ca2a3465..759b5b1ab4 100644
>> --- a/libavformat/format.c
>> +++ b/libavformat/format.c
>> @@ -19,10 +19,10 @@
>>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>>   */
>>  
>> -#include "libavutil/atomic.h"
>>  #include "libavutil/avstring.h"
>>  #include "libavutil/bprint.h"
>>  #include "libavutil/opt.h"
>> +#include "libavutil/thread.h"
>>  
>>  #include "avio_internal.h"
>>  #include "avformat.h"
>> @@ -58,28 +58,40 @@ AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
>>          return first_oformat;
>>  }
>>  
>> +static AVMutex iformat_register_mutex = AV_MUTEX_INITIALIZER;
>> +
>>  void av_register_input_format(AVInputFormat *format)
>>  {
>> -    AVInputFormat **p = last_iformat;
>> +    AVInputFormat **p;
>> +
>> +    ff_mutex_lock(&iformat_register_mutex);
>> +    p = last_iformat;
>>  
>> -    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
>> -    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
>> +    while (*p)
>>          p = &(*p)->next;
>> +    *p = format;
>> +    format->next = NULL;
>> +    last_iformat = &format->next;
>>  
>> -    if (!format->next)
>> -        last_iformat = &format->next;
>> +    ff_mutex_unlock(&iformat_register_mutex);
>>  }
>>  
>> +static AVMutex oformat_register_mutex = AV_MUTEX_INITIALIZER;
>> +
>>  void av_register_output_format(AVOutputFormat *format)
>>  {
>> -    AVOutputFormat **p = last_oformat;
>> +    AVOutputFormat **p;
>> +
>> +    ff_mutex_lock(&oformat_register_mutex);
>> +    p = last_oformat;
>>  
>> -    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
>> -    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
>> +    while (*p)
>>          p = &(*p)->next;
>> +    *p = format;
>> +    format->next = NULL;
>> +    last_oformat = &format->next;
>>  
>> -    if (!format->next)
>> -        last_oformat = &format->next;
>> +    ff_mutex_unlock(&oformat_register_mutex);
>>  }
>>  
>>  int av_match_ext(const char *filename, const char *extensions)
> 
> Can't see anything wrong with it.

Pushed.
diff mbox

Patch

diff --git a/libavformat/format.c b/libavformat/format.c
index 38ca2a3465..759b5b1ab4 100644
--- a/libavformat/format.c
+++ b/libavformat/format.c
@@ -19,10 +19,10 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/atomic.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
 #include "libavutil/opt.h"
+#include "libavutil/thread.h"
 
 #include "avio_internal.h"
 #include "avformat.h"
@@ -58,28 +58,40 @@  AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
         return first_oformat;
 }
 
+static AVMutex iformat_register_mutex = AV_MUTEX_INITIALIZER;
+
 void av_register_input_format(AVInputFormat *format)
 {
-    AVInputFormat **p = last_iformat;
+    AVInputFormat **p;
+
+    ff_mutex_lock(&iformat_register_mutex);
+    p = last_iformat;
 
-    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
-    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
+    while (*p)
         p = &(*p)->next;
+    *p = format;
+    format->next = NULL;
+    last_iformat = &format->next;
 
-    if (!format->next)
-        last_iformat = &format->next;
+    ff_mutex_unlock(&iformat_register_mutex);
 }
 
+static AVMutex oformat_register_mutex = AV_MUTEX_INITIALIZER;
+
 void av_register_output_format(AVOutputFormat *format)
 {
-    AVOutputFormat **p = last_oformat;
+    AVOutputFormat **p;
+
+    ff_mutex_lock(&oformat_register_mutex);
+    p = last_oformat;
 
-    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
-    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
+    while (*p)
         p = &(*p)->next;
+    *p = format;
+    format->next = NULL;
+    last_oformat = &format->next;
 
-    if (!format->next)
-        last_oformat = &format->next;
+    ff_mutex_unlock(&oformat_register_mutex);
 }
 
 int av_match_ext(const char *filename, const char *extensions)