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 QtGui 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 "qlock_p.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | #ifdef QT_NO_QWS_MULTIPROCESS
|
---|
46 |
|
---|
47 | QT_BEGIN_NAMESPACE
|
---|
48 |
|
---|
49 | /* no multiprocess - use a dummy */
|
---|
50 |
|
---|
51 | QLock::QLock(const QString & /*filename*/, char /*id*/, bool /*create*/)
|
---|
52 | : type(Read), data(0)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | QLock::~QLock()
|
---|
57 | {
|
---|
58 | }
|
---|
59 |
|
---|
60 | bool QLock::isValid() const
|
---|
61 | {
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void QLock::lock(Type t)
|
---|
66 | {
|
---|
67 | data = (QLockData *)-1;
|
---|
68 | type = t;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void QLock::unlock()
|
---|
72 | {
|
---|
73 | data = 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool QLock::locked() const
|
---|
77 | {
|
---|
78 | return data;
|
---|
79 | }
|
---|
80 |
|
---|
81 | QT_END_NAMESPACE
|
---|
82 |
|
---|
83 | #else // QT_NO_QWS_MULTIPROCESS
|
---|
84 |
|
---|
85 | #include "qwssignalhandler_p.h"
|
---|
86 |
|
---|
87 | #include <unistd.h>
|
---|
88 | #include <sys/types.h>
|
---|
89 | #if defined(Q_OS_DARWIN)
|
---|
90 | # define Q_NO_SEMAPHORE
|
---|
91 | # include <sys/stat.h>
|
---|
92 | # include <sys/file.h>
|
---|
93 | #else // Q_OS_DARWIN
|
---|
94 | # include <sys/sem.h>
|
---|
95 | # if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) && !defined(QT_LINUXBASE)) \
|
---|
96 | || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) \
|
---|
97 | || defined(Q_OS_BSDI)
|
---|
98 | /* union semun is defined by including <sys/sem.h> */
|
---|
99 | # else
|
---|
100 | /* according to X/OPEN we have to define it ourselves */
|
---|
101 | union semun {
|
---|
102 | int val; /* value for SETVAL */
|
---|
103 | struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
|
---|
104 | unsigned short *array; /* array for GETALL, SETALL */
|
---|
105 | };
|
---|
106 | # endif
|
---|
107 | #endif // Q_OS_DARWIN
|
---|
108 | #include <sys/ipc.h>
|
---|
109 | #include <string.h>
|
---|
110 | #include <errno.h>
|
---|
111 | #include <qdebug.h>
|
---|
112 | #include <signal.h>
|
---|
113 |
|
---|
114 | #include <private/qcore_unix_p.h> // overrides QT_OPEN
|
---|
115 |
|
---|
116 |
|
---|
117 | QT_BEGIN_NAMESPACE
|
---|
118 |
|
---|
119 | #define MAX_LOCKS 200 // maximum simultaneous read locks
|
---|
120 |
|
---|
121 | class QLockData
|
---|
122 | {
|
---|
123 | public:
|
---|
124 | #ifdef Q_NO_SEMAPHORE
|
---|
125 | QByteArray file;
|
---|
126 | #endif // Q_NO_SEMAPHORE
|
---|
127 | int id;
|
---|
128 | int count;
|
---|
129 | bool owned;
|
---|
130 | };
|
---|
131 |
|
---|
132 | /*!
|
---|
133 | \class QLock
|
---|
134 | \brief The QLock class is a wrapper for a System V shared semaphore.
|
---|
135 |
|
---|
136 | \ingroup qws
|
---|
137 |
|
---|
138 | \internal
|
---|
139 |
|
---|
140 | It is used by \l{Qt for Embedded Linux} for synchronizing access to the graphics
|
---|
141 | card and shared memory region between processes.
|
---|
142 | */
|
---|
143 |
|
---|
144 | /*!
|
---|
145 | \enum QLock::Type
|
---|
146 |
|
---|
147 | \value Read
|
---|
148 | \value Write
|
---|
149 | */
|
---|
150 |
|
---|
151 | /*!
|
---|
152 | \fn QLock::QLock(const QString &filename, char id, bool create)
|
---|
153 |
|
---|
154 | Creates a lock. \a filename is the file path of the Unix-domain
|
---|
155 | socket the \l{Qt for Embedded Linux} client is using. \a id is the name of the
|
---|
156 | particular lock to be created on that socket. If \a create is true
|
---|
157 | the lock is to be created (as the Qt for Embedded Linux server does); if \a
|
---|
158 | create is false the lock should exist already (as the Qt for Embedded Linux
|
---|
159 | client expects).
|
---|
160 | */
|
---|
161 |
|
---|
162 | QLock::QLock(const QString &filename, char id, bool create)
|
---|
163 | {
|
---|
164 | data = new QLockData;
|
---|
165 | data->count = 0;
|
---|
166 | #ifdef Q_NO_SEMAPHORE
|
---|
167 | data->file = QString(filename+id).toLocal8Bit().constData();
|
---|
168 | for(int x = 0; x < 2; x++) {
|
---|
169 | data->id = QT_OPEN(data->file, O_RDWR | (x ? O_CREAT : 0), S_IRWXU);
|
---|
170 | if(data->id != -1 || !create) {
|
---|
171 | data->owned = x;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | #else
|
---|
176 | key_t semkey = ftok(filename.toLocal8Bit().constData(), id);
|
---|
177 | data->id = semget(semkey,0,0);
|
---|
178 | data->owned = create;
|
---|
179 | if (create) {
|
---|
180 | semun arg; arg.val = 0;
|
---|
|
---|