gst: don't use volatile to mean atomic
authorMatthew Waters <matthew@centricular.com>
Fri, 19 Mar 2021 06:42:36 +0000 (17:42 +1100)
committerMatthew Waters <matthew@centricular.com>
Fri, 19 Mar 2021 08:03:54 +0000 (19:03 +1100)
volatile is not sufficient to provide atomic guarantees and real atomics
should be used instead.  GCC 11 has started warning about using volatile
with atomic operations.

https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1719

Discovered in https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/issues/868

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-vaapi/-/merge_requests/418>

17 files changed:
gst-libs/gst/vaapi/gstvaapicontext.c
gst-libs/gst/vaapi/gstvaapicontext.h
gst-libs/gst/vaapi/gstvaapiencoder.c
gst-libs/gst/vaapi/gstvaapifilter.c
gst-libs/gst/vaapi/gstvaapiminiobject.c
gst-libs/gst/vaapi/gstvaapiminiobject.h
gst-libs/gst/vaapi/gstvaapitexture_glx.c
gst-libs/gst/vaapi/gstvaapiutils_egl.c
gst-libs/gst/vaapi/gstvaapiutils_egl.h
gst-libs/gst/vaapi/gstvaapivalue.c
gst-libs/gst/vaapi/gstvaapiwindow_wayland.c
gst/vaapi/gstvaapipluginbase.c
gst/vaapi/gstvaapisink.c
gst/vaapi/gstvaapisink.h
gst/vaapi/gstvaapivideocontext.c
gst/vaapi/gstvaapivideomemory.c
tests/internal/simple-decoder.c

