author | kergoth <kergoth> | 2002-11-01 00:10:42 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-11-01 00:10:42 (UTC) |
commit | 5042e3cf0d3514552769e441f5aad590c8eaf967 (patch) (unidiff) | |
tree | 4a5ea45f3519d981a172ab5275bf38c6fa778dec /qmake/tools/qmutexpool.cpp | |
parent | 108c1c753e74e989cc13923086996791428c9af4 (diff) | |
download | opie-5042e3cf0d3514552769e441f5aad590c8eaf967.zip opie-5042e3cf0d3514552769e441f5aad590c8eaf967.tar.gz opie-5042e3cf0d3514552769e441f5aad590c8eaf967.tar.bz2 |
Adding qmake in preperation for new build system
-rw-r--r-- | qmake/tools/qmutexpool.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/qmake/tools/qmutexpool.cpp b/qmake/tools/qmutexpool.cpp new file mode 100644 index 0000000..9ed2829 --- a/dev/null +++ b/qmake/tools/qmutexpool.cpp | |||
@@ -0,0 +1,130 @@ | |||
1 | #include "qmutexpool_p.h" | ||
2 | |||
3 | #ifdef QT_THREAD_SUPPORT | ||
4 | |||
5 | #include <qthread.h> | ||
6 | #include <stdio.h> | ||
7 | |||
8 | QMutexPool *qt_global_mutexpool = 0; | ||
9 | |||
10 | // this is an internal class used only for inititalizing the global mutexpool | ||
11 | class QGlobalMutexPoolInitializer | ||
12 | { | ||
13 | public: | ||
14 | inline QGlobalMutexPoolInitializer() | ||
15 | { | ||
16 | /* | ||
17 | Purify will report a leak here. However, this mutex pool must be alive | ||
18 | until *everything* in Qt has been destructed. Unfortunately there is | ||
19 | no way to guarantee this, so we never destroy this mutex pool. | ||
20 | */ | ||
21 | qt_global_mutexpool = new QMutexPool( TRUE ); | ||
22 | } | ||
23 | }; | ||
24 | QGlobalMutexPoolInitializer qt_global_mutexpool_initializer; | ||
25 | |||
26 | /*! | ||
27 | \class QMutexPool qmutexpool_p.h | ||
28 | \brief The QMutexPool class provides a pool of QMutex objects. | ||
29 | |||
30 | \internal | ||
31 | |||
32 | \ingroup thread | ||
33 | |||
34 | QMutexPool is a convenience class that provides access to a fixed | ||
35 | number of QMutex objects. | ||
36 | |||
37 | Typical use of a QMutexPool is in situations where it is not | ||
38 | possible or feasible to use one QMutex for every protected object. | ||
39 | The mutex pool will return a mutex based on the address of the | ||
40 | object that needs protection. | ||
41 | |||
42 | For example, consider this simple class: | ||
43 | |||
44 | \code | ||
45 | class Number { | ||
46 | public: | ||
47 | Number( double n ) : num ( n ) { } | ||
48 | |||
49 | void setNumber( double n ) { num = n; } | ||
50 | double number() const { return num; } | ||
51 | |||
52 | private: | ||
53 | double num; | ||
54 | }; | ||
55 | \endcode | ||
56 | |||
57 | Adding a QMutex member to the Number class does not make sense, | ||
58 | because it is so small. However, in order to ensure that access to | ||
59 | each Number is protected, you need to use a mutex. In this case, a | ||
60 | QMutexPool would be ideal. | ||
61 | |||
62 | Code to calculate the square of a number would then look something | ||
63 | like this: | ||
64 | |||
65 | \code | ||
66 | void calcSquare( Number *num ) | ||
67 | { | ||
68 | QMutexLocker locker( mutexpool.get( num ) ); | ||
69 | num.setNumber( num.number() * num.number() ); | ||
70 | } | ||
71 | \endcode | ||
72 | |||
73 | This function will safely calculate the square of a number, since | ||
74 | it uses a mutex from a QMutexPool. The mutex is locked and | ||
75 | unlocked automatically by the QMutexLocker class. See the | ||
76 | QMutexLocker documentation for more details. | ||
77 | */ | ||
78 | |||
79 | /*! | ||
80 | Constructs a QMutexPool, reserving space for \a size QMutexes. If | ||
81 | \a recursive is TRUE, all QMutexes in the pool will be recursive | ||
82 | mutexes; otherwise they will all be non-recursive (the default). | ||
83 | |||
84 | The QMutexes are created when needed, and deleted when the | ||
85 | QMutexPool is destructed. | ||
86 | */ | ||
87 | QMutexPool::QMutexPool( bool recursive, int size ) | ||
88 | : mutex( FALSE ), mutexes( size ), recurs( recursive ) | ||
89 | { | ||
90 | mutexes.fill( 0 ); | ||
91 | } | ||
92 | |||
93 | /*! | ||
94 | Destructs a QMutexPool. All QMutexes that were created by the pool | ||
95 | are deleted. | ||
96 | */ | ||
97 | QMutexPool::~QMutexPool() | ||
98 | { | ||
99 | QMutexLocker locker( &mutex ); | ||
100 | QMutex **d = mutexes.data(); | ||
101 | for ( int index = 0; (uint) index < mutexes.size(); index++ ) { | ||
102 | delete d[index]; | ||
103 | d[index] = 0; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | /*! | ||
108 | Returns a QMutex from the pool. QMutexPool uses the value \a | ||
109 | address to determine which mutex is retured from the pool. | ||
110 | */ | ||
111 | QMutex *QMutexPool::get( void *address ) | ||
112 | { | ||
113 | QMutex **d = mutexes.data(); | ||
114 | int index = (int)( (ulong) address % mutexes.size() ); | ||
115 | |||
116 | if ( ! d[index] ) { | ||
117 | // mutex not created, create one | ||
118 | |||
119 | QMutexLocker locker( &mutex ); | ||
120 | // we need to check once again that the mutex hasn't been created, since | ||
121 | // 2 threads could be trying to create a mutex as the same index... | ||
122 | if ( ! d[index] ) { | ||
123 | d[index] = new QMutex( recurs ); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | return d[index]; | ||
128 | } | ||
129 | |||
130 | #endif | ||