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 731986 - GLib: implement GMutex natively on Linux
GLib: implement GMutex natively on Linux
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2014-06-20 19:53 UTC by Allison Karlitskaya (desrt)
Modified: 2015-06-19 14:25 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GLib: implement GMutex natively on Linux (7.10 KB, patch)
2014-06-20 19:53 UTC, Allison Karlitskaya (desrt)
none Details | Review
GLib: implement GMutex natively on Linux (8.90 KB, patch)
2014-06-20 19:54 UTC, Allison Karlitskaya (desrt)
none Details | Review
GMutex (linux): detect and report some errors (2.11 KB, patch)
2014-07-02 22:38 UTC, Allison Karlitskaya (desrt)
none Details | Review
GCond (linux): fix g_cond_wait_until() return value on timeout (1.69 KB, patch)
2014-07-06 10:56 UTC, Tim-Philipp Müller
none Details | Review
GLib: implement GMutex natively on Linux (7.13 KB, patch)
2014-07-06 17:14 UTC, Allison Karlitskaya (desrt)
committed Details | Review
GMutex (linux): detect and report some errors (2.11 KB, patch)
2014-07-06 17:14 UTC, Allison Karlitskaya (desrt)
committed Details | Review
GCond (linux): fix g_cond_wait_until() return value on timeout (1.74 KB, patch)
2014-07-06 17:14 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Allison Karlitskaya (desrt) 2014-06-20 19:53:03 UTC
If we have futex(2) then we can implement GMutex natively and gain a
substantial performance increase (vs. using pthreads).

This also avoids the need to allocate an extra structure in memory when
using GMutex or GCond: we can use the structure directly.

The main reason for the increase in performance is that our
implementation can be made more simple: we don't need to support the
array of options on pthread_mutex_t (which includes the possibility, for
example, of being recursive).

The result is a ~30% improvement in uncontended cases and a much larger
increase (3 to 4 times) in contended cases for a simple testcase.
Comment 1 Allison Karlitskaya (desrt) 2014-06-20 19:53:05 UTC
Created attachment 278866 [details] [review]
GLib: implement GMutex natively on Linux
Comment 2 Allison Karlitskaya (desrt) 2014-06-20 19:54:26 UTC
Created attachment 278867 [details] [review]
GLib: implement GMutex natively on Linux

(just putting this here in case it comes in handy some day)

See as well: https://bugs.debian.org/731509
Comment 3 Dan Winship 2014-06-20 22:07:58 UTC
It would be nice if this could at least optionally do error checking, to avoid stuff like bug 678758. "Optionally" meaning configurable at runtime.

(In reply to comment #2)
> See as well: https://bugs.debian.org/731509

url typo?
Comment 4 Allison Karlitskaya (desrt) 2014-06-21 16:35:31 UTC
Re bug 678758, we could easily add a check for "unlock the unlocked" without degrading the performance of the fast path: we do a replace and ensure that the old value was 1 for the fast path before dispatching the slow path.

All we'd have to do is add a check on the slow path for it having been 0, and throw an abort.


As for the url, I believe it to be correct.  Why do you think this is a typo?  This is the only report I could find about getenv() not working inside of ifuncs...
Comment 5 Dan Winship 2014-06-21 23:17:50 UTC
ah, I didn't look in detail at the obsoleted patch, so I was assuming you meant to link to some debian bug that had something to do with futexes
Comment 6 Allison Karlitskaya (desrt) 2014-07-02 22:38:40 UTC
Created attachment 279801 [details] [review]
GMutex (linux): detect and report some errors

Detect the following two errors:

 - attempting to unlock a mutex that is not locked

 - attempting to clear a mutex that was not initialised or was
   initialised but is still locked

Both of these are fatal errors.  We avoid using g_error() here because
doing so would involve calls back into the GMutex code, and if things
are going off the rails then we want to avoid that.
Comment 7 Tim-Philipp Müller 2014-07-06 10:56:05 UTC
Created attachment 279981 [details] [review]
GCond (linux): fix g_cond_wait_until() return value on timeout

It should return FALSE on timeout (and only on timeout), and TRUE otherwise. Includes addition to the unit tests.
Comment 8 Tim-Philipp Müller 2014-07-06 11:06:56 UTC
This is great stuff. It improves performance of GStreamer's main data processing code paths (gst_pad_push()) by just over 20% according to my measurements.

The code looks good to me, didn't spot anything wrong, with the exception of the g_cond_wait_until() return value (see patch above).

Two minor niggles: the error fprintf() lines should have a trailing newline, and there's a code style inconsistency, sometimes you use foo->i and sometimes &foo->i[0]. I prefer &foo->i[0] since it makes it clearer that only one int is used here.

I have run these patches through the 'make check' test suites of all the main GStreamer modules (GStreamer core, gst-plugins-base, gst-plugins-good, gst-plugins-ugly, gst-plugins-bad, gst-libav, gst-rtsp-server), and 'make check-torture' (= run 'make check' 10 times in a loop) now passes successfully on all of them after I fixed a few bugs in GStreamer elements or tests (double unlocks).

Would be great if we could get this into the next release.
Comment 9 Allison Karlitskaya (desrt) 2014-07-06 17:14:51 UTC
Created attachment 280004 [details] [review]
GLib: implement GMutex natively on Linux

If we have futex(2) then we can implement GMutex natively and gain a
substantial performance increase (vs. using pthreads).

This also avoids the need to allocate an extra structure in memory when
using GMutex or GCond: we can use the structure directly.

The main reason for the increase in performance is that our
implementation can be made more simple: we don't need to support the
array of options on pthread_mutex_t (which includes the possibility, for
example, of being recursive).

The result is a ~30% improvement in uncontended cases and a much larger
increase (3 to 4 times) in contended cases for a simple testcase.
Comment 10 Allison Karlitskaya (desrt) 2014-07-06 17:14:54 UTC
Created attachment 280005 [details] [review]
GMutex (linux): detect and report some errors

Detect the following two errors:

 - attempting to unlock a mutex that is not locked

 - attempting to clear a mutex that was not initialised or was
   initialised but is still locked

Both of these are fatal errors.  We avoid using g_error() here because
doing so would involve calls back into the GMutex code, and if things
are going off the rails then we want to avoid that.
Comment 11 Allison Karlitskaya (desrt) 2014-07-06 17:14:58 UTC
Created attachment 280006 [details] [review]
GCond (linux): fix g_cond_wait_until() return value on timeout

It should return FALSE on timeout (and only on timeout), and
TRUE otherwise.
Comment 12 Matthias Clasen 2014-07-09 03:05:15 UTC
Review of attachment 280004 [details] [review]:

ok.
Comment 13 Matthias Clasen 2014-07-09 03:06:52 UTC
Review of attachment 280005 [details] [review]:

ok
Comment 14 Matthias Clasen 2014-07-09 03:07:21 UTC
Review of attachment 280006 [details] [review]:

ok
Comment 15 Allison Karlitskaya (desrt) 2014-07-09 14:59:14 UTC
Attachment 280004 [details] pushed as 49b59e5 - GLib: implement GMutex natively on Linux
Attachment 280005 [details] pushed as ecf1359 - GMutex (linux): detect and report some errors
Attachment 280006 [details] pushed as 636cd00 - GCond (linux): fix g_cond_wait_until() return value on timeout
Comment 16 Ilya Konstantinov 2015-06-19 14:25:22 UTC
Does this mean that GRWLock is now potentially less efficient than GMutex on Linux? Can similar gains be made in GRWLock?