summaryrefslogtreecommitdiffstats
path: root/package/libglib2/libglib2-0003-implement-GMutex-natively-on-Linux.patch
blob: acfebadbaeed991c4bc1da83aa2dce3aecfbfea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
From 49b59e5ac4428a6a99a85d699c3662f96efc4e9d Mon Sep 17 00:00:00 2001
From: Ryan Lortie <desrt@desrt.ca>
Date: Tue, 10 Jun 2014 08:28:32 -0400
Subject: [PATCH] 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.

https://bugzilla.gnome.org/show_bug.cgi?id=731986
---
 glib/gthread-posix.c | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 207 insertions(+), 1 deletion(-)

diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index 6f5a606..f7d5d8a 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -66,6 +66,11 @@
 #include <windows.h>
 #endif
 
+/* clang defines __ATOMIC_SEQ_CST but doesn't support the GCC extension */
+#if defined(HAVE_FUTEX) && defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
+#define USE_NATIVE_MUTEX
+#endif
+
 static void
 g_thread_abort (gint         status,
                 const gchar *function)
@@ -77,6 +82,8 @@ g_thread_abort (gint         status,
 
 /* {{{1 GMutex */
 
+#if !defined(USE_NATIVE_MUTEX)
+
 static pthread_mutex_t *
 g_mutex_impl_new (void)
 {
@@ -258,6 +265,8 @@ g_mutex_trylock (GMutex *mutex)
   return FALSE;
 }
 
+#endif /* !defined(USE_NATIVE_MUTEX) */
+
 /* {{{1 GRecMutex */
 
 static pthread_mutex_t *
@@ -631,6 +640,8 @@ g_rw_lock_reader_unlock (GRWLock *rw_lock)
 
 /* {{{1 GCond */
 
+#if !defined(USE_NATIVE_MUTEX)
+
 static pthread_cond_t *
 g_cond_impl_new (void)
 {
@@ -902,6 +913,8 @@ g_cond_wait_until (GCond  *cond,
   return FALSE;
 }
 
+#endif /* defined(USE_NATIVE_MUTEX) */
+
 /* {{{1 GPrivate */
 
 /**
@@ -1219,5 +1232,198 @@ g_system_thread_set_name (const gchar *name)
 #endif
 }
 
-/* {{{1 Epilogue */
+/* {{{1 GMutex and GCond futex implementation */
+
+#if defined(USE_NATIVE_MUTEX)
+
+#include <linux/futex.h>
+#include <sys/syscall.h>
+
+/* We should expand the set of operations available in gatomic once we
+ * have better C11 support in GCC in common distributions (ie: 4.9).
+ *
+ * Before then, let's define a couple of useful things for our own
+ * purposes...
+ */
+
+#define exchange_acquire(ptr, new) \
+  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
+#define compare_exchange_acquire(ptr, old, new) \
+  __atomic_compare_exchange_4((ptr), (old), (new), 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
+
+#define exchange_release(ptr, new) \
+  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
+#define store_release(ptr, new) \
+  __atomic_store_4((ptr), (new), __ATOMIC_RELEASE)
+
+/* Our strategy for the mutex is pretty simple:
+ *
+ *  0: not in use
+ *
+ *  1: acquired by one thread only, no contention
+ *
+ *  > 1: contended
+ *
+ *
+ * As such, attempting to acquire the lock should involve an increment.
+ * If we find that the previous value was 0 then we can return
+ * immediately.
+ *
+ * On unlock, we always store 0 to indicate that the lock is available.
+ * If the value there was 1 before then we didn't have contention and
+ * can return immediately.  If the value was something other than 1 then
+ * we have the contended case and need to wake a waiter.
+ *
+ * If it was not 0 then there is another thread holding it and we must
+ * wait.  We must always ensure that we mark a value >1 while we are
+ * waiting in order to instruct the holder to do a wake operation on
+ * unlock.
+ */
+
+void
+g_mutex_init (GMutex *mutex)
+{
+  mutex->i[0] = 0;
+}
+
+void
+g_mutex_clear (GMutex *mutex)
+{
+}
+
+static void __attribute__((noinline))
+g_mutex_lock_slowpath (GMutex *mutex)
+{
+  /* Set to 2 to indicate contention.  If it was zero before then we
+   * just acquired the lock.
+   *
+   * Otherwise, sleep for as long as the 2 remains...
+   */
+  while (exchange_acquire (&mutex->i[0], 2) != 0)
+    syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAIT, (gsize) 2, NULL);
+}
+
+static void __attribute__((noinline))
+g_mutex_unlock_slowpath (GMutex *mutex)
+{
+  /* We seem to get better code for the uncontended case by splitting
+   * out this call...
+   */
+  syscall (__NR_futex, &mutex->i[0], (gsize) FUTEX_WAKE, (gsize) 1, NULL);
+}
+
+void
+g_mutex_lock (GMutex *mutex)
+{
+  /* 0 -> 1 and we're done.  Anything else, and we need to wait... */
+  if G_UNLIKELY (g_atomic_int_add (&mutex->i[0], 1) != 0)
+    g_mutex_lock_slowpath (mutex);
+}
+
+void
+g_mutex_unlock (GMutex *mutex)
+{
+  /* 1-> 0 and we're done.  Anything else and we need to signal... */
+  if G_UNLIKELY (exchange_release (&mutex->i[0], 0) != 1)
+    g_mutex_unlock_slowpath (mutex);
+}
+
+gboolean
+g_mutex_trylock (GMutex *mutex)
+{
+  guint zero = 0;
+
+  /* We don't want to touch the value at all unless we can move it from
+   * exactly 0 to 1.
+   */
+  return compare_exchange_acquire (&mutex->i[0], &zero, 1);
+}
+
+/* Condition variables are implemented in a rather simple way as well.
+ * In many ways, futex() as an abstraction is even more ideally suited
+ * to condition variables than it is to mutexes.
+ *
+ * We store a generation counter.  We sample it with the lock held and
+ * unlock before sleeping on the futex.
+ *
+ * Signalling simply involves increasing the counter and making the
+ * appropriate futex call.
+ *
+ * The only thing that is the slightest bit complicated is timed waits
+ * because we must convert our absolute time to relative.
+ */
+
+void
+g_cond_init (GCond *cond)
+{
+  cond->i[0] = 0;
+}
+
+void
+g_cond_clear (GCond *cond)
+{
+}
+
+void
+g_cond_wait (GCond  *cond,
+             GMutex *mutex)
+{
+  guint sampled = g_atomic_int_get (&cond->i[0]);
+
+  g_mutex_unlock (mutex);
+  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT, (gsize) sampled, NULL);
+  g_mutex_lock (mutex);
+}
+
+void
+g_cond_signal (GCond *cond)
+{
+  g_atomic_int_inc (&cond->i[0]);
+
+  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE, (gsize) 1, NULL);
+}
+
+void
+g_cond_broadcast (GCond *cond)
+{
+  g_atomic_int_inc (&cond->i[0]);
+
+  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAKE, (gsize) INT_MAX, NULL);
+}
+
+gboolean
+g_cond_wait_until (GCond  *cond,
+                   GMutex *mutex,
+                   gint64  end_time)
+{
+  struct timespec now;
+  struct timespec span;
+  guint sampled;
+
+  if (end_time < 0)
+    return FALSE;
+
+  clock_gettime (CLOCK_MONOTONIC, &now);
+  span.tv_sec = (end_time / 1000000) - now.tv_sec;
+  span.tv_nsec = ((end_time % 1000000) * 1000) - now.tv_nsec;
+  if (span.tv_nsec < 0)
+    {
+      span.tv_nsec += 1000000000;
+      span.tv_sec--;
+    }
+
+  if (span.tv_sec < 0)
+    return FALSE;
+
+  sampled = cond->i[0];
+  g_mutex_unlock (mutex);
+  syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT, (gsize) sampled, &span);
+  g_mutex_lock (mutex);
+
+  return TRUE;
+}
+
+#endif
+
+  /* {{{1 Epilogue */
 /* vim:set foldmethod=marker: */
-- 
1.8.1.4