Qt 4.8
Macros | Typedefs | Enumerations | Functions | Variables
qthread_unix.cpp File Reference
#include "qthread.h"
#include "qplatformdefs.h"
#include <private/qcoreapplication_p.h>
#include "../kernel/qeventdispatcher_glib_p.h"
#include <private/qeventdispatcher_unix_p.h>
#include "qthreadstorage.h"
#include "qthread_p.h"
#include "qdebug.h"
#include <sched.h>
#include <errno.h>
#include <CoreServices/CoreServices.h>
#include <sys/prctl.h>

Go to the source code of this file.

Macros

#define QT_HAS_THREAD_PRIORITY_SCHEDULING
 
#define SCHED_IDLE   5
 

Typedefs

typedef void *(* QtThreadCallback) (void *)
 

Enumerations

enum  { ThreadPriorityResetFlag = 0x80000000 }
 

Functions

static bool calculateUnixPriority (int priority, int *sched_policy, int *sched_priority)
 
static void clear_thread_data ()
 
static void create_current_thread_data_key ()
 
static void destroy_current_thread_data (void *p)
 
static void destroy_current_thread_data_key ()
 
static QThreadDataget_thread_data ()
 
static void set_thread_data (QThreadData *data)
 
static void setCurrentThreadName (pthread_t threadId, const char *name)
 
static void thread_sleep (struct timespec *ti)
 

Variables

static pthread_key_t current_thread_data_key
 
static pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT
 

Macro Definition Documentation

◆ QT_HAS_THREAD_PRIORITY_SCHEDULING

#define QT_HAS_THREAD_PRIORITY_SCHEDULING

Definition at line 107 of file qthread_unix.cpp.

◆ SCHED_IDLE

#define SCHED_IDLE   5

Definition at line 103 of file qthread_unix.cpp.

Referenced by calculateUnixPriority(), and QThread::setPriority().

Typedef Documentation

◆ QtThreadCallback

typedef void*(* QtThreadCallback) (void *)

Definition at line 266 of file qthread_unix.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ThreadPriorityResetFlag 

Definition at line 115 of file qthread_unix.cpp.

Function Documentation

◆ calculateUnixPriority()

static bool calculateUnixPriority ( int  priority,
int *  sched_policy,
int *  sched_priority 
)
static

Definition at line 537 of file qthread_unix.cpp.

Referenced by QThread::setPriority(), and QThread::start().

538 {
539 #ifdef Q_OS_QNX
540  // without Round Robin drawn intensive apps will hog the cpu
541  // and make the system appear frozen
542  *sched_policy = SCHED_RR;
543 #endif
544 #ifdef SCHED_IDLE
545  if (priority == QThread::IdlePriority) {
546  *sched_policy = SCHED_IDLE;
547  *sched_priority = 0;
548  return true;
549  }
550  const int lowestPriority = QThread::LowestPriority;
551 #else
552  const int lowestPriority = QThread::IdlePriority;
553 #endif
554  const int highestPriority = QThread::TimeCriticalPriority;
555 
556  int prio_min;
557  int prio_max;
558 #if defined(Q_OS_VXWORKS) && defined(VXWORKS_DKM)
559  // for other scheduling policies than SCHED_RR or SCHED_FIFO
560  prio_min = SCHED_FIFO_LOW_PRI;
561  prio_max = SCHED_FIFO_HIGH_PRI;
562 
563  if ((*sched_policy == SCHED_RR) || (*sched_policy == SCHED_FIFO))
564 #endif
565  {
566  prio_min = sched_get_priority_min(*sched_policy);
567  prio_max = sched_get_priority_max(*sched_policy);
568  }
569 
570  if (prio_min == -1 || prio_max == -1)
571  return false;
572 
573  int prio;
574  // crudely scale our priority enum values to the prio_min/prio_max
575  prio = ((priority - lowestPriority) * (prio_max - prio_min) / highestPriority) + prio_min;
576  prio = qMax(prio_min, qMin(prio_max, prio));
577 
578  *sched_priority = prio;
579  return true;
580 }
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define SCHED_IDLE

◆ clear_thread_data()

static void clear_thread_data ( )
static

Definition at line 204 of file qthread_unix.cpp.

Referenced by QThreadData::clearCurrentThreadData(), and QThreadData::current().

205 {
206 #ifdef HAVE_TLS
207  currentThreadData = 0;
208 #endif
209  pthread_setspecific(current_thread_data_key, 0);
210 }
static pthread_key_t current_thread_data_key

◆ create_current_thread_data_key()

static void create_current_thread_data_key ( )
static

Definition at line 165 of file qthread_unix.cpp.

Referenced by destroy_current_thread_data_key(), get_thread_data(), and set_thread_data().

