1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Copyright (C) 2012 Intel Corporation
5
** Contact: http://www.qt-project.org/legal
7
** This file is part of the QtCore module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** Commercial License Usage
11
** Licensees holding valid commercial Qt licenses may use this file in
12
** accordance with the commercial license agreement provided with the
13
** Software or, alternatively, in accordance with the terms contained in
14
** a written agreement between you and Digia. For licensing terms and
15
** conditions see http://qt.digia.com/licensing. For further information
16
** use the contact form at http://qt.digia.com/contact-us.
18
** GNU Lesser General Public License Usage
19
** Alternatively, this file may be used under the terms of the GNU Lesser
20
** General Public License version 2.1 as published by the Free Software
21
** Foundation and appearing in the file LICENSE.LGPL included in the
22
** packaging of this file. Please review the following information to
23
** ensure the GNU Lesser General Public License version 2.1 requirements
24
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26
** In addition, as a special exception, Digia gives you certain additional
27
** rights. These rights are described in the Digia Qt LGPL Exception
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30
** GNU General Public License Usage
31
** Alternatively, this file may be used under the terms of the GNU
32
** General Public License version 3.0 as published by the Free Software
33
** Foundation and appearing in the file LICENSE.GPL included in the
34
** packaging of this file. Please review the following information to
35
** ensure the GNU General Public License version 3.0 requirements will be
36
** met: http://www.gnu.org/copyleft/gpl.html.
41
****************************************************************************/
43
#include "qplatformdefs.h"
49
#include "qelapsedtimer.h"
51
#include <linux/futex.h>
52
#include <sys/syscall.h>
55
#include <asm/unistd.h>
57
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
59
# include <type_traits>
61
static void checkElapsedTimerIsTrivial()
63
Q_STATIC_ASSERT(std::has_trivial_default_constructor<QT_PREPEND_NAMESPACE(QElapsedTimer)>::value);
67
static void checkElapsedTimerIsTrivial()
72
#ifndef QT_LINUX_FUTEX
73
# error "Qt build is broken: qmutex_linux.cpp is being built but futex support is not wanted"
79
* QBasicMutex implementation on Linux with futexes
81
* QBasicMutex contains one pointer value, which can contain one of four
83
* 0x0 unlocked, non-recursive mutex
84
* 0x1 locked non-recursive mutex, no waiters
85
* 0x3 locked non-recursive mutex, at least one waiter
86
* > 0x3 recursive mutex, points to a QMutexPrivate object
88
* LOCKING (non-recursive):
90
* A non-recursive mutex starts in the 0x0 state, indicating that it's
91
* unlocked. When the first thread attempts to lock it, it will perform a
92
* testAndSetAcquire from 0x0 to 0x1. If that succeeds, the caller concludes
93
* that it successfully locked the mutex. That happens in fastTryLock().
95
* If that testAndSetAcquire fails, QBasicMutex::lockInternal is called.
97
* lockInternal will examine the value of the pointer. Otherwise, it will use
98
* futexes to sleep and wait for another thread to unlock. To do that, it needs
99
* to set a pointer value of 0x3, which indicates that thread is waiting. It
100
* does that by a simple fetchAndStoreAcquire operation.
102
* If the pointer value was 0x0, it means we succeeded in acquiring the mutex.
103
* For other values, it will then call FUTEX_WAIT and with an expected value of
106
* If the pointer value changed before futex(2) managed to sleep, it will
107
* return -1 / EWOULDBLOCK, in which case we have to start over. And even if we
108
* are woken up directly by a FUTEX_WAKE, we need to acquire the mutex, so we
111
* UNLOCKING (non-recursive):
113
* To unlock, we need to set a value of 0x0 to indicate it's unlocked. The
114
* first attempt is a testAndSetRelease operation from 0x1 to 0x0. If that
115
* succeeds, we're done.
117
* If it fails, unlockInternal() is called. The only possibility is that the
118
* mutex value was 0x3, which indicates some other thread is waiting or was
119
* waiting in the past. We then set the mutex to 0x0 and perform a FUTEX_WAKE.
122
static QBasicAtomicInt futexFlagSupport = Q_BASIC_ATOMIC_INITIALIZER(-1);
124
static int checkFutexPrivateSupport()
127
#if defined(FUTEX_PRIVATE_FLAG)
128
// check if the kernel supports extra futex flags
129
// FUTEX_PRIVATE_FLAG appeared in v2.6.22
130
Q_STATIC_ASSERT(FUTEX_PRIVATE_FLAG != 0x80000000);
132
// try an operation that has no side-effects: wake up 42 threads
133
// futex will return -1 (errno==ENOSYS) if the flag isn't supported
134
// there should be no other error conditions
135
value = syscall(__NR_futex, &futexFlagSupport,
136
FUTEX_WAKE | FUTEX_PRIVATE_FLAG,
139
value = FUTEX_PRIVATE_FLAG;
146
futexFlagSupport.store(value);
150
static inline int futexFlags()
152
int value = futexFlagSupport.load();
153
if (Q_LIKELY(value != -1))
155
return checkFutexPrivateSupport();
158
static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW
160
volatile int *int_addr = reinterpret_cast<volatile int *>(addr);
161
#if Q_BYTE_ORDER == Q_BIG_ENDIAN && QT_POINTER_SIZE == 8
162
int_addr++; //We want a pointer to the 32 least significant bit of QMutex::d
167
// we use __NR_futex because some libcs (like Android's bionic) don't
168
// provide SYS_futex etc.
169
return syscall(__NR_futex, int_addr, op | futexFlags(), val, timeout, addr2, val2);
172
static inline QMutexData *dummyFutexValue()
174
return reinterpret_cast<QMutexData *>(quintptr(3));
177
template <bool IsTimed> static inline
178
bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1) Q_DECL_NOTHROW
183
// we're here because fastTryLock() has just failed
187
struct timespec ts, *pts = 0;
188
QElapsedTimer elapsedTimer;
189
checkElapsedTimerIsTrivial();
190
if (IsTimed && timeout > 0) {
191
ts.tv_sec = timeout / 1000;
192
ts.tv_nsec = (timeout % 1000) * 1000 * 1000;
193
elapsedTimer.start();
196
// the mutex is locked already, set a bit indicating we're waiting
197
while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) != 0) {
198
if (IsTimed && pts == &ts) {
199
// recalculate the timeout
200
qint64 xtimeout = qint64(timeout) * 1000 * 1000;
201
xtimeout -= elapsedTimer.nsecsElapsed();
203
// timer expired after we returned
206
ts.tv_sec = xtimeout / Q_INT64_C(1000) / 1000 / 1000;
207
ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000);
209
if (IsTimed && timeout > 0)
212
// successfully set the waiting bit, now sleep
213
int r = _q_futex(&d_ptr, FUTEX_WAIT, quintptr(dummyFutexValue()), pts);
214
if (IsTimed && r != 0 && errno == ETIMEDOUT)
217
// we got woken up, so try to acquire the mutex
218
// note we must set to dummyFutexValue because there could be other threads
222
Q_ASSERT(d_ptr.load());
226
void QBasicMutex::lockInternal() Q_DECL_NOTHROW
228
Q_ASSERT(!isRecursive());
229
lockInternal_helper<false>(d_ptr);
232
bool QBasicMutex::lockInternal(int timeout) Q_DECL_NOTHROW
234
Q_ASSERT(!isRecursive());
235
return lockInternal_helper<true>(d_ptr, timeout);
238
void QBasicMutex::unlockInternal() Q_DECL_NOTHROW
240
QMutexData *d = d_ptr.load();
241
Q_ASSERT(d); //we must be locked
242
Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed
244
Q_ASSERT(!isRecursive());
246
d_ptr.storeRelease(0);
247
_q_futex(&d_ptr, FUTEX_WAKE, 1, 0);
253
#endif // QT_NO_THREAD