drd: Add a consistency check
[valgrind.git] / coregrind / m_scheduler / ticket-lock-linux.c
blobe94a6d4028c4904e30b8cd723e293b0237145c15
2 /*--------------------------------------------------------------------*/
3 /*--- Linux ticket lock implementation ticket-lock-linux.c ---*/
4 /*--- ---*/
5 /*--- Guarantees fair scheduling even if multiple threads are ---*/
6 /*--- runnable at the same time on a multicore system. Has been ---*/
7 /*--- observed to cause a slow-down compared to the generic ---*/
8 /*--- scheduler lock with CPU frequency scaling enabled. Makes ---*/
9 /*--- Valgrind slightly faster if CPU frequency scaling has been ---*/
10 /*--- disabled. See also http://bugs.kde.org/show_bug.cgi?id=270006---*/
11 /*--------------------------------------------------------------------*/
14 This file is part of Valgrind, a dynamic binary instrumentation
15 framework.
17 Copyright (C) 2011-2013 Bart Van Assche <bvanassche@acm.org>.
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of the
22 License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
32 02111-1307, USA.
34 The GNU General Public License is contained in the file COPYING.
37 #include "pub_core_basics.h"
38 #include "pub_core_libcassert.h"
39 #include "pub_core_libcbase.h" // VG_(memset)()
40 #include "pub_core_libcprint.h"
41 #include "pub_core_syscall.h"
42 #include "pub_core_vki.h"
43 #include "pub_core_vkiscnums.h" // __NR_futex
44 #include "pub_core_libcproc.h"
45 #include "pub_core_mallocfree.h"
46 #include "pub_core_threadstate.h"
47 #include "pub_core_inner.h"
48 #if defined(ENABLE_INNER_CLIENT_REQUEST)
49 #include "helgrind/helgrind.h"
50 #endif
51 #include "priv_sched-lock.h"
52 #include "priv_sched-lock-impl.h"
54 #define TL_FUTEX_COUNT_LOG2 4
55 #define TL_FUTEX_COUNT (1U << TL_FUTEX_COUNT_LOG2)
56 #define TL_FUTEX_MASK (TL_FUTEX_COUNT - 1)
58 struct sched_lock {
59 volatile unsigned head;
60 volatile unsigned tail;
61 volatile unsigned futex[TL_FUTEX_COUNT];
62 int owner;
65 #if 1
66 static Bool s_debug;
67 #else
68 static Bool s_debug = True;
69 #endif
71 static const HChar *get_sched_lock_name(void)
73 return "ticket lock";
76 static struct sched_lock *create_sched_lock(void)
78 struct sched_lock *p;
80 p = VG_(malloc)("sched_lock", sizeof(*p));
82 // The futex syscall requires that a futex takes four bytes.
83 vg_assert(sizeof(p->futex[0]) == 4);
85 VG_(memset)(p, 0, sizeof(*p));
87 INNER_REQUEST(ANNOTATE_RWLOCK_CREATE(p));
88 INNER_REQUEST(ANNOTATE_BENIGN_RACE_SIZED(&p->futex, sizeof(p->futex), ""));
89 return p;
92 static void destroy_sched_lock(struct sched_lock *p)
94 INNER_REQUEST(ANNOTATE_RWLOCK_DESTROY(p));
95 VG_(free)(p);
98 static int get_sched_lock_owner(struct sched_lock *p)
100 return p->owner;
104 * Acquire ticket lock. Increment the tail of the queue and use the original
105 * value as the ticket value. Wait until the head of the queue equals the
106 * ticket value. The futex used to wait depends on the ticket value in order
107 * to avoid that all threads get woken up every time a ticket lock is
108 * released. That last effect is sometimes called the "thundering herd"
109 * effect.
111 * See also Nick Piggin, x86: FIFO ticket spinlocks, Linux kernel mailing list
112 * (http://lkml.org/lkml/2007/11/1/125) for more info.
114 static void acquire_sched_lock(struct sched_lock *p)
116 unsigned ticket, futex_value;
117 volatile unsigned *futex;
118 SysRes sres;
120 ticket = __sync_fetch_and_add(&p->tail, 1);
121 futex = &p->futex[ticket & TL_FUTEX_MASK];
122 if (s_debug)
123 VG_(printf)("[%d/%d] acquire: ticket %d\n", VG_(getpid)(),
124 VG_(gettid)(), ticket);
125 for (;;) {
126 futex_value = *futex;
127 __sync_synchronize();
128 if (ticket == p->head)
129 break;
130 if (s_debug)
131 VG_(printf)("[%d/%d] acquire: ticket %d - waiting until"
132 " futex[%ld] != %d\n", VG_(getpid)(),
133 VG_(gettid)(), ticket, (long)(futex - p->futex),
134 futex_value);
135 sres = VG_(do_syscall3)(__NR_futex, (UWord)futex,
136 VKI_FUTEX_WAIT | VKI_FUTEX_PRIVATE_FLAG,
137 futex_value);
138 if (sr_isError(sres) && sres._val != VKI_EAGAIN) {
139 VG_(printf)("futex_wait() returned error code %ld\n", sres._val);
140 vg_assert(False);
143 __sync_synchronize();
144 INNER_REQUEST(ANNOTATE_RWLOCK_ACQUIRED(p, /*is_w*/1));
145 vg_assert(p->owner == 0);
146 p->owner = VG_(gettid)();
150 * Release a ticket lock by incrementing the head of the queue. Only generate
151 * a thread wakeup signal if at least one thread is waiting. If the queue tail
152 * matches the wakeup_ticket value, no threads have to be woken up.
154 * Note: tail will only be read after head has been incremented since both are
155 * declared as volatile and since the __sync...() functions imply a memory
156 * barrier.
158 static void release_sched_lock(struct sched_lock *p)
160 unsigned wakeup_ticket, futex_value;
161 volatile unsigned *futex;
162 SysRes sres;
164 vg_assert(p->owner != 0);
165 p->owner = 0;
166 INNER_REQUEST(ANNOTATE_RWLOCK_RELEASED(p, /*is_w*/1));
167 wakeup_ticket = __sync_fetch_and_add(&p->head, 1) + 1;
168 if (p->tail != wakeup_ticket) {
169 futex = &p->futex[wakeup_ticket & TL_FUTEX_MASK];
170 futex_value = __sync_fetch_and_add(futex, 1);
171 if (s_debug)
172 VG_(printf)("[%d/%d] release: waking up ticket %d (futex[%ld] = %d)"
173 "\n", VG_(getpid)(), VG_(gettid)(), wakeup_ticket,
174 (long)(futex - p->futex), futex_value);
175 sres = VG_(do_syscall3)(__NR_futex, (UWord)futex,
176 VKI_FUTEX_WAKE | VKI_FUTEX_PRIVATE_FLAG,
177 0x7fffffff);
178 vg_assert(!sr_isError(sres));
179 } else {
180 if (s_debug)
181 VG_(printf)("[%d/%d] release: no thread is waiting for ticket %d\n",
182 VG_(getpid)(), VG_(gettid)(), wakeup_ticket);
186 const struct sched_lock_ops ML_(linux_ticket_lock_ops) = {
187 .get_sched_lock_name = get_sched_lock_name,
188 .create_sched_lock = create_sched_lock,
189 .destroy_sched_lock = destroy_sched_lock,
190 .get_sched_lock_owner = get_sched_lock_owner,
191 .acquire_sched_lock = acquire_sched_lock,
192 .release_sched_lock = release_sched_lock,