QtBase
v6.3.1
tests
auto
corelib
kernel
qsharedmemory
producerconsumer
main.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the test suite of the Qt Toolkit.
7
**
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
16
**
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24
**
25
** $QT_END_LICENSE$
26
**
27
****************************************************************************/
28
29
#include <QSharedMemory>
30
#include <QStringList>
31
#include <QDebug>
32
#include <QTest>
33
#include <stdio.h>
34
35
void
set
(
QSharedMemory
&sm,
int
pos
,
char
value
)
36
{
37
((
char
*)sm.
data
())[
pos
] =
value
;
38
}
39
40
QChar
get
(
QSharedMemory
&sm,
int
i)
41
{
42
return
QChar::fromLatin1
(((
char
*)sm.
data
())[
i
]);
43
}
44
45
int
readonly_segfault
()
46
{
47
QSharedMemory
sharedMemory;
48
sharedMemory.
setKey
(
"readonly_segfault"
);
49
sharedMemory.
create
(1024,
QSharedMemory::ReadOnly
);
50
sharedMemory.
lock
();
51
set
(sharedMemory, 0,
'a'
);
52
sharedMemory.
unlock
();
53
return
EXIT_SUCCESS;
54
}
55
56
int
producer
()
57
{
58
QSharedMemory
producer
;
59
producer
.setKey(
"market"
);
60
61
int
size
= 1024;
62
if
(!
producer
.create(
size
)) {
63
if
(
producer
.error() ==
QSharedMemory::AlreadyExists
) {
64
if
(!
producer
.attach()) {
65
qWarning
() <<
"Could not attach to"
<<
producer
.key();
66
return
EXIT_FAILURE
;
67
}
68
}
else
{
69
qWarning
() <<
"Could not create"
<<
producer
.key();
70
return
EXIT_FAILURE
;
71
}
72
}
73
// tell parent we're ready
74
//qDebug("producer created and attached");
75
puts(
""
);
76
fflush(stdout);
77
78
if
(!
producer
.lock()) {
79
qWarning
() <<
"Could not lock"
<<
producer
.key();
80
return
EXIT_FAILURE
;
81
}
82
set
(
producer
, 0,
'Q'
);
83
if
(!
producer
.unlock()) {
84
qWarning
() <<
"Could not lock"
<<
producer
.key();
85
return
EXIT_FAILURE
;
86
}
87
88
int
i
= 0;
89
while
(
i
< 5) {
90
if
(!
producer
.lock()) {
91
qWarning
() <<
"Could not lock"
<<
producer
.key();
92
return
EXIT_FAILURE
;
93
}
94
if
(
get
(
producer
, 0) ==
'Q'
) {
95
if
(!
producer
.unlock()) {
96
qWarning
() <<
"Could not unlock"
<<
producer
.key();
97
return
EXIT_FAILURE
;
98
}
99
QTest::qSleep
(1);
100
continue
;
101
}
102
//qDebug() << "producer:" << i);
103
++
i
;
104
set
(
producer
, 0,
'Q'
);
105
if
(!
producer
.unlock()) {
106
qWarning
() <<
"Could not unlock"
<<
producer
.key();
107
return
EXIT_FAILURE
;
108
}
109
QTest::qSleep
(1);
110
}
111
if
(!
producer
.lock()) {
112
qWarning
() <<
"Could not lock"
<<
producer
.key();
113
return
EXIT_FAILURE
;
114
}
115
set
(
producer
, 0,
'E'
);
116
if
(!
producer
.unlock()) {
117
qWarning
() <<
"Could not unlock"
<<
producer
.key();
118
return
EXIT_FAILURE
;
119
}
120
121
//qDebug("producer done");
122
123
// Sleep for a bit to let all consumers exit
124
getchar();
125
return
EXIT_SUCCESS;
126
}
127
128
int
consumer
()
129
{
130
QSharedMemory
consumer
;
131
consumer
.setKey(
"market"
);
132
133
//qDebug("consumer starting");
134
int
tries = 0;
135
while
(!
consumer
.attach()) {
136
if
(tries == 5000) {
137
qWarning
() <<
"consumer exiting, waiting too long"
;
138
return
EXIT_FAILURE
;
139
}
140
++tries;
141
QTest::qSleep
(1);
142
}
143
//qDebug("consumer attached");
144
145
146
int
i
= 0;
147
while
(
true
) {
148
if
(!
consumer
.lock()) {
149
qWarning
() <<
"Could not lock"
<<
consumer
.key();
150
return
EXIT_FAILURE
;
151
}
152
if
(
get
(
consumer
, 0) ==
'Q'
) {
153
set
(
consumer
, 0, ++
i
);
154
//qDebug() << "consumer sets" << i;
155
}
156
if
(
get
(
consumer
, 0) ==
'E'
) {
157
if
(!
consumer
.unlock()) {
158
qWarning
() <<
"Could not unlock"
<<
consumer
.key();
159
return
EXIT_FAILURE
;
160
}
161
break
;
162
}
163
if
(!
consumer
.unlock()) {
164
qWarning
() <<
"Could not unlock"
<<
consumer
.key();
165
return
EXIT_FAILURE
;
166
}
167
QTest::qSleep
(10);
168
}
169
170
//qDebug("consumer detaching");
171
if
(!
consumer
.detach()) {
172
qWarning
() <<
"Could not detach"
<<
consumer
.key();
173
return
EXIT_FAILURE
;
174
}
175
return
EXIT_SUCCESS;
176
}
177
178
int
main
(
int
argc,
char
*argv[])
179
{
180
QCoreApplication
app
(argc, argv);
181
182
QStringList
arguments
=
app
.
arguments
();
183
if
(
app
.
arguments
().count() != 2) {
184
qWarning
(
"Please call the helper with the function to call as argument"
);
185
return
EXIT_FAILURE
;
186
}
187
QString
function
=
arguments
.
at
(1);
188
if
(
function
==
QLatin1String
(
"readonly_segfault"
))
189
return
readonly_segfault
();
190
else
if
(
function
==
QLatin1String
(
"producer"
))
191
return
producer
();
192
else
if
(
function
==
QLatin1String
(
"consumer"
))
193
return
consumer
();
194
else
195
qWarning
() <<
"Unknown function"
<<
arguments
.
at
(1);
196
197
return
EXIT_SUCCESS;
198
}
i
small capitals from c petite p scientific i
[1]
Definition:
afcover.h:80
QChar
The QChar class provides a 16-bit Unicode character.
Definition:
qchar.h:84
QChar::fromLatin1
static constexpr QChar fromLatin1(char c) noexcept
Definition:
qchar.h:492
QCoreApplication
The QCoreApplication class provides an event loop for Qt applications without UI.
Definition:
qcoreapplication.h:78
QCoreApplication::arguments
static QStringList arguments()
Definition:
qcoreapplication.cpp:2481
QLatin1String
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition:
qstring.h:84
QList::at
const_reference at(qsizetype i) const noexcept
Definition:
qlist.h:457
QSharedMemory
The QSharedMemory class provides access to a shared memory segment.
Definition:
qsharedmemory.h:62
QSharedMemory::create
bool create(qsizetype size, AccessMode mode=ReadWrite)
Definition:
qsharedmemory.cpp:400
QSharedMemory::data
void * data()
Definition:
qsharedmemory.cpp:538
QSharedMemory::unlock
bool unlock()
Definition:
qsharedmemory.cpp:605
QSharedMemory::lock
bool lock()
Definition:
qsharedmemory.cpp:580
QSharedMemory::ReadOnly
@ ReadOnly
Definition:
qsharedmemory.h:71
QSharedMemory::AlreadyExists
@ AlreadyExists
Definition:
qsharedmemory.h:81
QSharedMemory::setKey
void setKey(const QString &key)
Definition:
qsharedmemory.cpp:271
QString
The QString class provides a Unicode character string.
Definition:
qstring.h:388
QStringList
The QStringList class provides a list of strings.
main
int main(int argc, char **argv)
Definition:
main.cpp:1
arguments
QList< QVariant > arguments
Definition:
doc_src_qsignalspy.cpp:59
EXIT_FAILURE
#define EXIT_FAILURE
Definition:
jerror.c:35
QTest::qSleep
Q_CORE_EXPORT void qSleep(int ms)
Definition:
qtestsupport_core.cpp:65
value
EGLOutputLayerEXT EGLint EGLAttrib value
Definition:
qeglstreamconvenience_p.h:82
qWarning
#define qWarning
Definition:
qlogging.h:179
size
GLenum GLuint GLintptr GLsizeiptr size
[1]
Definition:
qopengles2ext.h:660
pos
pos
[7]
Definition:
src_corelib_io_qsettings.cpp:104
app
QApplication app(argc, argv)
[0]
consumer
int consumer()
Definition:
main.cpp:128
producer
int producer()
Definition:
main.cpp:56
get
QChar get(QSharedMemory &sm, int i)
Definition:
main.cpp:40
readonly_segfault
int readonly_segfault()
Definition:
main.cpp:45
set
void set(QSharedMemory &sm, int pos, char value)
Definition:
main.cpp:35
Generated by
1.9.1