After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 660744 - finish killing g_thread_init()
finish killing g_thread_init()
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gthread
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks: 660747
 
 
Reported: 2011-10-03 05:18 UTC by Allison Karlitskaya (desrt)
Modified: 2011-10-05 09:31 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Clean up process of calling g_debug_init() (5.48 KB, patch)
2011-10-04 02:02 UTC, Allison Karlitskaya (desrt)
committed Details | Review
Deprecate g_thread_init() (22.16 KB, patch)
2011-10-04 02:02 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
Deprecate g_thread_init() (29.91 KB, patch)
2011-10-04 14:52 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Allison Karlitskaya (desrt) 2011-10-03 05:18:57 UTC
All of the synchronisation primatives (mutex, cond, recmutex, rwlock, private) are completely free of the need for g_thread_init(), and g_thread_new() works without it too.

There are just a couple of places left inside GLib that have thread-unsafe initialisation that g_thread_init() forces to occur in single-threaded context.

We need to finish up the ctor story to make this work.
Comment 1 Allison Karlitskaya (desrt) 2011-10-04 02:02:38 UTC
Created attachment 198173 [details] [review]
Clean up process of calling g_debug_init()

Make sure that it calls absolutely nothing that may ever recurse back
into GLib again:

  - g_ascii_strcasecmp() is unsafe because it has g_return_if_fail() at
    the top.  As far as I know, the only ASCII character letter that
    would get special treatment here is "i" and that appears in neither
    "help" nor "all".

  - g_getenv() is very complicated on Windows, so use a simple version
    that is sufficient for our purposes.

Now that it's completely safe, we can just call it from g_logv() in the
usual way without all of the hacks.
Comment 2 Allison Karlitskaya (desrt) 2011-10-04 02:02:41 UTC
Created attachment 198174 [details] [review]
Deprecate g_thread_init()

Move the last few things that needed thread-safe initialisation to a
global ctor.
Comment 3 Matthias Clasen 2011-10-04 11:12:16 UTC
Review of attachment 198173 [details] [review]:

Looks good to me.
Comment 4 Matthias Clasen 2011-10-04 11:16:58 UTC
Review of attachment 198174 [details] [review]:

Forgot git add glib-init.[hc] ?
Comment 5 Matthias Clasen 2011-10-04 11:23:08 UTC
Review of attachment 198174 [details] [review]:

Also, we need to add a paragraph about non-threadsafe setup code to the thread docs. Where we explain that it is safe to call things like setenv(), etc
before creating the first thread.

We should also say somewhere that it is no longer necessary to link against gthread, and review the docs for mentions of it.

::: glib/deprecated/gthread-deprecated.c
@@ +139,3 @@
+ * other thread-related functions of GLib. Those can be used without
+ * having to link with the thread libraries.</para></note>
+ */

Needs a 

Deprecated:2.32: It is no longer necessary to explicitly initialize the thread system before using any thread-related functions.

@@ +148,3 @@
+ * Returns: %TRUE if threads have been initialized.
+ *
+ * Since: 2.20

Needs a

Deprecated:2.32: Threads are always initialized.
Comment 6 Allison Karlitskaya (desrt) 2011-10-04 14:52:33 UTC
Created attachment 198223 [details] [review]
Deprecate g_thread_init()

Move the last few things that needed thread-safe initialisation to a
global ctor.
Comment 7 Colin Walters 2011-10-04 15:12:42 UTC
Review of attachment 198223 [details] [review]:

::: glib/deprecated/gthread-deprecated.c
@@ +139,3 @@
+ * other thread-related functions of GLib. Those can be used without
+ * having to link with the thread libraries.</para></note>
+ */

This is where a dummy g_thread_init() should go.  (And a dummy g_thread_init_glib()).

::: glib/glib-init.c
@@ +243,3 @@
+
+#else
+# error Your platform/compiler is missing constructor support

This would be a regression on those platforms.  I'd prefer we not commit this patch until we have some plan for fallbacks in place.  Probably these compilers as a starting list:

* LLVM
* Sun CC
* MSVC

One approach is to just keep the internal calls to g_thread_init(), and make it a no-op on GCC.  The downside is this is extremely likely to bitrot over time.

We could go with the C++ fallback and get good coverage I think.

::: glib/glib.symbols
@@ -1096,3 +1096,2 @@
 g_thread_exit
 g_thread_functions_for_glib_use
-g_thread_init_glib

I'd prefer not to remove any symbols.  Yes, no one should be calling it, but it's still something that can be flagged as an ABI change.
Comment 8 Allison Karlitskaya (desrt) 2011-10-04 15:24:24 UTC
Review of attachment 198223 [details] [review]:

::: glib/deprecated/gthread-deprecated.c
@@ +139,3 @@
+ * other thread-related functions of GLib. Those can be used without
+ * having to link with the thread libraries.</para></note>
+ */

This is a little bit complicated.

The dummy g_thread_init() needs to be in libgthread.  That's a consequence of the fact that some people might have done direct linking (and therefore expect to find that symbol in exactly that library).

At the same time, we might want to have it in libglib as well so that we can modify the gthread pkgconfig file to no longer link to libgthread without breaking people who still call g_thread_init().  On the other hand, maybe we could just leave the pkg-config alone and advise people to stop using it.

Another option (one that I don't particular care for) would be to symlink libgthread to libglib.  Apparently that works, and would solve both issues.

::: glib/glib-init.c
@@ +243,3 @@
+
+#else
+# error Your platform/compiler is missing constructor support

MSVC is handled by the G_OS_WIN32 case.

I think clang supports the GCC attributes (and even defines __GNUC__ based on some bug reports we've seen).

Alex is best equipped to comment on this.

::: glib/glib.symbols
@@ -1096,3 +1096,2 @@
 g_thread_exit
 g_thread_functions_for_glib_use
-g_thread_init_glib

I had a feeling you'd have a problem with this :)

I'll add it back in as a do-nothing.
Comment 9 Allison Karlitskaya (desrt) 2011-10-04 19:32:06 UTC
Comment on attachment 198223 [details] [review]
Deprecate g_thread_init()

Committed with g_thread_init_glib() added back in.
Comment 10 Alexander Larsson 2011-10-05 09:31:30 UTC
Review of attachment 198223 [details] [review]:

::: glib/glib-init.c
@@ +243,3 @@
+
+#else
+# error Your platform/compiler is missing constructor support

This should work fine for win32 and gcc, and llvm.
We should make sure to add support for sun cc though. Should be easy as this is open-coded rather than a generally usable macro.