index 52fd751..88cd110 100644 (file)
@@ -51,7 +51,7 @@ static void
 _init_vaapi_context_debug (void)
 {
 #ifndef GST_DISABLE_GST_DEBUG
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_context, "vaapicontext", 0,
index 820fa63..7117722 100644 (file)
@@ -100,7 +100,7 @@ struct _GstVaapiContextInfo
 struct _GstVaapiContext
 {
   /*< private >*/
-  volatile gint ref_count;
+  gint ref_count;
   GstVaapiDisplay *display;
   GstVaapiID object_id;
 
index 83d26f0..6c103ac 100644 (file)
@@ -1822,7 +1822,7 @@ out:
 GType
 gst_vaapi_encoder_tune_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   static const GEnumValue encoder_tune_values[] = {
     /* *INDENT-OFF* */
@@ -1850,7 +1850,7 @@ gst_vaapi_encoder_tune_get_type (void)
 GType
 gst_vaapi_encoder_mbbrc_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   if (g_once_init_enter (&g_type)) {
     static const GEnumValue encoder_mbbrc_values[] = {
index a9443e4..e0b3cd5 100644 (file)
@@ -38,7 +38,7 @@ struct _GstVaapiFilterOpData
 {
   GstVaapiFilterOp op;
   GParamSpec *pspec;
-  volatile gint ref_count;
+  gint ref_count;
   guint va_type;
   guint va_subtype;
   gpointer va_caps;
@@ -550,7 +550,7 @@ op_data_new (GstVaapiFilterOp op, GParamSpec * pspec)
 
   op_data->op = op;
   op_data->pspec = pspec;
-  op_data->ref_count = 1;
+  g_atomic_int_set (&op_data->ref_count, 1);
   op_data->va_buffer = VA_INVALID_ID;
 
   switch (op) {
index 7b18983..30586b0 100644 (file)
@@ -70,7 +70,7 @@ gst_vaapi_mini_object_new (const GstVaapiMiniObjectClass * object_class)
     return NULL;
 
   object->object_class = object_class;
-  object->ref_count = 1;
+  g_atomic_int_set (&object->ref_count, 1);
   object->flags = 0;
   return object;
 }
index 778fc07..52f2863 100644 (file)
@@ -120,7 +120,7 @@ struct _GstVaapiMiniObject
 {
   /*< private >*/
   gconstpointer object_class;
-  volatile gint ref_count;
+  gint ref_count;
   guint flags;
 };
 
index 273afc9..76f450a 100644 (file)
@@ -249,7 +249,7 @@ GstVaapiGLApi
 gl_get_curent_api_once ()
 {
   static GstVaapiGLApi cur_api = GST_VAAPI_GL_API_NONE;
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     cur_api = gl_get_current_api (NULL, NULL);
index 0c2a1f5..99ae37d 100644 (file)
@@ -614,7 +614,7 @@ egl_display_thread (gpointer data)
   g_cond_broadcast (&display->gl_thread_ready);
   g_mutex_unlock (&display->mutex);
 
-  while (!display->gl_thread_cancel) {
+  while (!g_atomic_int_get (&display->gl_thread_cancel)) {
     EglMessage *const msg =
         g_async_queue_timeout_pop (display->gl_queue, 100000);
 
@@ -671,7 +671,7 @@ egl_display_init (EglDisplay * display)
 static void
 egl_display_finalize (EglDisplay * display)
 {
-  display->gl_thread_cancel = TRUE;
+  g_atomic_int_set (&display->gl_thread_cancel, TRUE);
   g_thread_join (display->gl_thread);
   g_cond_clear (&display->gl_thread_ready);
   g_mutex_clear (&display->mutex);
index abf3735..20b553e 100644 (file)
@@ -120,7 +120,7 @@ struct egl_display_s
   GMutex mutex;
   GThread *gl_thread;
   GCond gl_thread_ready;
-  volatile gboolean gl_thread_cancel;
+  gboolean gl_thread_cancel;
   GAsyncQueue *gl_queue;
   gboolean created;
 };
index 1e01213..62e440b 100644 (file)
@@ -50,7 +50,7 @@ default_free_func (gpointer data)
 GType
 gst_vaapi_point_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   if (g_once_init_enter (&g_type)) {
     GType type =
@@ -67,7 +67,7 @@ gst_vaapi_point_get_type (void)
 GType
 gst_vaapi_rectangle_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   if (g_once_init_enter (&g_type)) {
     GType type =
@@ -85,7 +85,7 @@ gst_vaapi_rectangle_get_type (void)
 GType
 gst_vaapi_render_mode_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   static const GEnumValue render_modes[] = {
     {GST_VAAPI_RENDER_MODE_OVERLAY,
@@ -108,7 +108,7 @@ gst_vaapi_render_mode_get_type (void)
 GType
 gst_vaapi_rotation_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   static const GEnumValue rotation_values[] = {
     {GST_VAAPI_ROTATION_0,
@@ -137,7 +137,7 @@ gst_vaapi_rotation_get_type (void)
 GType
 gst_vaapi_rate_control_get_type (void)
 {
-  static volatile gsize g_type = 0;
+  static gsize g_type = 0;
 
   static const GEnumValue rate_control_values[] = {
     {GST_VAAPI_RATECONTROL_NONE,
index caf1dc6..394a089 100644 (file)
@@ -98,7 +98,7 @@ struct _GstVaapiWindowWaylandPrivate
   guint is_shown:1;
   guint fullscreen_on_show:1;
   guint sync_failed:1;
-  volatile guint num_frames_pending;
+  guint num_frames_pending;
   gint configure_pending;
   gboolean need_vpp;
   gboolean dmabuf_broken;
index 3a8664b..a670e97 100644 (file)
@@ -1546,7 +1546,7 @@ static void
 _init_performance_debug (void)
 {
 #ifndef GST_DISABLE_GST_DEBUG
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
index e722b71..9b5ae98 100644 (file)
@@ -976,7 +976,7 @@ static gpointer
 gst_vaapisink_event_thread (GstVaapiSink * sink)
 {
   GST_OBJECT_LOCK (sink);
-  while (!sink->event_thread_cancel) {
+  while (!g_atomic_int_get (&sink->event_thread_cancel)) {
     GST_OBJECT_UNLOCK (sink);
     sink->backend->handle_events (sink);
     g_usleep (G_USEC_PER_SEC / 20);
@@ -1001,7 +1001,7 @@ gst_vaapisink_set_event_handling (GstVaapiSink * sink, gboolean handle_events)
     if (sink->backend->pre_start_event_thread)
       sink->backend->pre_start_event_thread (sink);
 
-    sink->event_thread_cancel = FALSE;
+    g_atomic_int_set (&sink->event_thread_cancel, FALSE);
     sink->event_thread = g_thread_try_new ("vaapisink-events",
         (GThreadFunc) gst_vaapisink_event_thread, sink, NULL);
   } else if (!handle_events && sink->event_thread) {
@@ -1012,7 +1012,7 @@ gst_vaapisink_set_event_handling (GstVaapiSink * sink, gboolean handle_events)
     /* Grab thread and mark it as NULL */
     thread = sink->event_thread;
     sink->event_thread = NULL;
-    sink->event_thread_cancel = TRUE;
+    g_atomic_int_set (&sink->event_thread_cancel, TRUE);
   }
   GST_OBJECT_UNLOCK (sink);
 
index 1fb6e83..525f422 100644 (file)
@@ -99,7 +99,7 @@ struct _GstVaapiSink
   guint color_standard;
   gint32 view_id;
   GThread *event_thread;
-  volatile gboolean event_thread_cancel;
+  gboolean event_thread_cancel;
 
   /* Color balance values */
   guint cb_changed;
index bd3db01..ae3af85 100644 (file)
@@ -44,7 +44,7 @@ static void
 _init_context_debug (void)
 {
 #ifndef GST_DISABLE_GST_DEBUG
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
index abf9715..750385a 100644 (file)
@@ -47,7 +47,7 @@ static void
 _init_performance_debug (void)
 {
 #ifndef GST_DISABLE_GST_DEBUG
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
@@ -60,7 +60,7 @@ static void
 _init_vaapi_video_memory_debug (void)
 {
 #ifndef GST_DISABLE_GST_DEBUG
-  static volatile gsize _init = 0;
+  static gsize _init = 0;
 
   if (g_once_init_enter (&_init)) {
     GST_DEBUG_CATEGORY_INIT (gst_debug_vaapivideomemory, "vaapivideomemory", 0,
index 134b845..7575276 100644 (file)
@@ -85,7 +85,7 @@ typedef struct
   GstVaapiDisplay *display;
   GstVaapiDecoder *decoder;
   GThread *decoder_thread;
-  volatile gboolean decoder_thread_cancel;
+  gboolean decoder_thread_cancel;
   GAsyncQueue *decoder_queue;
   GstVaapiCodec codec;
   guint fps_n;
@@ -97,7 +97,7 @@ typedef struct
   guint window_width;
   guint window_height;
   GThread *render_thread;
-  volatile gboolean render_thread_cancel;
+  gboolean render_thread_cancel;
   GCond render_ready;
   RenderFrame *last_frame;
   GError *error;
@@ -241,7 +241,7 @@ decoder_thread (gpointer data)
 
   pts = g_get_monotonic_time ();
   ofs = 0;
-  while (!app->decoder_thread_cancel) {
+  while (!g_atomic_int_get (&app->decoder_thread_cancel)) {
     if (G_UNLIKELY (ofs == app->file_size))
       buffer = NULL;
     else {
@@ -376,7 +376,7 @@ stop_decoder (App * app)
 {
   g_timer_stop (app->timer);
 
-  app->decoder_thread_cancel = TRUE;
+  g_atomic_int_set (&app->decoder_thread_cancel, TRUE);
   g_thread_join (app->decoder_thread);
   g_print ("Decoder thread stopped\n");
   return TRUE;
@@ -462,7 +462,7 @@ renderer_thread (gpointer data)
 
   g_print ("Render thread started\n");
 
-  while (!app->render_thread_cancel) {
+  while (!g_atomic_int_get (&app->render_thread_cancel)) {
     rfp = g_async_queue_timeout_pop (app->decoder_queue, 1000000);
     if (rfp && !renderer_process (app, rfp))
       break;
@@ -497,7 +497,7 @@ start_renderer (App * app)
 static gboolean
 stop_renderer (App * app)
 {
-  app->render_thread_cancel = TRUE;
+  g_atomic_int_set (&app->render_thread_cancel, TRUE);
   g_thread_join (app->render_thread);
 
   g_print ("Render thread stopped\n");