Qt 4.8
qwslock.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 QtGui 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 "qwslock_p.h"
43 
44 #ifndef QT_NO_QWS_MULTIPROCESS
45 
46 #include "qwssignalhandler_p.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <string.h>
52 #include <sys/types.h>
53 #include <sys/ipc.h>
54 #ifndef QT_POSIX_IPC
55 #include <sys/sem.h>
56 #endif
57 #include <sys/time.h>
58 #include <time.h>
59 #ifdef Q_OS_LINUX
60 #include <linux/version.h>
61 #endif
62 #include <unistd.h>
63 
64 #include <private/qcore_unix_p.h>
65 
67 
68 #ifdef QT_NO_SEMAPHORE
69 #error QWSLock currently requires semaphores
70 #endif
71 
72 #ifdef QT_POSIX_IPC
73 #include <QtCore/QAtomicInt>
74 
75 static QBasicAtomicInt localUniqueId = Q_BASIC_ATOMIC_INITIALIZER(1);
76 #endif
77 
78 QWSLock::QWSLock(int id) : semId(id)
79 {
80  static unsigned short initialValues[3] = { 1, 1, 0 };
81 
82 #ifndef QT_NO_QWS_SIGNALHANDLER
84 #endif
85 
86 #ifndef QT_POSIX_IPC
87  if (semId == -1) {
88  semId = semget(IPC_PRIVATE, 3, IPC_CREAT | 0666);
89  if (semId == -1) {
90  perror("QWSLock::QWSLock");
91  qFatal("Unable to create semaphore");
92  }
93 
94  qt_semun semval;
95  semval.array = initialValues;
96  if (semctl(semId, 0, SETALL, semval) == -1) {
97  perror("QWSLock::QWSLock");
98  qFatal("Unable to initialize semaphores");
99  }
100  }
101 #else
102  sems[0] = sems[1] = sems[2] = SEM_FAILED;
103  owned = false;
104 
105  if (semId == -1) {
106  // ### generate really unique IDs
107  semId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
108  owned = true;
109  }
110 
111  QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
112  QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
113  for (int i = 0; i < 3; ++i) {
114  if (owned)
115  sem_unlink(keys[i].constData());
116  do {
117  sems[i] = sem_open(keys[i].constData(), (owned ? O_CREAT : 0), 0666, initialValues[i]);
118  } while (sems[i] == SEM_FAILED && errno == EINTR);
119  if (sems[i] == SEM_FAILED) {
120  perror("QWSLock::QWSLock");
121  qFatal("Unable to %s semaphore", (owned ? "create" : "open"));
122  }
123  }
124 #endif
125 
126  lockCount[0] = lockCount[1] = 0;
127 }
128 
130 {
131 #ifndef QT_NO_QWS_SIGNALHANDLER
133 #endif
134 
135  if (semId != -1) {
136 #ifndef QT_POSIX_IPC
137  qt_semun semval;
138  semval.val = 0;
139  semctl(semId, 0, IPC_RMID, semval);
140  semId = -1;
141 #else
142  // emulate the SEM_UNDO behavior for the BackingStore lock
143  while (hasLock(BackingStore))
145 
146  QByteArray pfx = "/qwslock_" + QByteArray::number(semId, 16) + '_';
147  QByteArray keys[3] = { pfx + "BackingStore", pfx + "Communication", pfx + "RegionEvent" };
148  for (int i = 0; i < 3; ++i) {
149  if (sems[i] != SEM_FAILED) {
150  sem_close(sems[i]);
151  sems[i] = SEM_FAILED;
152  }
153  if (owned)
154  sem_unlink(keys[i].constData());
155  }
156 #endif
157  }
158 }
159 
160 bool QWSLock::up(unsigned short semNum)
161 {
162  int ret;
163 
164 #ifndef QT_POSIX_IPC
165  sembuf sops = { semNum, 1, 0 };
166  // As the BackingStore lock is a mutex, and only one process may own
167  // the lock, it's safe to use SEM_UNDO. On the other hand, the
168  // Communication lock is locked by the client but unlocked by the
169  // server and therefore can't use SEM_UNDO.
170  if (semNum == BackingStore)
171  sops.sem_flg |= SEM_UNDO;
172 
173  EINTR_LOOP(ret, semop(semId, &sops, 1));
174 #else
175  ret = sem_post(sems[semNum]);
176 #endif
177  if (ret == -1) {
178  qDebug("QWSLock::up(): %s", strerror(errno));
179  return false;
180  }
181 
182  return true;
183 }
184 
185 bool QWSLock::down(unsigned short semNum, int)
186 {
187  int ret;
188 
189 #ifndef QT_POSIX_IPC
190  sembuf sops = { semNum, -1, 0 };
191  // As the BackingStore lock is a mutex, and only one process may own
192  // the lock, it's safe to use SEM_UNDO. On the other hand, the
193  // Communication lock is locked by the client but unlocked by the
194  // server and therefore can't use SEM_UNDO.
195  if (semNum == BackingStore)
196  sops.sem_flg |= SEM_UNDO;
197 
198  EINTR_LOOP(ret, semop(semId, &sops, 1));
199 #else
200  EINTR_LOOP(ret, sem_wait(sems[semNum]));
201 #endif
202  if (ret == -1) {
203  qDebug("QWSLock::down(): %s", strerror(errno));
204  return false;
205  }
206 
207  return true;
208 }
209 
210 int QWSLock::getValue(unsigned short semNum) const
211 {
212  int ret;
213 #ifndef QT_POSIX_IPC
214  ret = semctl(semId, semNum, GETVAL, 0);
215 #else
216  if (sem_getvalue(sems[semNum], &ret) == -1)
217  ret = -1;
218 #endif
219  if (ret == -1)
220  qDebug("QWSLock::getValue(): %s", strerror(errno));
221  return ret;
222 }
223 
224 bool QWSLock::lock(LockType type, int timeout)
225 {
226  if (type == RegionEvent)
227  return up(type);
228 
229  if (lockCount[type] > 0) {
230  ++lockCount[type];
231  return true;
232  }
233 
234  if (down(type, timeout)) {
235  ++lockCount[type];
236  return true;
237  }
238 
239  return false;
240 }
241 
243 {
244  if (type == RegionEvent)
245  return getValue(type) == 0;
246 
247  return lockCount[type] > 0;
248 }
249 
251 {
252  if (type == RegionEvent) {
253  down(type, -1);
254  return;
255  }
256 
257  if (lockCount[type] > 0) {
258  --lockCount[type];
259  if (lockCount[type] > 0)
260  return;
261  }
262 
263  up(type);
264 }
265 
266 bool QWSLock::wait(LockType type, int timeout)
267 {
268  bool ok = down(type, timeout);
269  if (ok)
270  unlock(type);
271  return ok;
272 }
273 
275 
276 #endif // QT_NO_QWS_MULTIPROCESS
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool wait(LockType type, int timeout=-1)
Definition: qwslock.cpp:266
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void removeWSLock(QWSLock *wslock)
~QWSLock()
Definition: qwslock.cpp:129
bool down(unsigned short semNum, int timeout)
Definition: qwslock.cpp:185
int lockCount[2]
Definition: qwslock_p.h:86
#define Q_BASIC_ATOMIC_INITIALIZER(a)
Definition: qbasicatomic.h:218
#define O_CREAT
QStringList keys
void unlock(LockType type)
Definition: qwslock.cpp:250
static QWSSignalHandler * instance()
Q_CORE_EXPORT void qDebug(const char *,...)
bool hasLock(LockType type)
Definition: qwslock.cpp:242
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QWSLock(int lockId=-1)
Definition: qwslock.cpp:78
int getValue(unsigned short semNum) const
Definition: qwslock.cpp:210
bool up(unsigned short semNum)
Definition: qwslock.cpp:160
int semId
Definition: qwslock_p.h:85
Q_CORE_EXPORT void qFatal(const char *,...)
unsigned short ushort
Definition: qglobal.h:995
void addWSLock(QWSLock *wslock)
bool lock(LockType type, int timeout=-1)
Definition: qwslock.cpp:224
unsigned short * array
Definition: qcore_unix_p.h:357
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
int errno