source: trunk/src/corelib/io/qwindowspipewriter.cpp@ 1168

Last change on this file since 1168 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qwindowspipewriter_p.h"
43#include <string.h>
44
45QT_BEGIN_NAMESPACE
46
47#ifndef QT_NO_THREAD
48
49QWindowsPipeWriter::QWindowsPipeWriter(HANDLE pipe, QObject * parent)
50 : QThread(parent),
51 writePipe(INVALID_HANDLE_VALUE),
52 quitNow(false),
53 hasWritten(false)
54{
55#if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
56 DuplicateHandle(GetCurrentProcess(), pipe, GetCurrentProcess(),
57 &writePipe, 0, FALSE, DUPLICATE_SAME_ACCESS);
58#else
59 Q_UNUSED(pipe);
60 writePipe = GetCurrentProcess();
61#endif
62}
63
64QWindowsPipeWriter::~QWindowsPipeWriter()
65{
66 lock.lock();
67 quitNow = true;
68 waitCondition.wakeOne();
69 lock.unlock();
70 if (!wait(100))
71 terminate();
72#if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
73 CloseHandle(writePipe);
74#endif
75}
76
77bool QWindowsPipeWriter::waitForWrite(int msecs)
78{
79 QMutexLocker locker(&lock);
80 bool hadWritten = hasWritten;
81 hasWritten = false;
82 if (hadWritten)
83 return true;
84 if (!waitCondition.wait(&lock, msecs))
85 return false;
86 hadWritten = hasWritten;
87 hasWritten = false;
88 return hadWritten;
89}
90
91qint64 QWindowsPipeWriter::write(const char *ptr, qint64 maxlen)
92{
93 if (!isRunning())
94 return -1;
95
96 QMutexLocker locker(&lock);
97 data.append(QByteArray(ptr, maxlen));
98 waitCondition.wakeOne();
99 return maxlen;
100}
101
102void QWindowsPipeWriter::run()
103{
104 OVERLAPPED overl;
105 memset(&overl, 0, sizeof overl);
106 overl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
107 forever {
108 lock.lock();
109 while(data.isEmpty() && (!quitNow)) {
110 waitCondition.wakeOne();
111 waitCondition.wait(&lock);
112 }
113
114 if (quitNow) {
115 lock.unlock();
116 quitNow = false;
117 break;
118 }
119
120 QByteArray copy = data;
121
122 lock.unlock();
123
124 const char *ptrData = copy.data();
125 qint64 maxlen = copy.size();
126 qint64 totalWritten = 0;
127 overl.Offset = 0;
128 overl.OffsetHigh = 0;
129 while ((!quitNow) && totalWritten < maxlen) {
130 DWORD written = 0;
131 if (!WriteFile(writePipe, ptrData + totalWritten,
132 maxlen - totalWritten, &written, &overl)) {
133
134 if (GetLastError() == 0xE8/*NT_STATUS_INVALID_USER_BUFFER*/) {
135 // give the os a rest
136 msleep(100);
137 continue;
138 }
139#ifndef Q_OS_WINCE
140 if (GetLastError() == ERROR_IO_PENDING) {
141 if (!GetOverlappedResult(writePipe, &overl, &written, TRUE)) {
142 CloseHandle(overl.hEvent);
143 return;
144 }
145 } else {
146 CloseHandle(overl.hEvent);
147 return;
148 }
149#else
150 return;
151#endif
152 }
153 totalWritten += written;
154#if defined QPIPEWRITER_DEBUG
155 qDebug("QWindowsPipeWriter::run() wrote %d %d/%d bytes",
156 written, int(totalWritten), int(maxlen));
157#endif
158 lock.lock();
159 data.remove(0, written);
160 hasWritten = true;
161 lock.unlock();
162 }
163 emit bytesWritten(totalWritten);
164 emit canWrite();
165 }
166 CloseHandle(overl.hEvent);
167}
168
169#endif //QT_NO_THREAD
170
171QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.