2 /*--------------------------------------------------------------------*/
3 /*--- Linux ticket lock implementation ticket-lock-linux.c ---*/
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
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
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"
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)
59 volatile unsigned head
;
60 volatile unsigned tail
;
61 volatile unsigned futex
[TL_FUTEX_COUNT
];
68 static Bool s_debug
= True
;
71 static const HChar
*get_sched_lock_name(void)
76 static struct sched_lock
*create_sched_lock(void)
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
), ""));
92 static void destroy_sched_lock(struct sched_lock
*p
)
94 INNER_REQUEST(ANNOTATE_RWLOCK_DESTROY(p
));
98 static int get_sched_lock_owner(struct sched_lock
*p
)
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"
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
;
120 ticket
= __sync_fetch_and_add(&p
->tail
, 1);
121 futex
= &p
->futex
[ticket
& TL_FUTEX_MASK
];
123 VG_(printf
)("[%d/%d] acquire: ticket %d\n", VG_(getpid
)(),
124 VG_(gettid
)(), ticket
);
126 futex_value
= *futex
;
127 __sync_synchronize();
128 if (ticket
== p
->head
)
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
),
135 sres
= VG_(do_syscall3
)(__NR_futex
, (UWord
)futex
,
136 VKI_FUTEX_WAIT
| VKI_FUTEX_PRIVATE_FLAG
,
138 if (sr_isError(sres
) && sres
._val
!= VKI_EAGAIN
) {
139 VG_(printf
)("futex_wait() returned error code %ld\n", sres
._val
);
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
158 static void release_sched_lock(struct sched_lock
*p
)
160 unsigned wakeup_ticket
, futex_value
;
161 volatile unsigned *futex
;
164 vg_assert(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);
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
,
178 vg_assert(!sr_isError(sres
));
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
,