Qt 4.8
qlock.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 "qlock_p.h"
43 
44 #ifdef QT_NO_QWS_MULTIPROCESS
45 
47 
48 /* no multiprocess - use a dummy */
49 
50 QLock::QLock(const QString & /*filename*/, char /*id*/, bool /*create*/)
51  : type(Read), data(0)
52 {
53 }
54 
56 {
57 }
58 
59 bool QLock::isValid() const
60 {
61  return true;
62 }
63 
64 void QLock::lock(Type t)
65 {
66  data = (QLockData *)-1;
67  type = t;
68 }
69 
70 void QLock::unlock()
71 {
72  data = 0;
73 }
74 
75 bool QLock::locked() const
76 {
77  return data;
78 }
79 
81 
82 #else // QT_NO_QWS_MULTIPROCESS
83 
84 #if defined(Q_OS_DARWIN)
85 # define QT_NO_SEMAPHORE
86 #endif
87 
88 #include "qwssignalhandler_p.h"
89 
90 #include <unistd.h>
91 #include <sys/types.h>
92 #include <sys/ipc.h>
93 #if defined(QT_NO_SEMAPHORE)
94 # include <sys/stat.h>
95 # include <sys/file.h>
96 #elif !defined(QT_POSIX_IPC)
97 # include <sys/sem.h>
98 #else
99 # include <semaphore.h>
100 #endif
101 #include <string.h>
102 #include <errno.h>
103 
104 #include <private/qcore_unix_p.h> // overrides QT_OPEN
105 
107 
108 #define MAX_LOCKS 200 // maximum simultaneous read locks
109 
111 {
112 public:
113 #if defined(QT_NO_SEMAPHORE) || defined(QT_POSIX_IPC)
115 #endif
116 #if !defined(QT_POSIX_IPC)
117  int id;
118 #else
119  sem_t *id; // Read mode resource counter
120  sem_t *rsem; // Read mode lock
121  sem_t *wsem; // Write mode lock
122 #endif
123  int count;
124  bool owned;
125 };
126 
127 
158 QLock::QLock(const QString &filename, char id, bool create)
159 {
160  data = new QLockData;
161  data->count = 0;
162 #if defined(QT_NO_SEMAPHORE)
163  data->file = filename.toLocal8Bit() + id;
164  for (int x = 0; x < 2; ++x) {
165  data->id = QT_OPEN(data->file.constData(), O_RDWR | (x ? O_CREAT : 0), S_IRWXU);
166  if (data->id != -1 || !create) {
167  data->owned = x;
168  break;
169  }
170  }
171 #elif !defined(QT_POSIX_IPC)
172  key_t semkey = ftok(filename.toLocal8Bit().constData(), id);
173  data->id = semget(semkey, 0, 0);
174  data->owned = create;
175  if (create) {
176  qt_semun arg;
177  arg.val = 0;
178  if (data->id != -1)
179  semctl(data->id, 0, IPC_RMID, arg);
180  data->id = semget(semkey, 1, IPC_CREAT | 0600);
181  arg.val = MAX_LOCKS;
182  semctl(data->id, 0, SETVAL, arg);
183  }
184 #else
185  data->file = filename.toLocal8Bit() + id;
186  data->owned = create;
187 
188  char ids[3] = { 'c', 'r', 'w' };
189  sem_t **sems[3] = { &data->id, &data->rsem, &data->wsem };
190  unsigned short initialValues[3] = { MAX_LOCKS, 1, 1 };
191  for (int i = 0; i < 3; ++i) {
192  QByteArray file = data->file + ids[i];
193  do {
194  *sems[i] = sem_open(file.constData(), 0, 0666, 0);
195  } while (*sems[i] == SEM_FAILED && errno == EINTR);
196  if (create) {
197  if (*sems[i] != SEM_FAILED) {
198  sem_close(*sems[i]);
199  sem_unlink(file.constData());
200  }
201  do {
202  *sems[i] = sem_open(file.constData(), O_CREAT, 0666, initialValues[i]);
203  } while (*sems[i] == SEM_FAILED && errno == EINTR);
204  }
205  }
206 #endif
207  if (!isValid()) {
208  qWarning("QLock::QLock: Cannot %s semaphore %s '%c' (%d, %s)",
209  (create ? "create" : "get"), qPrintable(filename), id,
210  errno, strerror(errno));
211  }
212 
213 #ifndef QT_NO_QWS_SIGNALHANDLER
215 #endif
216 }
217 
222 {
223 #ifndef QT_NO_QWS_SIGNALHANDLER
225 #endif
226 
227  while (locked())
228  unlock();
229 
230 #if defined(QT_NO_SEMAPHORE)
231  if (isValid())
232  QT_CLOSE(data->id);
233 #elif defined(QT_POSIX_IPC)
234  if (data->id != SEM_FAILED)
235  sem_close(data->id);
236  if (data->rsem != SEM_FAILED)
237  sem_close(data->rsem);
238  if (data->wsem != SEM_FAILED)
239  sem_close(data->wsem);
240 #endif
241 
242  if (data->owned) {
243 #if defined(QT_NO_SEMAPHORE)
244  unlink(data->file.constData());
245 #elif !defined(QT_POSIX_IPC)
246  qt_semun semval;
247  semval.val = 0;
248  semctl(data->id, 0, IPC_RMID, semval);
249 #else
250  char ids[3] = { 'c', 'r', 'w' };
251  for (int i = 0; i < 3; ++i) {
252  QByteArray file = data->file + ids[i];
253  sem_unlink(file.constData());
254  }
255 #endif
256  }
257  delete data;
258  data = 0;
259 }
260 
265 bool QLock::isValid() const
266 {
267 #if !defined(QT_POSIX_IPC)
268  return data && data->id != -1;
269 #else
270  return data && data->id != SEM_FAILED && data->rsem != SEM_FAILED && data->wsem != SEM_FAILED;
271 #endif
272 }
273 
286 {
287  if (!isValid())
288  return;
289 
290  if (!data->count) {
291  type = t;
292 
293  int rv;
294 #if defined(QT_NO_SEMAPHORE)
295  int op = type == Write ? LOCK_EX : LOCK_SH;
296 
297  EINTR_LOOP(rv, flock(data->id, op));
298 #elif !defined(QT_POSIX_IPC)
299  sembuf sops;
300  sops.sem_num = 0;
301  sops.sem_op = type == Write ? -MAX_LOCKS : -1;
302  sops.sem_flg = SEM_UNDO;
303 
304  EINTR_LOOP(rv, semop(data->id, &sops, 1));
305 #else
306  if (type == Write) {
307  EINTR_LOOP(rv, sem_wait(data->rsem));
308  if (rv != -1) {
309  EINTR_LOOP(rv, sem_wait(data->wsem));
310  if (rv == -1)
311  sem_post(data->rsem);
312  }
313  } else {
314  EINTR_LOOP(rv, sem_wait(data->wsem));
315  if (rv != -1) {
316  EINTR_LOOP(rv, sem_trywait(data->rsem));
317  if (rv != -1 || errno == EAGAIN) {
318  EINTR_LOOP(rv, sem_wait(data->id));
319  if (rv == -1) {
320  int semval;
321  sem_getvalue(data->id, &semval);
322  if (semval == MAX_LOCKS)
323  sem_post(data->rsem);
324  }
325  }
326  rv = sem_post(data->wsem);
327  }
328  }
329 #endif
330  if (rv == -1) {
331  qDebug("QLock::lock(): %s", strerror(errno));
332  return;
333  }
334  } else if (type == Read && t == Write) {
335  qDebug("QLock::lock(): Attempt to lock for write while locked for read");
336  }
337  data->count++;
338 }
339 
346 {
347  if (!isValid())
348  return;
349 
350  if (data->count > 0) {
351  data->count--;
352  if (!data->count) {
353  int rv;
354 #if defined(QT_NO_SEMAPHORE)
355  EINTR_LOOP(rv, flock(data->id, LOCK_UN));
356 #elif !defined(QT_POSIX_IPC)
357  sembuf sops;
358  sops.sem_num = 0;
359  sops.sem_op = type == Write ? MAX_LOCKS : 1;
360  sops.sem_flg = SEM_UNDO;
361 
362  EINTR_LOOP(rv, semop(data->id, &sops, 1));
363 #else
364  if (type == Write) {
365  sem_post(data->wsem);
366  rv = sem_post(data->rsem);
367  } else {
368  EINTR_LOOP(rv, sem_wait(data->wsem));
369  if (rv != -1) {
370  sem_post(data->id);
371  int semval;
372  sem_getvalue(data->id, &semval);
373  if (semval == MAX_LOCKS)
374  sem_post(data->rsem);
375  rv = sem_post(data->wsem);
376  }
377  }
378 #endif
379  if (rv == -1)
380  qDebug("QLock::unlock(): %s", strerror(errno));
381  }
382  } else {
383  qDebug("QLock::unlock(): Unlock without corresponding lock");
384  }
385 }
386 
391 bool QLock::locked() const
392 {
393  return isValid() && data->count > 0;
394 }
395 
397 
398 #endif // QT_NO_QWS_MULTIPROCESS
#define MAX_LOCKS
Definition: qlock.cpp:108
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
bool owned
Definition: qlock.cpp:124
int id
Definition: qlock.cpp:117
The QString class provides a Unicode character string.
Definition: qstring.h:83
void unlock()
Unlocks the semaphore.
Definition: qlock.cpp:345
#define O_CREAT
static QWSSignalHandler * instance()
Q_CORE_EXPORT void qDebug(const char *,...)
int count
Definition: qlock.cpp:123
bool locked() const
Returns true if the lock is currently held by the current process; otherwise returns false...
Definition: qlock.cpp:391
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Type
Definition: qlock_p.h:68
Q_CORE_EXPORT void qWarning(const char *,...)
void lock(Type type)
Locks the semaphore with a lock of type t.
Definition: qlock.cpp:285
static const char * data(const QByteArray &arr)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define QT_OPEN
Definition: qcore_unix_p.h:186
bool isValid() const
Returns true if the lock constructor was successful; returns false if the lock could not be created o...
Definition: qlock.cpp:265
void removeLock(QLock *lock)
QLock(const QString &filename, char id, bool create=false)
Creates a lock.
Definition: qlock.cpp:158
#define qPrintable(string)
Definition: qglobal.h:1750
~QLock()
Destroys a lock.
Definition: qlock.cpp:221
#define O_RDWR
QByteArray file
Definition: qlock.cpp:114
void addLock(QLock *lock)
int errno
#define QT_CLOSE
Definition: qcore_unix_p.h:304