~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/thread/qmutex_linux.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
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
 
6
**
 
7
** This file is part of the QtCore module of the Qt Toolkit.
 
8
**
 
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.
 
17
**
 
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.
 
25
**
 
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.
 
29
**
 
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.
 
37
**
 
38
**
 
39
** $QT_END_LICENSE$
 
40
**
 
41
****************************************************************************/
 
42
 
 
43
#include "qplatformdefs.h"
 
44
#include "qmutex.h"
 
45
 
 
46
#ifndef QT_NO_THREAD
 
47
#include "qatomic.h"
 
48
#include "qmutex_p.h"
 
49
#include "qelapsedtimer.h"
 
50
 
 
51
#include <linux/futex.h>
 
52
#include <sys/syscall.h>
 
53
#include <unistd.h>
 
54
#include <errno.h>
 
55
#include <asm/unistd.h>
 
56
 
 
57
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
 
58
// C++11 mode
 
59
#  include <type_traits>
 
60
 
 
61
static void checkElapsedTimerIsTrivial()
 
62
{
 
63
    Q_STATIC_ASSERT(std::has_trivial_default_constructor<QT_PREPEND_NAMESPACE(QElapsedTimer)>::value);
 
64
}
 
65
 
 
66
#else
 
67
static void checkElapsedTimerIsTrivial()
 
68
{
 
69
}
 
70
#endif
 
71
 
 
72
#ifndef QT_LINUX_FUTEX
 
73
# error "Qt build is broken: qmutex_linux.cpp is being built but futex support is not wanted"
 
74
#endif
 
75
 
 
76
QT_BEGIN_NAMESPACE
 
77
 
 
78
/*
 
79
 * QBasicMutex implementation on Linux with futexes
 
80
 *
 
81
 * QBasicMutex contains one pointer value, which can contain one of four
 
82
 * different values:
 
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
 
87
 *
 
88
 * LOCKING (non-recursive):
 
89
 *
 
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().
 
94
 *
 
95
 * If that testAndSetAcquire fails, QBasicMutex::lockInternal is called.
 
96
 *
 
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.
 
101
 *
 
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
 
104
 * 0x3.
 
105
 *
 
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
 
109
 * start over again.
 
110
 *
 
111
 * UNLOCKING (non-recursive):
 
112
 *
 
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.
 
116
 *
 
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.
 
120
 */
 
121
 
 
122
static QBasicAtomicInt futexFlagSupport = Q_BASIC_ATOMIC_INITIALIZER(-1);
 
123
 
 
124
static int checkFutexPrivateSupport()
 
125
{
 
126
    int value = 0;
 
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);
 
131
 
 
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,
 
137
                    42, 0, 0, 0);
 
138
    if (value != -1)
 
139
        value = FUTEX_PRIVATE_FLAG;
 
140
    else
 
141
        value = 0;
 
142
 
 
143
#else
 
144
    value = 0;
 
145
#endif
 
146
    futexFlagSupport.store(value);
 
147
    return value;
 
148
}
 
149
 
 
150
static inline int futexFlags()
 
151
{
 
152
    int value = futexFlagSupport.load();
 
153
    if (Q_LIKELY(value != -1))
 
154
        return value;
 
155
    return checkFutexPrivateSupport();
 
156
}
 
157
 
 
158
static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW
 
159
{
 
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
 
163
#endif
 
164
    int *addr2 = 0;
 
165
    int val2 = 0;
 
166
 
 
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);
 
170
}
 
171
 
 
172
static inline QMutexData *dummyFutexValue()
 
173
{
 
174
    return reinterpret_cast<QMutexData *>(quintptr(3));
 
175
}
 
176
 
 
177
template <bool IsTimed> static inline
 
178
bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1) Q_DECL_NOTHROW
 
179
{
 
180
    if (!IsTimed)
 
181
        timeout = -1;
 
182
 
 
183
    // we're here because fastTryLock() has just failed
 
184
    if (timeout == 0)
 
185
        return false;
 
186
 
 
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();
 
194
    }
 
195
 
 
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();
 
202
            if (xtimeout <= 0) {
 
203
                // timer expired after we returned
 
204
                return false;
 
205
            }
 
206
            ts.tv_sec = xtimeout / Q_INT64_C(1000) / 1000 / 1000;
 
207
            ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000);
 
208
        }
 
209
        if (IsTimed && timeout > 0)
 
210
            pts = &ts;
 
211
 
 
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)
 
215
            return false;
 
216
 
 
217
        // we got woken up, so try to acquire the mutex
 
218
        // note we must set to dummyFutexValue because there could be other threads
 
219
        // also waiting
 
220
    }
 
221
 
 
222
    Q_ASSERT(d_ptr.load());
 
223
    return true;
 
224
}
 
225
 
 
226
void QBasicMutex::lockInternal() Q_DECL_NOTHROW
 
227
{
 
228
    Q_ASSERT(!isRecursive());
 
229
    lockInternal_helper<false>(d_ptr);
 
230
}
 
231
 
 
232
bool QBasicMutex::lockInternal(int timeout) Q_DECL_NOTHROW
 
233
{
 
234
    Q_ASSERT(!isRecursive());
 
235
    return lockInternal_helper<true>(d_ptr, timeout);
 
236
}
 
237
 
 
238
void QBasicMutex::unlockInternal() Q_DECL_NOTHROW
 
239
{
 
240
    QMutexData *d = d_ptr.load();
 
241
    Q_ASSERT(d); //we must be locked
 
242
    Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed
 
243
    Q_UNUSED(d);
 
244
    Q_ASSERT(!isRecursive());
 
245
 
 
246
    d_ptr.storeRelease(0);
 
247
    _q_futex(&d_ptr, FUTEX_WAKE, 1, 0);
 
248
}
 
249
 
 
250
 
 
251
QT_END_NAMESPACE
 
252
 
 
253
#endif // QT_NO_THREAD