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

« back to all changes in this revision

Viewing changes to src/corelib/thread/qmutexpool.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
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtCore module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qatomic.h"
 
43
#include "qmutexpool_p.h"
 
44
 
 
45
#ifndef QT_NO_THREAD
 
46
 
 
47
QT_BEGIN_NAMESPACE
 
48
 
 
49
Q_GLOBAL_STATIC_WITH_ARGS(QMutexPool, globalMutexPool, (QMutex::Recursive))
 
50
 
 
51
/*!
 
52
    \class QMutexPool
 
53
    \inmodule QtCore
 
54
    \brief The QMutexPool class provides a pool of QMutex objects.
 
55
 
 
56
    \internal
 
57
 
 
58
    \ingroup thread
 
59
 
 
60
    QMutexPool is a convenience class that provides access to a fixed
 
61
    number of QMutex objects.
 
62
 
 
63
    Typical use of a QMutexPool is in situations where it is not
 
64
    possible or feasible to use one QMutex for every protected object.
 
65
    The mutex pool will return a mutex based on the address of the
 
66
    object that needs protection.
 
67
 
 
68
    For example, consider this simple class:
 
69
 
 
70
    \snippet code/src_corelib_thread_qmutexpool.cpp 0
 
71
 
 
72
    Adding a QMutex member to the Number class does not make sense,
 
73
    because it is so small. However, in order to ensure that access to
 
74
    each Number is protected, you need to use a mutex. In this case, a
 
75
    QMutexPool would be ideal.
 
76
 
 
77
    Code to calculate the square of a number would then look something
 
78
    like this:
 
79
 
 
80
    \snippet code/src_corelib_thread_qmutexpool.cpp 1
 
81
 
 
82
    This function will safely calculate the square of a number, since
 
83
    it uses a mutex from a QMutexPool. The mutex is locked and
 
84
    unlocked automatically by the QMutexLocker class. See the
 
85
    QMutexLocker documentation for more details.
 
86
*/
 
87
 
 
88
/*!
 
89
    Constructs  a QMutexPool, reserving space for \a size QMutexes. All
 
90
    mutexes in the pool are created with \a recursionMode. By default,
 
91
    all mutexes are non-recursive.
 
92
 
 
93
    The QMutexes are created when needed, and deleted when the
 
94
    QMutexPool is destructed.
 
95
*/
 
96
QMutexPool::QMutexPool(QMutex::RecursionMode recursionMode, int size)
 
97
    : mutexes(size), recursionMode(recursionMode)
 
98
{
 
99
    for (int index = 0; index < mutexes.count(); ++index) {
 
100
        mutexes[index].store(0);
 
101
    }
 
102
}
 
103
 
 
104
/*!
 
105
    Destructs a QMutexPool. All QMutexes that were created by the pool
 
106
    are deleted.
 
107
*/
 
108
QMutexPool::~QMutexPool()
 
109
{
 
110
    for (int index = 0; index < mutexes.count(); ++index)
 
111
        delete mutexes[index].load();
 
112
}
 
113
 
 
114
/*!
 
115
    Returns the global QMutexPool instance.
 
116
*/
 
117
QMutexPool *QMutexPool::instance()
 
118
{
 
119
    return globalMutexPool();
 
120
}
 
121
 
 
122
/*!
 
123
    \fn QMutexPool::get(const void *address)
 
124
    Returns a QMutex from the pool. QMutexPool uses the value \a address
 
125
    to determine which mutex is returned from the pool.
 
126
*/
 
127
 
 
128
/*!
 
129
    \internal
 
130
  create the mutex for the given index
 
131
 */
 
132
QMutex *QMutexPool::createMutex(int index)
 
133
{
 
134
    // mutex not created, create one
 
135
    QMutex *newMutex = new QMutex(recursionMode);
 
136
    if (!mutexes[index].testAndSetRelease(0, newMutex))
 
137
        delete newMutex;
 
138
    return mutexes[index].load();
 
139
}
 
140
 
 
141
/*!
 
142
    Returns a QMutex from the global mutex pool.
 
143
*/
 
144
QMutex *QMutexPool::globalInstanceGet(const void *address)
 
145
{
 
146
    QMutexPool * const globalInstance = globalMutexPool();
 
147
    if (globalInstance == 0)
 
148
        return 0;
 
149
    return globalInstance->get(address);
 
150
}
 
151
 
 
152
QT_END_NAMESPACE
 
153
 
 
154
#endif // QT_NO_THREAD