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 <QCoreApplication>
30#include <QDebug>
31#include <QStringList>
32#include <QSystemSemaphore>
33
34int acquire(int count = 1)
35{
36 QSystemSemaphore sem("store");
37
38 for (int i = 0; i < count; ++i) {
39 if (!sem.acquire()) {
40 qWarning() << "Could not acquire" << sem.key();
41 return EXIT_FAILURE;
42 }
43 }
44 qDebug(msg: "done aquiring");
45 return EXIT_SUCCESS;
46}
47
48int release()
49{
50 QSystemSemaphore sem("store");
51 if (!sem.release()) {
52 qWarning() << "Could not release" << sem.key();
53 return EXIT_FAILURE;
54 }
55 qDebug(msg: "done releasing");
56 return EXIT_SUCCESS;
57}
58
59int acquirerelease()
60{
61 QSystemSemaphore sem("store");
62 if (!sem.acquire()) {
63 qWarning() << "Could not acquire" << sem.key();
64 return EXIT_FAILURE;
65 }
66 if (!sem.release()) {
67 qWarning() << "Could not release" << sem.key();
68 return EXIT_FAILURE;
69 }
70 return EXIT_SUCCESS;
71}
72
73int main(int argc, char *argv[])
74{
75 QCoreApplication app(argc, argv);
76
77 QStringList arguments = app.arguments();
78 // binary name is not used here
79 arguments.takeFirst();
80 if (arguments.count() < 1) {
81 qWarning(msg: "Please call the helper with the function to call as argument");
82 return EXIT_FAILURE;
83 }
84 QString function = arguments.takeFirst();
85 if (function == QLatin1String("acquire")) {
86 int count = 1;
87 bool ok = true;
88 if (arguments.count())
89 count = arguments.takeFirst().toInt(ok: &ok);
90 if (!ok)
91 count = 1;
92 return acquire(count);
93 } else if (function == QLatin1String("release")) {
94 return release();
95 } else if (function == QLatin1String("acquirerelease")) {
96 return acquirerelease();
97 } else {
98 qWarning() << "Unknown function" << function;
99 }
100 return EXIT_SUCCESS;
101}
102

source code of qtbase/tests/auto/corelib/kernel/qsystemsemaphore/acquirerelease/main.cpp