Qt 4.8
qwindowspipewriter.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qwindowspipewriter_p.h"
43 #include <string.h>
44 
46 
47 #ifndef QT_NO_THREAD
48 
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 
65 {
66  lock.lock();
67  quitNow = true;
69  lock.unlock();
70  if (!wait(30000))
71  terminate();
72 #if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
73  CloseHandle(writePipe);
74 #endif
75 }
76 
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 
92 {
93  if (!isRunning())
94  return -1;
95 
96  QMutexLocker locker(&lock);
97  data.append(QByteArray(ptr, maxlen));
99  return maxlen;
100 }
101 
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)) {
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 
qint64 write(const char *data, qint64 maxlen)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void lock()
Locks the mutex.
Definition: qmutex.cpp:151
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray & append(char c)
Appends the character ch to this byte array.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static void msleep(unsigned long)
Forces the current thread to sleep for msecs milliseconds.
Q_CORE_EXPORT void qDebug(const char *,...)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QWindowsPipeWriter(HANDLE writePipe, QObject *parent=0)
#define emit
Definition: qobjectdefs.h:76
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
const T * ptr(const T &t)
__int64 qint64
Definition: qglobal.h:942
void * HANDLE
Definition: qnamespace.h:1671
void unlock()
Unlocks the mutex.
Definition: qmutex.cpp:296
bool wait(QMutex *mutex, unsigned long time=ULONG_MAX)
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
QWaitCondition waitCondition
void bytesWritten(qint64 bytes)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
bool waitForWrite(int msecs)
void terminate()
Terminates the execution of the thread.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
void run()
The starting point for the thread.
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
bool isRunning() const
Returns true if the thread is running; otherwise returns false.
Definition: qthread.cpp:494
QByteArray & remove(int index, int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
#define forever
This macro is provided for convenience for writing infinite loops.
Definition: qglobal.h:2452