QtBase  v6.3.1
qthreadstorage.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QTHREADSTORAGE_H
41 #define QTHREADSTORAGE_H
42 
43 #include <QtCore/qglobal.h>
44 
45 #if QT_CONFIG(thread)
46 
48 
49 
50 class Q_CORE_EXPORT QThreadStorageData
51 {
52 public:
53  explicit QThreadStorageData(void (*func)(void *));
54  ~QThreadStorageData();
55 
56  void** get() const;
57  void** set(void* p);
58 
59  static void finish(void**);
60  int id;
61 };
62 
63 #if !defined(QT_MOC_CPP)
64 // MOC_SKIP_BEGIN
65 
66 // pointer specialization
67 template <typename T>
68 inline
69 T *&qThreadStorage_localData(QThreadStorageData &d, T **)
70 {
71  void **v = d.get();
72  if (!v) v = d.set(nullptr);
73  return *(reinterpret_cast<T**>(v));
74 }
75 
76 template <typename T>
77 inline
78 T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
79 {
80  void **v = d.get();
81  return v ? *(reinterpret_cast<T**>(v)) : 0;
82 }
83 
84 template <typename T>
85 inline
86 void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
87 { (void) d.set(*t); }
88 
89 template <typename T>
90 inline
91 void qThreadStorage_deleteData(void *d, T **)
92 { delete static_cast<T *>(d); }
93 
94 // value-based specialization
95 template <typename T>
96 inline
97 T &qThreadStorage_localData(QThreadStorageData &d, T *)
98 {
99  void **v = d.get();
100  if (!v) v = d.set(new T());
101  return *(reinterpret_cast<T*>(*v));
102 }
103 
104 template <typename T>
105 inline
106 T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
107 {
108  void **v = d.get();
109  return v ? *(reinterpret_cast<T*>(*v)) : T();
110 }
111 
112 template <typename T>
113 inline
114 void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
115 { (void) d.set(new T(*t)); }
116 
117 template <typename T>
118 inline
119 void qThreadStorage_deleteData(void *d, T *)
120 { delete static_cast<T *>(d); }
121 
122 
123 // MOC_SKIP_END
124 #endif
125 
126 template <class T>
127 class QThreadStorage
128 {
129 private:
130  QThreadStorageData d;
131 
133 
134  static inline void deleteData(void *x)
135  { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
136 
137 public:
138  inline QThreadStorage() : d(deleteData) { }
139  inline ~QThreadStorage() { }
140 
141  inline bool hasLocalData() const
142  { return d.get() != nullptr; }
143 
144  inline T& localData()
145  { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
146  inline T localData() const
147  { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
148 
149  inline void setLocalData(T t)
150  { qThreadStorage_setLocalData(d, &t); }
151 };
152 
154 
155 #else // !QT_CONFIG(thread)
156 
157 #include <QtCore/qscopedpointer.h>
158 
159 #include <type_traits>
160 
162 
163 template <typename T, typename U>
165 {
166  return !!data;
167 }
168 
169 template <typename T, typename U>
171 {
172  return !!data ? *data != nullptr : false;
173 }
174 
175 template <typename T>
177 {
178  delete t;
179 }
180 
181 template <typename T>
183 {
184  delete *t;
185  delete t;
186 }
187 
188 template <class T>
190 {
191 private:
192  struct ScopedPointerThreadStorageDeleter
193  {
194  static inline void cleanup(T *t)
195  {
196  if (t == nullptr)
197  return;
199  }
200  };
202 
203 public:
204  QThreadStorage() = default;
205  ~QThreadStorage() = default;
208 
209  inline bool hasLocalData() const
210  {
212  }
213 
214  inline T &localData()
215  {
216  if (!data)
217  data.reset(new T());
218  return *data;
219  }
220 
221  inline T localData() const
222  {
223  return !!data ? *data : T();
224  }
225 
226  inline void setLocalData(T t)
227  {
228  data.reset(new T(t));
229  }
230 };
231 
233 
234 #endif // QT_CONFIG(thread)
235 
236 #endif // QTHREADSTORAGE_H
char * data()
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
The QThreadStorage class provides per-thread data storage.
QThreadStorage()=default
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
T localData() const
~QThreadStorage()=default
void setLocalData(T t)
#define T(x)
Definition: main.cpp:42
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld cleanup[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1 beq endif SRC MASK if dst_r_bpp DST_R else add endif PF add sub src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head pixblock_size cache_preload_simple process_pixblock_tail pixinterleave dst_w_basereg irp beq endif process_pixblock_tail_head tst beq irp if pixblock_size chunk_size tst beq pixld SRC pixld MASK if DST_R else pixld DST_R endif if src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head if pixblock_size cache_preload_simple endif process_pixblock_tail pixinterleave dst_w_basereg irp if pixblock_size chunk_size tst beq if DST_W else pixst DST_W else mov ORIG_W endif add lsl if lsl endif if lsl endif lsl endif lsl endif lsl endif subs mov DST_W if regs_shortage str endif bge start_of_loop_label endm macro generate_composite_function
void
Definition: png.h:1080
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
GLenum GLuint id
[6]
Definition: qopengl.h:270
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum func
Definition: qopenglext.h:663
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
QT_BEGIN_NAMESPACE bool qThreadStorage_hasLocalData(const QScopedPointer< T, U > &data)
void qThreadStorage_deleteLocalData(T *t)
QFuture< QSet< QChar > > set
[10]
QObject::connect nullptr
http get(QUrl::toPercentEncoding("/index.html"))
Definition: main.cpp:38
#define rhs