Qt 4.8
Public Functions | Static Public Functions | Public Variables | List of all members
QThreadStorageData Class Reference

#include <qthreadstorage.h>

Public Functions

void ** get () const
 
 QThreadStorageData (void(*func)(void *))
 
void ** set (void *p)
 
 ~QThreadStorageData ()
 

Static Public Functions

static void finish (void **)
 

Public Variables

int id
 

Detailed Description

Definition at line 55 of file qthreadstorage.h.

Constructors and Destructors

◆ QThreadStorageData()

QThreadStorageData::QThreadStorageData ( void(*)(void *)  func)
explicit

Definition at line 78 of file qthreadstorage.cpp.

79 {
80  QMutexLocker locker(mutex());
81  DestructorMap *destr = destructors();
82  if (!destr) {
83  /*
84  the destructors vector has already been destroyed, yet a new
85  QThreadStorage is being allocated. this can only happen during global
86  destruction, at which point we assume that there is only one thread.
87  in order to keep QThreadStorage working, we need somewhere to store
88  the data, best place we have in this situation is at the tail of the
89  current thread's tls vector. the destructor is ignored, since we have
90  no where to store it, and no way to actually call it.
91  */
93  id = data->tls.count();
94  DEBUG_MSG("QThreadStorageData: Allocated id %d, destructor %p cannot be stored", id, func);
95  return;
96  }
97  for (id = 0; id < destr->count(); id++) {
98  if (destr->at(id) == 0)
99  break;
100  }
101  if (id == destr->count()) {
102  destr->append(func);
103  } else {
104  (*destr)[id] = func;
105  }
106  DEBUG_MSG("QThreadStorageData: Allocated id %d, destructor %p", id, func);
107 }
#define DEBUG_MSG
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< void * > tls
Definition: qthread_p.h:268
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
static const char * data(const QByteArray &arr)
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QThreadData * current()

◆ ~QThreadStorageData()

QThreadStorageData::~QThreadStorageData ( )

Definition at line 109 of file qthreadstorage.cpp.

110 {
111  DEBUG_MSG("QThreadStorageData: Released id %d", id);
112  QMutexLocker locker(mutex());
113  if (destructors())
114  (*destructors())[id] = 0;
115 }
#define DEBUG_MSG
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101

Functions

◆ finish()

void QThreadStorageData::finish ( void **  p)
static

Definition at line 174 of file qthreadstorage.cpp.

Referenced by QThreadPrivate::finish(), and QCoreApplicationPrivate::~QCoreApplicationPrivate().

175 {
176  QVector<void *> *tls = reinterpret_cast<QVector<void *> *>(p);
177  if (!tls || tls->isEmpty() || !mutex())
178  return; // nothing to do
179 
180  DEBUG_MSG("QThreadStorageData: Destroying storage for thread %p", QThread::currentThread());
181  while (!tls->isEmpty()) {
182  void *&value = tls->last();
183  void *q = value;
184  value = 0;
185  int i = tls->size() - 1;
186  tls->resize(i);
187 
188  if (!q) {
189  // data already deleted
190  continue;
191  }
192 
193  QMutexLocker locker(mutex());
194  void (*destructor)(void *) = destructors()->value(i);
195  locker.unlock();
196 
197  if (!destructor) {
199  qWarning("QThreadStorage: Thread %p exited after QThreadStorage %d destroyed",
201  continue;
202  }
203  destructor(q); //crash here might mean the thread exited after qthreadstorage was destroyed
204 
205  if (tls->size() > i) {
206  //re reset the tls in case it has been recreated by its own destructor.
207  (*tls)[i] = 0;
208  }
209  }
210  tls->clear();
211 }
#define DEBUG_MSG
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
Q_CORE_EXPORT void qWarning(const char *,...)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QThreadStorage< QSharedNetworkSessionManager * > tls

◆ get()

void ** QThreadStorageData::get ( ) const

Definition at line 117 of file qthreadstorage.cpp.

Referenced by QThreadStorage< QNetworkAccessCache *>::hasLocalData(), qThreadStorage_localData(), and qThreadStorage_localData_const().

118 {
120  if (!data) {
121  qWarning("QThreadStorage::get: QThreadStorage can only be used with threads started with QThread");
122  return 0;
123  }
124  QVector<void *> &tls = data->tls;
125  if (tls.size() <= id)
126  tls.resize(id + 1);
127  void **v = &tls[id];
128 
129  DEBUG_MSG("QThreadStorageData: Returning storage %d, data %p, for thread %p",
130  id,
131  *v,
132  data->thread);
133 
134  return *v ? v : 0;
135 }
#define DEBUG_MSG
QVector< void * > tls
Definition: qthread_p.h:268
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
static QThreadData * current()
QThread * thread
Definition: qthread_p.h:260
QThreadStorage< QSharedNetworkSessionManager * > tls

◆ set()

void ** QThreadStorageData::set ( void *  p)

Definition at line 137 of file qthreadstorage.cpp.

Referenced by qThreadStorage_localData().

138 {
140  if (!data) {
141  qWarning("QThreadStorage::set: QThreadStorage can only be used with threads started with QThread");
142  return 0;
143  }
144  QVector<void *> &tls = data->tls;
145  if (tls.size() <= id)
146  tls.resize(id + 1);
147 
148  void *&value = tls[id];
149  // delete any previous data
150  if (value != 0) {
151  DEBUG_MSG("QThreadStorageData: Deleting previous storage %d, data %p, for thread %p",
152  id,
153  value,
154  data->thread);
155 
156  QMutexLocker locker(mutex());
157  DestructorMap *destr = destructors();
158  void (*destructor)(void *) = destr ? destr->value(id) : 0;
159  locker.unlock();
160 
161  void *q = value;
162  value = 0;
163 
164  if (destructor)
165  destructor(q);
166  }
167 
168  // store new data
169  value = p;
170  DEBUG_MSG("QThreadStorageData: Set storage %d for thread %p to %p", id, data->thread, p);
171  return &value;
172 }
#define DEBUG_MSG
QVector< void * > tls
Definition: qthread_p.h:268
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QThreadData * current()
QThread * thread
Definition: qthread_p.h:260
QThreadStorage< QSharedNetworkSessionManager * > tls

Properties

◆ id

int QThreadStorageData::id

Definition at line 65 of file qthreadstorage.h.

Referenced by get(), and set().


The documentation for this class was generated from the following files: