1
/****************************************************************************
2
**
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
14
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file.  Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
**
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27
**
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**
32
**
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QWINDOWSPIPEWRITER_P_H
43
#define QWINDOWSPIPEWRITER_P_H
44
45
//
46
//  W A R N I N G
47
//  -------------
48
//
49
// This file is not part of the Qt API.  It exists purely as an
50
// implementation detail.  This header file may change from version to
51
// version without notice, or even be removed.
52
//
53
// We mean it.
54
//
55
56
#include <qdatetime.h>
57
#include <qthread.h>
58
#include <qmutex.h>
59
#include <qwaitcondition.h>
60
#include <qt_windows.h>
61
62
QT_BEGIN_HEADER
63
64
QT_BEGIN_NAMESPACE
65
66
QT_MODULE(Core)
67
68
#ifndef QT_NO_THREAD
69
70
#define SLEEPMIN 10
71
#define SLEEPMAX 500
72
73
class QIncrementalSleepTimer
74
{
75
76
public:
77
    QIncrementalSleepTimer(int msecs)
78
        : totalTimeOut(msecs)
79
        , nextSleep(qMin(SLEEPMIN, totalTimeOut))
80
    {
81
        if (totalTimeOut == -1)
82
            nextSleep = SLEEPMIN;
83
        timer.start();
84
    }
85
86
    int nextSleepTime()
87
    {
88
        int tmp = nextSleep;
89
        nextSleep = qMin(nextSleep * 2, qMin(SLEEPMAX, timeLeft()));
90
        return tmp;
91
    }
92
93
    int timeLeft() const
94
    {
95
        if (totalTimeOut == -1)
96
            return SLEEPMAX;
97
        return qMax(totalTimeOut - timer.elapsed(), 0);
98
    }
99
100
    bool hasTimedOut() const
101
    {
102
        if (totalTimeOut == -1)
103
            return false;
104
        return timer.elapsed() >= totalTimeOut;
105
    }
106
107
    void resetIncrements()
108
    {
109
        nextSleep = qMin(SLEEPMIN, timeLeft());
110
    }
111
112
private:
113
    QTime timer;
114
    int totalTimeOut;
115
    int nextSleep;
116
};
117
118
class Q_CORE_EXPORT QWindowsPipeWriter : public QThread
119
{
120
    Q_OBJECT
121
122
Q_SIGNALS:
123
    void canWrite();
124
125
public:
126
    QWindowsPipeWriter(HANDLE writePipe, QObject * parent = 0);
127
    ~QWindowsPipeWriter();
128
129
    bool waitForWrite(int msecs);
130
    qint64 write(const char *data, qint64 maxlen);
131
132
    qint64 bytesToWrite() const
133
    {
134
        QMutexLocker locker(&lock);
135
        return data.size();
136
    }
137
138
    bool hadWritten() const
139
    {
140
        return hasWritten;
141
    }
142
143
protected:
144
   void run();
145
146
private:
147
    QByteArray data;
148
    QWaitCondition waitCondition;
149
    mutable QMutex lock;
150
    HANDLE writePipe;
151
    volatile bool quitNow;
152
    bool hasWritten;
153
};
154
155
#endif //QT_NO_THREAD
156
157
QT_END_NAMESPACE
158
159
QT_END_HEADER
160
161
#endif // QT_NO_PROCESS