Qt 4.8
Public Types | Public Functions | Private Functions | Properties | List of all members
QWSLock Class Reference

#include <qwslock_p.h>

Public Types

enum  LockType { BackingStore, Communication, RegionEvent }
 

Public Functions

bool hasLock (LockType type)
 
int id () const
 
bool lock (LockType type, int timeout=-1)
 
 QWSLock (int lockId=-1)
 
void unlock (LockType type)
 
bool wait (LockType type, int timeout=-1)
 
 ~QWSLock ()
 

Private Functions

bool down (unsigned short semNum, int timeout)
 
int getValue (unsigned short semNum) const
 
bool up (unsigned short semNum)
 

Properties

int lockCount [2]
 
int semId
 

Detailed Description

Definition at line 66 of file qwslock_p.h.

Enumerations

◆ LockType

Enumerator
BackingStore 
Communication 
RegionEvent 

Definition at line 69 of file qwslock_p.h.

Constructors and Destructors

◆ QWSLock()

QWSLock::QWSLock ( int  lockId = -1)

Definition at line 78 of file qwslock.cpp.

78  : 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 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int lockCount[2]
Definition: qwslock_p.h:86
#define O_CREAT
QStringList keys
static QWSSignalHandler * instance()
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)
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

◆ ~QWSLock()

QWSLock::~QWSLock ( )

Definition at line 129 of file qwslock.cpp.

Referenced by QWSSignalHandler::clear().

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 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void removeWSLock(QWSLock *wslock)
QStringList keys
void unlock(LockType type)
Definition: qwslock.cpp:250
static QWSSignalHandler * instance()
bool hasLock(LockType type)
Definition: qwslock.cpp:242
int semId
Definition: qwslock_p.h:85
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)...

Functions

◆ down()

bool QWSLock::down ( unsigned short  semNum,
int  timeout 
)
private

Definition at line 185 of file qwslock.cpp.

Referenced by id(), lock(), unlock(), and wait().

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 }
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
Q_CORE_EXPORT void qDebug(const char *,...)
int semId
Definition: qwslock_p.h:85
int errno

◆ getValue()

int QWSLock::getValue ( unsigned short  semNum) const
private

Definition at line 210 of file qwslock.cpp.

Referenced by hasLock(), and id().

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 }
Q_CORE_EXPORT void qDebug(const char *,...)
int semId
Definition: qwslock_p.h:85
int errno

◆ hasLock()

bool QWSLock::hasLock ( LockType  type)

Definition at line 242 of file qwslock.cpp.

Referenced by ~QWSLock().

243 {
244  if (type == RegionEvent)
245  return getValue(type) == 0;
246 
247  return lockCount[type] > 0;
248 }
int type
Definition: qmetatype.cpp:239
int lockCount[2]
Definition: qwslock_p.h:86
int getValue(unsigned short semNum) const
Definition: qwslock.cpp:210

◆ id()

int QWSLock::id ( ) const
inline

Definition at line 78 of file qwslock_p.h.

Referenced by QWSSharedMemSurface::permanentState(), QWSDisplay::setIdentity(), and QWSMemorySurface::setLock().

78 { return semId; }
int semId
Definition: qwslock_p.h:85

◆ lock()

bool QWSLock::lock ( LockType  type,
int  timeout = -1 
)

Definition at line 224 of file qwslock.cpp.

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 }
int type
Definition: qmetatype.cpp:239
bool down(unsigned short semNum, int timeout)
Definition: qwslock.cpp:185
int lockCount[2]
Definition: qwslock_p.h:86
bool up(unsigned short semNum)
Definition: qwslock.cpp:160

◆ unlock()

void QWSLock::unlock ( LockType  type)

Definition at line 250 of file qwslock.cpp.

Referenced by wait(), and ~QWSLock().

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 }
int type
Definition: qmetatype.cpp:239
bool down(unsigned short semNum, int timeout)
Definition: qwslock.cpp:185
int lockCount[2]
Definition: qwslock_p.h:86
bool up(unsigned short semNum)
Definition: qwslock.cpp:160

◆ up()

bool QWSLock::up ( unsigned short  semNum)
private

Definition at line 160 of file qwslock.cpp.

Referenced by id(), lock(), and unlock().

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 }
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
Q_CORE_EXPORT void qDebug(const char *,...)
int semId
Definition: qwslock_p.h:85
int errno

◆ wait()

bool QWSLock::wait ( LockType  type,
int  timeout = -1 
)

Definition at line 266 of file qwslock.cpp.

267 {
268  bool ok = down(type, timeout);
269  if (ok)
270  unlock(type);
271  return ok;
272 }
int type
Definition: qmetatype.cpp:239
bool down(unsigned short semNum, int timeout)
Definition: qwslock.cpp:185
void unlock(LockType type)
Definition: qwslock.cpp:250

Properties

◆ lockCount

int QWSLock::lockCount[2]
private

Definition at line 86 of file qwslock_p.h.

Referenced by hasLock(), lock(), QWSLock(), and unlock().

◆ semId

int QWSLock::semId
private

Definition at line 85 of file qwslock_p.h.

Referenced by down(), getValue(), id(), QWSLock(), up(), and ~QWSLock().


The documentation for this class was generated from the following files: