~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/io/qwindowspipewriter.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
 
 
45
QT_BEGIN_NAMESPACE
 
46
 
 
47
#ifndef QT_NO_THREAD
 
48
 
 
49
QWindowsPipeWriter::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
 
 
64
QWindowsPipeWriter::~QWindowsPipeWriter()
 
65
{
 
66
    lock.lock();
 
67
    quitNow = true;
 
68
    waitCondition.wakeOne();
 
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
 
 
77
bool 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
 
 
91
qint64 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
 
 
102
void 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
 
 
171
QT_END_NAMESPACE