File indexing completed on 2024-12-22 04:35:01
0001 /* 0002 SPDX-FileCopyrightText: 2009 Nokia Corporation and /or its subsidiary(-ies). 0003 Contact: Qt Software Information (qt-info@nokia.com) 0004 0005 This file is part of the QtCore module of the Qt Toolkit. 0006 0007 $QT_BEGIN_LICENSE:LGPL$ 0008 Commercial Usage 0009 Licensees holding valid Qt Commercial licenses may use this file in 0010 accordance with the Qt Commercial License Agreement provided with the 0011 Software or, alternatively, in accordance with the terms contained in 0012 a written agreement between you and Nokia. 0013 0014 GNU Lesser General Public License Usage 0015 Alternatively, this file may be used under the terms of the GNU Lesser 0016 General Public License version 2.1 as published by the Free Software 0017 Foundation and appearing in the file LICENSE.LGPL included in the 0018 packaging of this file. Please review the following information to 0019 ensure the GNU Lesser General Public License version 2.1 requirements 0020 will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 0021 0022 In addition, as a special exception, Nokia gives you certain 0023 additional rights. These rights are described in the Nokia Qt LGPL 0024 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 0025 package. 0026 0027 GNU General Public License Usage 0028 Alternatively, this file may be used under the terms of the GNU 0029 General Public License version 3.0 as published by the Free Software 0030 Foundation and appearing in the file LICENSE.GPL included in the 0031 packaging of this file. Please review the following information to 0032 ensure the GNU General Public License version 3.0 requirements will be 0033 met: https://www.gnu.org/licenses/gpl-3.0.html. 0034 0035 If you are unsure which license is appropriate for your use, please 0036 contact the sales department at qt-sales@nokia.com. 0037 $QT_END_LICENSE$ 0038 0039 */ 0040 0041 #ifndef QWINDOWSPIPEWRITER_P_H 0042 #define QWINDOWSPIPEWRITER_P_H 0043 0044 // 0045 // W A R N I N G 0046 // ------------- 0047 // 0048 // This file is not part of the Qt API. It exists purely as an 0049 // implementation detail. This header file may change from version to 0050 // version without notice, or even be removed. 0051 // 0052 // We mean it. 0053 // 0054 0055 #include <QDateTime> 0056 #include <QThread> 0057 #include <QMutex> 0058 #include <QWaitCondition> 0059 #include <qt_windows.h> 0060 0061 QT_BEGIN_HEADER 0062 0063 QT_BEGIN_NAMESPACE 0064 0065 QT_MODULE(Core) 0066 0067 #ifndef QT_NO_THREAD 0068 0069 #define SLEEPMIN 10 0070 #define SLEEPMAX 500 0071 0072 class QIncrementalSleepTimer 0073 { 0074 0075 public: 0076 QIncrementalSleepTimer(int msecs) 0077 : totalTimeOut(msecs) 0078 , nextSleep(qMin(SLEEPMIN, totalTimeOut)) 0079 { 0080 if (totalTimeOut == -1) 0081 nextSleep = SLEEPMIN; 0082 timer.start(); 0083 } 0084 0085 int nextSleepTime() 0086 { 0087 int tmp = nextSleep; 0088 nextSleep = qMin(nextSleep * 2, qMin(SLEEPMAX, timeLeft())); 0089 return tmp; 0090 } 0091 0092 int timeLeft() const 0093 { 0094 if (totalTimeOut == -1) 0095 return SLEEPMAX; 0096 return qMax(totalTimeOut - timer.elapsed(), 0); 0097 } 0098 0099 bool hasTimedOut() const 0100 { 0101 if (totalTimeOut == -1) 0102 return false; 0103 return timer.elapsed() >= totalTimeOut; 0104 } 0105 0106 void resetIncrements() 0107 { 0108 nextSleep = qMin(SLEEPMIN, timeLeft()); 0109 } 0110 0111 private: 0112 QTime timer; 0113 int totalTimeOut; 0114 int nextSleep; 0115 }; 0116 0117 class Q_CORE_EXPORT QWindowsPipeWriter : public QThread 0118 { 0119 Q_OBJECT 0120 0121 Q_SIGNALS: 0122 void canWrite(); 0123 0124 public: 0125 QWindowsPipeWriter(HANDLE writePipe, QObject * parent = 0); 0126 ~QWindowsPipeWriter(); 0127 0128 bool waitForWrite(int msecs); 0129 qint64 write(const char *data, qint64 maxlen); 0130 0131 qint64 bytesToWrite() const 0132 { 0133 QMutexLocker locker(&lock); 0134 return data.size(); 0135 } 0136 0137 bool hadWritten() const 0138 { 0139 return hasWritten; 0140 } 0141 0142 protected: 0143 void run(); 0144 0145 private: 0146 QByteArray data; 0147 QWaitCondition waitCondition; 0148 mutable QMutex lock; 0149 HANDLE writePipe; 0150 volatile bool quitNow; 0151 bool hasWritten; 0152 }; 0153 0154 #endif //QT_NO_THREAD 0155 0156 QT_END_NAMESPACE 0157 0158 QT_END_HEADER 0159 0160 #endif // QT_NO_PROCESS