diff mbox

[FFmpeg-devel,6/7] avutil/opencl: convert to stdatomic

Message ID 20170322233412.6952-7-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer March 22, 2017, 11:34 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavutil/opencl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

wm4 March 23, 2017, 6:11 a.m. UTC | #1
On Wed, 22 Mar 2017 20:34:11 -0300
James Almer <jamrial@gmail.com> wrote:

> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavutil/opencl.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/libavutil/opencl.c b/libavutil/opencl.c
> index af35770e06..9b6c8d10d8 100644
> --- a/libavutil/opencl.c
> +++ b/libavutil/opencl.c
> @@ -29,7 +29,7 @@
>  
>  #if HAVE_THREADS
>  #include "thread.h"
> -#include "atomic.h"
> +#include <stdatomic.h>
>  
>  static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
>  #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
> @@ -351,13 +351,14 @@ static inline int init_opencl_mtx(void)
>      if (!atomic_opencl_lock) {
>          int err;
>          pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
> +        const pthread_mutex_t *cmp = NULL;
>          if (!tmp)
>              return AVERROR(ENOMEM);
>          if ((err = pthread_mutex_init(tmp, NULL))) {
>              av_free(tmp);
>              return AVERROR(err);
>          }
> -        if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock, NULL, tmp)) {
> +        if (!atomic_compare_exchange_strong(&atomic_opencl_lock, &cmp, tmp)) {
>              pthread_mutex_destroy(tmp);
>              av_free(tmp);
>          }

Uh I guess that's ok (we really should add static mutex initializer
emulation on Windows).
wm4 March 23, 2017, 6:12 a.m. UTC | #2
On Wed, 22 Mar 2017 20:34:11 -0300
James Almer <jamrial@gmail.com> wrote:

> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavutil/opencl.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/libavutil/opencl.c b/libavutil/opencl.c
> index af35770e06..9b6c8d10d8 100644
> --- a/libavutil/opencl.c
> +++ b/libavutil/opencl.c
> @@ -29,7 +29,7 @@
>  
>  #if HAVE_THREADS
>  #include "thread.h"
> -#include "atomic.h"
> +#include <stdatomic.h>
>  
>  static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
>  #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
> @@ -351,13 +351,14 @@ static inline int init_opencl_mtx(void)
>      if (!atomic_opencl_lock) {
>          int err;
>          pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
> +        const pthread_mutex_t *cmp = NULL;
>          if (!tmp)
>              return AVERROR(ENOMEM);
>          if ((err = pthread_mutex_init(tmp, NULL))) {
>              av_free(tmp);
>              return AVERROR(err);
>          }
> -        if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock, NULL, tmp)) {
> +        if (!atomic_compare_exchange_strong(&atomic_opencl_lock, &cmp, tmp)) {
>              pthread_mutex_destroy(tmp);
>              av_free(tmp);
>          }

PS: will work with real atomics only. Sorry for the PITA.
Wei Gao Aug. 15, 2017, 3:03 a.m. UTC | #3
2017-03-23 7:34 GMT+08:00 James Almer <jamrial@gmail.com>:

> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavutil/opencl.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/libavutil/opencl.c b/libavutil/opencl.c
> index af35770e06..9b6c8d10d8 100644
> --- a/libavutil/opencl.c
> +++ b/libavutil/opencl.c
> @@ -29,7 +29,7 @@
>
>  #if HAVE_THREADS
>  #include "thread.h"
> -#include "atomic.h"
> +#include <stdatomic.h>
>
>  static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
>  #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
> @@ -351,13 +351,14 @@ static inline int init_opencl_mtx(void)
>      if (!atomic_opencl_lock) {
>          int err;
>          pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
> +        const pthread_mutex_t *cmp = NULL;
>          if (!tmp)
>              return AVERROR(ENOMEM);
>          if ((err = pthread_mutex_init(tmp, NULL))) {
>              av_free(tmp);
>              return AVERROR(err);
>          }
> -        if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock,
> NULL, tmp)) {
> +        if (!atomic_compare_exchange_strong(&atomic_opencl_lock, &cmp,
> tmp)) {
>              pthread_mutex_destroy(tmp);
>              av_free(tmp);
>          }
> --
> 2.12.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Hi
Looks good to me, thanks
diff mbox

Patch

diff --git a/libavutil/opencl.c b/libavutil/opencl.c
index af35770e06..9b6c8d10d8 100644
--- a/libavutil/opencl.c
+++ b/libavutil/opencl.c
@@ -29,7 +29,7 @@ 
 
 #if HAVE_THREADS
 #include "thread.h"
-#include "atomic.h"
+#include <stdatomic.h>
 
 static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
 #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
@@ -351,13 +351,14 @@  static inline int init_opencl_mtx(void)
     if (!atomic_opencl_lock) {
         int err;
         pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
+        const pthread_mutex_t *cmp = NULL;
         if (!tmp)
             return AVERROR(ENOMEM);
         if ((err = pthread_mutex_init(tmp, NULL))) {
             av_free(tmp);
             return AVERROR(err);
         }
-        if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock, NULL, tmp)) {
+        if (!atomic_compare_exchange_strong(&atomic_opencl_lock, &cmp, tmp)) {
             pthread_mutex_destroy(tmp);
             av_free(tmp);
         }