[vlc-devel] [PATCH 29/39] directx_va: replace the internal reference counter by an atomic value

Steve Lhomme robux4 at videolabs.io
Fri Jun 2 16:46:32 CEST 2017


---
 modules/codec/avcodec/directx_va.c | 10 +++++-----
 modules/codec/avcodec/directx_va.h |  3 ++-
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/modules/codec/avcodec/directx_va.c b/modules/codec/avcodec/directx_va.c
index 5bb0c3f85b..b83e4c38ab 100644
--- a/modules/codec/avcodec/directx_va.c
+++ b/modules/codec/avcodec/directx_va.c
@@ -349,7 +349,7 @@ int directx_va_Setup(vlc_va_t *va, directx_sys_t *dx_sys, AVCodecContext *avctx)
             dx_sys->surface_count = i;
             return VLC_ENOMEM;
         }
-        surface->refcount = 0;
+        atomic_init(&surface->refcount, 1);
         surface->order = 0;
         surface->p_lock = &dx_sys->surface_lock;
         surface->p_pic = dx_sys->pf_alloc_surface_pic(va, &fmt, i);
@@ -398,7 +398,7 @@ vlc_va_surface_t *directx_va_Get(vlc_va_t *va, directx_sys_t *dx_sys)
 
     for (i = 0; i < dx_sys->surface_count; i++) {
         vlc_va_surface_t *surface = dx_sys->surface[i];
-        if (((old == -1 || surface->order < dx_sys->surface[old]->order)) && !surface->refcount)
+        if (((old == -1 || surface->order < dx_sys->surface[old]->order)) && atomic_load(&surface->refcount))
             old = i;
         if (old_used == -1 || surface->order < dx_sys->surface[old_used]->order)
             old_used = i;
@@ -413,7 +413,7 @@ vlc_va_surface_t *directx_va_Get(vlc_va_t *va, directx_sys_t *dx_sys)
 
     vlc_va_surface_t *surface = dx_sys->surface[i];
 
-    surface->refcount = 1;
+    atomic_store(&surface->refcount,1);
     surface->order = ++dx_sys->surface_order;
     surface->decoderSurface = dx_sys->hw_surface[i];
 
@@ -425,14 +425,14 @@ vlc_va_surface_t *directx_va_Get(vlc_va_t *va, directx_sys_t *dx_sys)
 void directx_va_AddRef(vlc_va_surface_t *surface)
 {
     vlc_mutex_lock( surface->p_lock );
-    surface->refcount++;
+    atomic_fetch_add(&surface->refcount, 1);
     vlc_mutex_unlock( surface->p_lock );
 }
 
 void directx_va_Release(vlc_va_surface_t *surface)
 {
     vlc_mutex_lock( surface->p_lock );
-    surface->refcount--;
+    atomic_fetch_sub(&surface->refcount, 1);
     vlc_mutex_unlock( surface->p_lock );
 }
 
diff --git a/modules/codec/avcodec/directx_va.h b/modules/codec/avcodec/directx_va.h
index 85e25e2606..858b01705b 100644
--- a/modules/codec/avcodec/directx_va.h
+++ b/modules/codec/avcodec/directx_va.h
@@ -40,10 +40,11 @@
 #include "va.h"
 
 #include <unknwn.h>
+#include <stdatomic.h>
 
 /* */
 typedef struct {
-    int                refcount;
+    atomic_uintptr_t     refcount;
     unsigned int       order;
     vlc_mutex_t        *p_lock;
     picture_t          *p_pic;
-- 
2.12.1



More information about the vlc-devel mailing list