166 {
168 }
static pthread_key_t current_thread_data_key
static void destroy_current_thread_data(void *p)

◆ destroy_current_thread_data()

static void destroy_current_thread_data ( void *  p)
static

Definition at line 131 of file qthread_unix.cpp.

Referenced by create_current_thread_data_key().

132 {
133 #if defined(Q_OS_VXWORKS)
134  // Calling setspecific(..., 0) sets the value to 0 for ALL threads.
135  // The 'set to 1' workaround adds a bit of an overhead though,
136  // since this function is called twice now.
137  if (p == (void *)1)
138  return;
139 #endif
140  // POSIX says the value in our key is set to zero before calling
141  // this destructor function, so we need to set it back to the
142  // right value...
143  pthread_setspecific(current_thread_data_key, p);
144  QThreadData *data = static_cast<QThreadData *>(p);
145  if (data->isAdopted) {
146  QThread *thread = data->thread;
147  Q_ASSERT(thread);
148  QThreadPrivate *thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
149  Q_ASSERT(!thread_p->finished);
150  thread_p->finish(thread);
151  }
152  data->deref();
153 
154  // ... but we must reset it to zero before returning so we aren't
155  // called again (POSIX allows implementations to call destructor
156  // functions repeatedly until all values are zero)
157  pthread_setspecific(current_thread_data_key,
158 #if defined(Q_OS_VXWORKS)
159  (void *)1);
160 #else
161  0);
162 #endif
163 }
bool isAdopted
Definition: qthread_p.h:269
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
void deref()
Definition: qthread.cpp:125
static const char * data(const QByteArray &arr)
static pthread_key_t current_thread_data_key
static void finish(void *)
QThread * thread
Definition: qthread_p.h:260
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59

◆ destroy_current_thread_data_key()

static void destroy_current_thread_data_key ( )
static

Definition at line 170 of file qthread_unix.cpp.

171 {
173  pthread_key_delete(current_thread_data_key);
174 
175  // Reset current_thread_data_once in case we end up recreating
176  // the thread-data in the rare case of QObject construction
177  // after destroying the QThreadData.
178  pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT;
179  current_thread_data_once = pthread_once_init;
180 }
static void create_current_thread_data_key()
static pthread_once_t current_thread_data_once
static pthread_key_t current_thread_data_key

◆ get_thread_data()

static QThreadData* get_thread_data ( )
static

Definition at line 185 of file qthread_unix.cpp.

Referenced by QThreadData::current().

186 {
187 #ifdef HAVE_TLS
188  return currentThreadData;
189 #else
191  return reinterpret_cast<QThreadData *>(pthread_getspecific(current_thread_data_key));
192 #endif
193 }
static void create_current_thread_data_key()
static pthread_once_t current_thread_data_once
static pthread_key_t current_thread_data_key

◆ set_thread_data()

static void set_thread_data ( QThreadData data)
static

Definition at line 195 of file qthread_unix.cpp.

Referenced by QThreadData::current(), and QThreadPrivate::start().

196 {
197 #ifdef HAVE_TLS
198  currentThreadData = data;
199 #endif
201  pthread_setspecific(current_thread_data_key, data);
202 }
static const char * data(const QByteArray &arr)
static void create_current_thread_data_key()
static pthread_once_t current_thread_data_once
static pthread_key_t current_thread_data_key

◆ setCurrentThreadName()

static void setCurrentThreadName ( pthread_t  threadId,
const char *  name 
)
static

Definition at line 295 of file qthread_unix.cpp.

Referenced by QThreadPrivate::start().

296 {
297 # if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
298  Q_UNUSED(threadId);
299  prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0);
300 # elif (defined(Q_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
301  Q_UNUSED(threadId);
303  pthread_setname_np(name);
304 # elif defined(Q_OS_QNX)
305  pthread_setname_np(threadId, name);
306 # endif
307 }
const char * name
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ thread_sleep()

static void thread_sleep ( struct timespec *  ti)
static

Definition at line 483 of file qthread_unix.cpp.

Referenced by QThread::msleep(), QThread::sleep(), and QThread::usleep().

484 {
485  pthread_mutex_t mtx;
486  pthread_cond_t cnd;
487 
488  pthread_mutex_init(&mtx, 0);
489  pthread_cond_init(&cnd, 0);
490 
491  pthread_mutex_lock(&mtx);
492  (void) pthread_cond_timedwait(&cnd, &mtx, ti);
493  pthread_mutex_unlock(&mtx);
494 
495  pthread_cond_destroy(&cnd);
496  pthread_mutex_destroy(&mtx);
497 }

Variable Documentation

◆ current_thread_data_key

pthread_key_t current_thread_data_key
static

◆ current_thread_data_once

pthread_once_t current_thread_data_once = PTHREAD_ONCE_INIT
static