diff mbox series

[FFmpeg-devel,1/3] avcodec/mlp: Make initializing CRCs thread-safe

Message ID 20201121121728.1018014-1-andreas.rheinhardt@gmail.com
State Accepted
Commit 1f0e27dd66dabe243b289aa332525e99036feea5
Headers show
Series [FFmpeg-devel,1/3] avcodec/mlp: Make initializing CRCs thread-safe | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Nov. 21, 2020, 12:17 p.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mlp.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

Comments

Andreas Rheinhardt Nov. 23, 2020, 7:59 p.m. UTC | #1
Andreas Rheinhardt:
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/mlp.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/libavcodec/mlp.c b/libavcodec/mlp.c
> index ddbab60c4e..74363c3b95 100644
> --- a/libavcodec/mlp.c
> +++ b/libavcodec/mlp.c
> @@ -23,6 +23,7 @@
>  
>  #include "libavutil/crc.h"
>  #include "libavutil/intreadwrite.h"
> +#include "libavutil/thread.h"
>  #include "mlp.h"
>  
>  const uint8_t ff_mlp_huffman_tables[3][18][2] = {
> @@ -62,7 +63,6 @@ const uint64_t ff_mlp_channel_layouts[12] = {
>      AV_CH_LAYOUT_4POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0,
>  };
>  
> -static int crc_init = 0;
>  #if CONFIG_SMALL
>  #define CRC_TABLE_SIZE 257
>  #else
> @@ -72,14 +72,17 @@ static AVCRC crc_63[CRC_TABLE_SIZE];
>  static AVCRC crc_1D[CRC_TABLE_SIZE];
>  static AVCRC crc_2D[CRC_TABLE_SIZE];
>  
> +static av_cold void mlp_init_crc(void)
> +{
> +    av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
> +    av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
> +    av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
> +}
> +
>  av_cold void ff_mlp_init_crc(void)
>  {
> -    if (!crc_init) {
> -        av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
> -        av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
> -        av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
> -        crc_init = 1;
> -    }
> +    static AVOnce init_static_once = AV_ONCE_INIT;
> +    ff_thread_once(&init_static_once, mlp_init_crc);
>  }
>  
>  uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
> 
Will apply this patchset tomorrow unless there are objections.

- Andreas
Jai Luthra Nov. 23, 2020, 9:36 p.m. UTC | #2
LGTM

On Tue, Nov 24, 2020, at 1:29 AM, Andreas Rheinhardt wrote:
> Andreas Rheinhardt:
> > Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> > ---
> >  libavcodec/mlp.c | 17 ++++++++++-------
> >  1 file changed, 10 insertions(+), 7 deletions(-)
> > 
> > diff --git a/libavcodec/mlp.c b/libavcodec/mlp.c
> > index ddbab60c4e..74363c3b95 100644
> > --- a/libavcodec/mlp.c
> > +++ b/libavcodec/mlp.c
> > @@ -23,6 +23,7 @@
> >  
> >  #include "libavutil/crc.h"
> >  #include "libavutil/intreadwrite.h"
> > +#include "libavutil/thread.h"
> >  #include "mlp.h"
> >  
> >  const uint8_t ff_mlp_huffman_tables[3][18][2] = {
> > @@ -62,7 +63,6 @@ const uint64_t ff_mlp_channel_layouts[12] = {
> >      AV_CH_LAYOUT_4POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0,
> >  };
> >  
> > -static int crc_init = 0;
> >  #if CONFIG_SMALL
> >  #define CRC_TABLE_SIZE 257
> >  #else
> > @@ -72,14 +72,17 @@ static AVCRC crc_63[CRC_TABLE_SIZE];
> >  static AVCRC crc_1D[CRC_TABLE_SIZE];
> >  static AVCRC crc_2D[CRC_TABLE_SIZE];
> >  
> > +static av_cold void mlp_init_crc(void)
> > +{
> > +    av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
> > +    av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
> > +    av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
> > +}
> > +
> >  av_cold void ff_mlp_init_crc(void)
> >  {
> > -    if (!crc_init) {
> > -        av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
> > -        av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
> > -        av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
> > -        crc_init = 1;
> > -    }
> > +    static AVOnce init_static_once = AV_ONCE_INIT;
> > +    ff_thread_once(&init_static_once, mlp_init_crc);
> >  }
> >  
> >  uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
> > 
> Will apply this patchset tomorrow unless there are objections.
> 
> - Andreas
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/mlp.c b/libavcodec/mlp.c
index ddbab60c4e..74363c3b95 100644
--- a/libavcodec/mlp.c
+++ b/libavcodec/mlp.c
@@ -23,6 +23,7 @@ 
 
 #include "libavutil/crc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/thread.h"
 #include "mlp.h"
 
 const uint8_t ff_mlp_huffman_tables[3][18][2] = {
@@ -62,7 +63,6 @@  const uint64_t ff_mlp_channel_layouts[12] = {
     AV_CH_LAYOUT_4POINT1, AV_CH_LAYOUT_5POINT1_BACK, 0,
 };
 
-static int crc_init = 0;
 #if CONFIG_SMALL
 #define CRC_TABLE_SIZE 257
 #else
@@ -72,14 +72,17 @@  static AVCRC crc_63[CRC_TABLE_SIZE];
 static AVCRC crc_1D[CRC_TABLE_SIZE];
 static AVCRC crc_2D[CRC_TABLE_SIZE];
 
+static av_cold void mlp_init_crc(void)
+{
+    av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
+    av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
+    av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
+}
+
 av_cold void ff_mlp_init_crc(void)
 {
-    if (!crc_init) {
-        av_crc_init(crc_63, 0,  8,   0x63, sizeof(crc_63));
-        av_crc_init(crc_1D, 0,  8,   0x1D, sizeof(crc_1D));
-        av_crc_init(crc_2D, 0, 16, 0x002D, sizeof(crc_2D));
-        crc_init = 1;
-    }
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, mlp_init_crc);
 }
 
 uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)