/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the core module of the Qt Toolkit.
**
** Licensees holding valid Qt Preview licenses may use this file in
** accordance with the Qt Preview License Agreement provided with the
** Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QTHREAD_H
#define QTHREAD_H

#include <qobject.h>

#include <limits.h>

class QThreadData;
class QThreadPrivate;

class Q_CORE_EXPORT QThread : public QObject
{
    friend class QThreadData;

    Q_OBJECT
    Q_DECLARE_PRIVATE(QThread)
    Q_DISABLE_COPY(QThread)

public:
    static Qt::HANDLE currentThread();
    static QThread *currentQThread(); // better name?

    static void initialize();
    static void cleanup();

    QThread();
    virtual ~QThread();

    enum Priority {
        IdlePriority,

        LowestPriority,
        LowPriority,
        NormalPriority,
        HighPriority,
        HighestPriority,

        TimeCriticalPriority,

        InheritPriority
    };

    bool isFinished() const;
    bool isRunning() const;

    void setStackSize(uint stackSize);
    uint stackSize() const;

    void exit(int retcode = 0);

public slots:
    void start(Priority = InheritPriority);
    void terminate();
    void quit();

public:
    // default argument causes thread to block indefinately
    bool wait(unsigned long time = ULONG_MAX);

signals:
    void started();
    void finished();
    void terminated();

protected:
    virtual void run() = 0;

    static void sleep(unsigned long);
    static void msleep(unsigned long);
    static void usleep(unsigned long);
#ifdef QT_COMPAT
public:
    inline QT_COMPAT bool finished() const { return isFinished(); }
    inline QT_COMPAT bool running() const { return isRunning(); }
#endif
};

#endif // QTHREAD_H
