Bug 548967 - 1 bit mutex lock
[platform/upstream/glib.git] / glib / gbitlock.c
1 /*
2  * Copyright © 2008 Ryan Lortie
3  * Copyright © 2010 Codethink Limited
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the licence, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 #include "gbitlock.h"
24
25 #include <glib/gatomic.h>
26 #include <glib/gslist.h>
27 #include <glib/gthread.h>
28
29 #include "gthreadprivate.h"
30 #include "config.h"
31
32 #include "galias.h"
33
34 #ifndef HAVE_FUTEX
35 static GSList *g_futex_address_list = NULL;
36 static GMutex *g_futex_mutex = NULL;
37 #endif
38
39 void
40 _g_futex_thread_init (void) {
41 #ifndef HAVE_FUTEX
42   g_futex_mutex = g_mutex_new ();
43 #endif
44 }
45
46 #ifdef HAVE_FUTEX
47 /*
48  * We have headers for futex(2) on the build machine.  This does not
49  * imply that every system that ever runs the resulting glib will have
50  * kernel support for futex, but you'd have to have a pretty old
51  * kernel in order for that not to be the case.
52  *
53  * If anyone actually gets bit by this, please file a bug. :)
54  */
55 #include <linux/futex.h>
56 #include <syscall.h>
57 #include <unistd.h>
58
59 /* < private >
60  * g_futex_wait:
61  * @address: a pointer to an integer
62  * @value: the value that should be at @address
63  *
64  * Atomically checks that the value stored at @address is equal to
65  * @value and then blocks.  If the value stored at @address is not
66  * equal to @value then this function returns immediately.
67  *
68  * To unblock, call g_futex_wake() on @address.
69  *
70  * This call may spuriously unblock (for example, in response to the
71  * process receiving a signal) but this is not guaranteed.  Unlike the
72  * Linux system call of a similar name, there is no guarantee that a
73  * waiting process will unblock due to a g_futex_wake() call in a
74  * separate process.
75  */
76 static void
77 g_futex_wait (const volatile gint *address,
78               gint                 value)
79 {
80   syscall (SYS_futex, address, (gsize) FUTEX_WAIT, (gsize) value, NULL);
81 }
82
83 /* < private >
84  * g_futex_wake:
85  * @address: a pointer to an integer
86  *
87  * Nominally, wakes one thread that is blocked in g_futex_wait() on
88  * @address (if any thread is currently waiting).
89  *
90  * As mentioned in the documention for g_futex_wait(), spurious
91  * wakeups may occur.  As such, this call may result in more than one
92  * thread being woken up.
93  */
94 static void
95 g_futex_wake (const volatile gint *address)
96 {
97   syscall (SYS_futex, address, (gsize) FUTEX_WAKE, (gsize) 1, NULL);
98 }
99
100 #else
101
102 /* emulate futex(2) */
103 typedef struct
104 {
105   const volatile gint *address;
106   gint                 ref_count;
107   GCond               *wait_queue;
108 } WaitAddress;
109
110 static GSList *g_futex_address_list;
111 static GMutex *g_futex_mutex;
112
113 static WaitAddress *
114 g_futex_find_address (const volatile gint *address)
115 {
116   GSList *node;
117
118   for (node = g_futex_address_list; node; node = node->next)
119     {
120       WaitAddress *waiter = node->data;
121
122       if (waiter->address == address)
123         return waiter;
124     }
125
126   return NULL;
127 }
128
129 static void
130 g_futex_wait (const volatile gint *address,
131               gint                 value)
132 {
133   g_mutex_lock (g_futex_mutex);
134   if G_LIKELY (g_atomic_int_get (address) == value)
135     {
136       WaitAddress *waiter;
137
138       if ((waiter = g_futex_find_address (address)) == NULL)
139         {
140           waiter = g_slice_new (WaitAddress);
141           waiter->address = address;
142           waiter->wait_queue = g_cond_new ();
143           waiter->ref_count = 0;
144           g_futex_address_list =
145             g_slist_prepend (g_futex_address_list, waiter);
146         }
147
148       waiter->ref_count++;
149       g_cond_wait (waiter->wait_queue, g_futex_mutex);
150
151       if (!--waiter->ref_count)
152         {
153           g_futex_address_list =
154             g_slist_remove (g_futex_address_list, waiter);
155           g_cond_free (waiter->wait_queue);
156           g_slice_free (WaitAddress, waiter);
157         }
158     }
159   g_mutex_unlock (g_futex_mutex);
160 }
161
162 static void
163 g_futex_wake (const volatile gint *address)
164 {
165   WaitAddress *waiter;
166
167   /* need to lock here for two reasons:
168    *   1) need to acquire/release lock to ensure waiter is not in
169    *      the process of registering a wait
170    *   2) need to -stay- locked until the end to ensure a wake()
171    *      in another thread doesn't cause 'waiter' to stop existing
172    */
173   g_mutex_lock (g_futex_mutex);
174   if ((waiter = g_futex_find_address (address)))
175     g_cond_signal (waiter->wait_queue);
176   g_mutex_unlock (g_futex_mutex);
177 }
178 #endif
179
180 #define CONTENTION_CLASSES 11
181 static volatile gint g_bit_lock_contended[CONTENTION_CLASSES];
182
183 /**
184  * g_bit_lock:
185  * @address: a pointer to an integer
186  * @lock_bit: a bit value between 0 and 31
187  *
188  * Sets the indicated @lock_bit in @address.  If the bit is already
189  * set, this call will block until g_bit_unlock() unsets the
190  * corresponding bit.
191  *
192  * Attempting to lock on two different bits within the same integer is
193  * not supported and will very probably cause deadlocks.
194  *
195  * The value of the bit that is set is (1u << @bit).  If @bit is not
196  * between 0 and 31 then the result is undefined.
197  *
198  * This function accesses @address atomically.  All other accesses to
199  * @address must be atomic in order for this function to work
200  * reliably.
201  **/
202 void
203 g_bit_lock (volatile gint *address,
204             gint           lock_bit)
205 {
206   guint v;
207
208  retry:
209   v = g_atomic_int_get (address);
210   if (v & (1u << lock_bit))
211     /* already locked */
212     {
213       guint class = ((gsize) address) % G_N_ELEMENTS (g_bit_lock_contended);
214
215       g_atomic_int_add (&g_bit_lock_contended[class], +1);
216       g_futex_wait (address, v);
217       g_atomic_int_add (&g_bit_lock_contended[class], -1);
218
219       goto retry;
220     }
221
222   if (!g_atomic_int_compare_and_exchange (address, v, v | (1u << lock_bit)))
223     goto retry;
224 }
225
226 /**
227  * g_bit_trylock:
228  * @address: a pointer to an integer
229  * @lock_bit: a bit value between 0 and 31
230  *
231  * Sets the indicated @lock_bit in @address, returning %TRUE if
232  * successful.  If the bit is already set, returns %FALSE immediately.
233  *
234  * Attempting to lock on two different bits within the same integer is
235  * not supported.
236  *
237  * The value of the bit that is set is (1u << @bit).  If @bit is not
238  * between 0 and 31 then the result is undefined.
239  *
240  * This function accesses @address atomically.  All other accesses to
241  * @address must be atomic in order for this function to work
242  * reliably.
243  **/
244 gboolean
245 g_bit_trylock (volatile gint *address,
246                gint           lock_bit)
247 {
248   guint v;
249
250  retry:
251   v = g_atomic_int_get (address);
252   if (v & (1u << lock_bit))
253     /* already locked */
254     return FALSE;
255
256   if (!g_atomic_int_compare_and_exchange (address, v, v | (1u << lock_bit)))
257     goto retry;
258
259   return TRUE;
260 }
261
262 /**
263  * g_bit_unlock:
264  * @address: a pointer to an integer
265  * @lock_bit: a bit value between 0 and 31
266  *
267  * Clears the indicated @lock_bit in @address.  If another thread is
268  * currently blocked in g_bit_lock() on this same bit then it will be
269  * woken up.
270  *
271  * This function accesses @address atomically.  All other accesses to
272  * @address must be atomic in order for this function to work
273  * reliably.
274  **/
275 void
276 g_bit_unlock (volatile gint *address,
277               gint           lock_bit)
278 {
279   guint class = ((gsize) address) % G_N_ELEMENTS (g_bit_lock_contended);
280   guint v;
281
282  retry:
283   v = g_atomic_int_get (address);
284   if (!g_atomic_int_compare_and_exchange (address, v, v & ~(1u << lock_bit)))
285     goto retry;
286
287   if (g_atomic_int_get (&g_bit_lock_contended[class]))
288     g_futex_wake (address);
289 }
290
291 #define __G_BITLOCK_C__
292 #include "galiasdef.c"