[Spice-devel] [PATCH spice-gtk] Make sure our foo_get_type functions are thread-safe

Hans de Goede hdegoede at redhat.com
Wed May 29 06:54:49 PDT 2013


While debugging:
https://bugzilla.redhat.com/show_bug.cgi?id=866718

I found a thread-safeness issue with a couple of foo_get_type functions
in polkit causing this crash.

After this I decided to check if spice-gtk has the same issue, and it does,
since foo_get_type can be called from different threads, it is important to
make them thread-safe.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
---
 gtk/Makefile.am          | 18 ++++++++++++------
 gtk/spice-grabsequence.c |  5 ++++-
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 17544fb..d31a396 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -455,9 +455,12 @@ spice-glib-enums.c: spice-channel.h channel-inputs.h spice-session.h
 			--vprod "  { @VALUENAME@, \"@VALUENAME@\", \"@valuenick@\" }," \
 			--vtail "  { 0, NULL, NULL }\n};\n\n" \
 			--vtail "GType\n at enum_name@_get_type (void)\n{\n" \
-			--vtail "  static GType type = 0;\n\n" \
-			--vtail "  if (!type)\n" \
-			--vtail "    type = g_ at type@_register_static (\"@EnumName@\", _ at enum_name@_values);\n\n" \
+			--vtail "  static GType type = 0;\n" \
+			--vtail "  static volatile gsize type_volatile = 0;\n\n" \
+			--vtail "  if (g_once_init_enter(&type_volatile)) {\n" \
+			--vtail "    type = g_ at type@_register_static (\"@EnumName@\", _ at enum_name@_values);\n" \
+			--vtail "    g_once_init_leave(&type_volatile, type);\n" \
+			--vtail "  }\n\n" \
 			--vtail "  return type;\n}\n\n" \
 		$^ > $@
 
@@ -479,9 +482,12 @@ spice-widget-enums.c: spice-widget.h
 			--vprod "  { @VALUENAME@, \"@VALUENAME@\", \"@valuenick@\" }," \
 			--vtail "  { 0, NULL, NULL }\n};\n\n" \
 			--vtail "GType\n at enum_name@_get_type (void)\n{\n" \
-			--vtail "  static GType type = 0;\n\n" \
-			--vtail "  if (!type)\n" \
-			--vtail "    type = g_ at type@_register_static (\"@EnumName@\", _ at enum_name@_values);\n\n" \
+			--vtail "  static GType type = 0;\n" \
+			--vtail "  static volatile gsize type_volatile = 0;\n\n" \
+			--vtail "  if (g_once_init_enter(&type_volatile)) {\n" \
+			--vtail "    type = g_ at type@_register_static (\"@EnumName@\", _ at enum_name@_values);\n" \
+			--vtail "    g_once_init_leave(&type_volatile, type);\n" \
+			--vtail "  }\n\n" \
 			--vtail "  return type;\n}\n\n" \
 		$< > $@
 
diff --git a/gtk/spice-grabsequence.c b/gtk/spice-grabsequence.c
index dab438c..5d6fa9f 100644
--- a/gtk/spice-grabsequence.c
+++ b/gtk/spice-grabsequence.c
@@ -28,12 +28,15 @@
 GType spice_grab_sequence_get_type(void)
 {
 	static GType grab_sequence_type = 0;
+	static volatile gsize grab_sequence_type_volatile;
 
-	if (G_UNLIKELY(grab_sequence_type == 0)) {
+	if (g_once_init_enter(&grab_sequence_type_volatile)) {
 		grab_sequence_type = g_boxed_type_register_static
 			("SpiceGrabSequence",
 			 (GBoxedCopyFunc)spice_grab_sequence_copy,
 			 (GBoxedFreeFunc)spice_grab_sequence_free);
+		g_once_init_leave(&grab_sequence_type_volatile,
+				  grab_sequence_type);
 	}
 
 	return grab_sequence_type;
-- 
1.8.2.1



More information about the Spice-devel mailing list