Blame qtools/qthread_unix.cpp

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
Packit Service 50c9f2
** Contact: Nokia Corporation (qt-info@nokia.com)
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the QtCore module of the Qt Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** $QT_BEGIN_LICENSE:LGPL$
Packit Service 50c9f2
** No Commercial Usage
Packit Service 50c9f2
** This file contains pre-release code and may not be distributed.
Packit Service 50c9f2
** You may use this file in accordance with the terms and conditions
Packit Service 50c9f2
** contained in the either Technology Preview License Agreement or the
Packit Service 50c9f2
** Beta Release License Agreement.
Packit Service 50c9f2
**
Packit Service 50c9f2
** GNU Lesser General Public License Usage
Packit Service 50c9f2
** Alternatively, this file may be used under the terms of the GNU Lesser
Packit Service 50c9f2
** General Public License version 2.1 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.LGPL included in the
Packit Service 50c9f2
** packaging of this file.  Please review the following information to
Packit Service 50c9f2
** ensure the GNU Lesser General Public License version 2.1 requirements
Packit Service 50c9f2
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Packit Service 50c9f2
**
Packit Service 50c9f2
** In addition, as a special exception, Nokia gives you certain
Packit Service 50c9f2
** additional rights. These rights are described in the Nokia Qt LGPL
Packit Service 50c9f2
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
Packit Service 50c9f2
** package.
Packit Service 50c9f2
**
Packit Service 50c9f2
** GNU General Public License Usage
Packit Service 50c9f2
** Alternatively, this file may be used under the terms of the GNU
Packit Service 50c9f2
** General Public License version 3.0 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.  Please review the following information to
Packit Service 50c9f2
** ensure the GNU General Public License version 3.0 requirements will be
Packit Service 50c9f2
** met: http://www.gnu.org/copyleft/gpl.html.
Packit Service 50c9f2
**
Packit Service 50c9f2
** If you are unsure which license is appropriate for your use, please
Packit Service 50c9f2
** contact the sales department at http://www.qtsoftware.com/contact.
Packit Service 50c9f2
** $QT_END_LICENSE$
Packit Service 50c9f2
**
Packit Service 50c9f2
****************************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
#include "qglobal.h"
Packit Service 50c9f2
Packit Service 50c9f2
#if defined(_OS_HPUX_)
Packit Service 50c9f2
#include <sys/pstat.h>
Packit Service 50c9f2
#elif defined(_OS_MAC_)
Packit Service 50c9f2
#undef DEBUG
Packit Service 50c9f2
#include <CoreServices/CoreServices.h>
Packit Service 50c9f2
#elif defined(_OS_BSDI_)
Packit Service 50c9f2
#include <mach/mach_types.h>
Packit Service 50c9f2
#include <sys/systm.h>
Packit Service 50c9f2
#include <sys/types.h>
Packit Service 50c9f2
#include <sys/sysctl.h>
Packit Service 50c9f2
#endif
Packit Service 50c9f2
#include <signal.h>
Packit Service 50c9f2
#include <unistd.h>
Packit Service 50c9f2
#include <stdio.h>
Packit Service 50c9f2
Packit Service 50c9f2
#include "qthread.h"
Packit Service 50c9f2
#include "qthread_p.h"
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/**************************************************************************
Packit Service 50c9f2
 ** QThreadPrivate
Packit Service 50c9f2
 *************************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
QThreadPrivate::QThreadPrivate() :
Packit Service 50c9f2
  running(FALSE), finished(FALSE), terminated(FALSE), stackSize(0)
Packit Service 50c9f2
{
Packit Service 50c9f2
  thread_id = 0;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
QThreadPrivate::~QThreadPrivate()
Packit Service 50c9f2
{
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void *QThreadPrivate::start(void *arg)
Packit Service 50c9f2
{
Packit Service 50c9f2
#ifndef __ANDROID__
Packit Service 50c9f2
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    pthread_cleanup_push(QThreadPrivate::finish, arg);
Packit Service 50c9f2
Packit Service 50c9f2
    QThread *thr = reinterpret_cast<QThread *>(arg);
Packit Service 50c9f2
Packit Service 50c9f2
    thr->started();
Packit Service 50c9f2
#ifndef __ANDROID__
Packit Service 50c9f2
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
Packit Service 50c9f2
    pthread_testcancel();
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    thr->run();
Packit Service 50c9f2
Packit Service 50c9f2
    pthread_cleanup_pop(1);
Packit Service 50c9f2
    return 0;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void QThreadPrivate::finish(void *arg)
Packit Service 50c9f2
{
Packit Service 50c9f2
    QThread *thr = reinterpret_cast<QThread *>(arg);
Packit Service 50c9f2
    QThreadPrivate *d = thr->d;
Packit Service 50c9f2
    QMutexLocker locker(&d->mutex);
Packit Service 50c9f2
Packit Service 50c9f2
    d->running = FALSE;
Packit Service 50c9f2
    d->finished = TRUE;
Packit Service 50c9f2
    if (d->terminated)
Packit Service 50c9f2
        thr->terminated();
Packit Service 50c9f2
    d->terminated = FALSE;
Packit Service 50c9f2
    thr->finished();
Packit Service 50c9f2
Packit Service 50c9f2
    d->thread_id = 0;
Packit Service 50c9f2
    d->thread_done.wakeAll();
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
/**************************************************************************
Packit Service 50c9f2
 ** QThread
Packit Service 50c9f2
 *************************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
void QThread::start()
Packit Service 50c9f2
{
Packit Service 50c9f2
    QMutexLocker locker(&d->mutex);
Packit Service 50c9f2
    if (d->running) return;
Packit Service 50c9f2
Packit Service 50c9f2
    // Block the SIGINT signal. The threads will inherit the signal mask.
Packit Service 50c9f2
    // This will avoid them catching SIGINT instead of this thread.
Packit Service 50c9f2
    sigset_t sigset, oldset;
Packit Service 50c9f2
    sigemptyset(&sigset);
Packit Service 50c9f2
    sigaddset(&sigset, SIGINT);
Packit Service 50c9f2
    pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
Packit Service 50c9f2
Packit Service 50c9f2
    d->running = TRUE;
Packit Service 50c9f2
    d->finished = FALSE;
Packit Service 50c9f2
Packit Service 50c9f2
    pthread_attr_t attr;
Packit Service 50c9f2
    pthread_attr_init(&attr);
Packit Service 50c9f2
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
Packit Service 50c9f2
#ifndef __ANDROID__
Packit Service 50c9f2
    pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    if (d->stackSize>0)
Packit Service 50c9f2
    {
Packit Service 50c9f2
#if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0>0)
Packit Service 50c9f2
      pthread_attr_setstacksize(&attr,d->stackSize);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    }
Packit Service 50c9f2
    int code = pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this);
Packit Service 50c9f2
    pthread_attr_destroy(&attr);
Packit Service 50c9f2
Packit Service 50c9f2
    if (code)
Packit Service 50c9f2
    {
Packit Service 50c9f2
        qWarning("QThread::start: Thread creation error: %d", code);
Packit Service 50c9f2
Packit Service 50c9f2
        d->running = FALSE;
Packit Service 50c9f2
        d->finished = FALSE;
Packit Service 50c9f2
        d->thread_id = 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    else
Packit Service 50c9f2
    {
Packit Service 50c9f2
       // Restore the old signal mask only for this thread.
Packit Service 50c9f2
       pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Packit Service 50c9f2
    }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void QThread::terminate()
Packit Service 50c9f2
{
Packit Service 50c9f2
    QMutexLocker locker(&d->mutex);
Packit Service 50c9f2
#ifndef __ANDROID__
Packit Service 50c9f2
    if (!d->thread_id) return;
Packit Service 50c9f2
Packit Service 50c9f2
    int code = pthread_cancel(d->thread_id);
Packit Service 50c9f2
    if (code)
Packit Service 50c9f2
    {
Packit Service 50c9f2
        qWarning("QThread::start: Thread termination error: %d", code);
Packit Service 50c9f2
    }
Packit Service 50c9f2
    else
Packit Service 50c9f2
    {
Packit Service 50c9f2
        d->terminated = TRUE;
Packit Service 50c9f2
    }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
void QThread::wait()
Packit Service 50c9f2
{
Packit Service 50c9f2
    QMutexLocker locker(&d->mutex);
Packit Service 50c9f2
    if (d->finished || !d->running) return;
Packit Service 50c9f2
Packit Service 50c9f2
    while (d->running)
Packit Service 50c9f2
    {
Packit Service 50c9f2
      d->thread_done.wait(locker.mutex());
Packit Service 50c9f2
    }
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
#if defined(QT_LINUXBASE) && !defined(_SC_NPROCESSORS_ONLN)
Packit Service 50c9f2
// LSB doesn't define _SC_NPROCESSORS_ONLN.
Packit Service 50c9f2
#  define _SC_NPROCESSORS_ONLN 84
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
int QThread::idealThreadCount()
Packit Service 50c9f2
{
Packit Service 50c9f2
    int cores = -1;
Packit Service 50c9f2
#if defined(_OS_MAC_)
Packit Service 50c9f2
    // Mac OS X
Packit Service 50c9f2
    cores = (int)MPProcessorsScheduled();
Packit Service 50c9f2
#elif defined(_OS_HPUX_)
Packit Service 50c9f2
    // HP-UX
Packit Service 50c9f2
    struct pst_dynamic psd;
Packit Service 50c9f2
    if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1) 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      perror("pstat_getdynamic");
Packit Service 50c9f2
      cores = -1;
Packit Service 50c9f2
    } 
Packit Service 50c9f2
    else 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      cores = (int)psd.psd_proc_cnt;
Packit Service 50c9f2
    }
Packit Service 50c9f2
#elif defined(_OS_BSDI_)
Packit Service 50c9f2
    // FreeBSD, OpenBSD, NetBSD, BSD/OS
Packit Service 50c9f2
    size_t len = sizeof(cores);
Packit Service 50c9f2
    int mib[2];
Packit Service 50c9f2
    mib[0] = CTL_HW;
Packit Service 50c9f2
    mib[1] = HW_NCPU;
Packit Service 50c9f2
Packit Service 50c9f2
    if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0) 
Packit Service 50c9f2
    {
Packit Service 50c9f2
      perror("sysctl");
Packit Service 50c9f2
      cores = -1;
Packit Service 50c9f2
    }
Packit Service 50c9f2
#elif defined(_OS_IRIX_)
Packit Service 50c9f2
    // IRIX
Packit Service 50c9f2
    cores = (int)sysconf(_SC_NPROC_ONLN);
Packit Service 50c9f2
#else
Packit Service 50c9f2
    // the rest: Linux, Solaris, AIX, Tru64
Packit Service 50c9f2
    cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
Packit Service 50c9f2
#endif
Packit Service 50c9f2
    return cores;  
Packit Service 50c9f2
}
Packit Service 50c9f2