Qt 4.8
Public Functions | Public Variables | List of all members
QSystemSemaphorePrivate Class Reference

#include <qsystemsemaphore_p.h>

Public Functions

void cleanHandle ()
 
HANDLE handle (QSystemSemaphore::AccessMode mode=QSystemSemaphore::Open)
 
QString makeKeyFileName () const
 
bool modifySemaphore (int count)
 
 QSystemSemaphorePrivate ()
 
void setErrorString (const QString &function)
 

Public Variables

QSystemSemaphore::SystemSemaphoreError error
 
QString errorString
 
QString fileName
 
int initialValue
 
QString key
 
HANDLE semaphore
 
HANDLE semaphoreLock
 

Detailed Description

Definition at line 74 of file qsystemsemaphore_p.h.

Constructors and Destructors

◆ QSystemSemaphorePrivate()

QSystemSemaphorePrivate::QSystemSemaphorePrivate ( )

Definition at line 71 of file qsystemsemaphore_unix.cpp.

71  :
72 #ifndef QT_POSIX_IPC
73  unix_key(-1), semaphore(-1), createdFile(false),
74 #else
75  semaphore(SEM_FAILED),
76 #endif
77  createdSemaphore(false), error(QSystemSemaphore::NoError)
78 {
79 }
QSystemSemaphore::SystemSemaphoreError error

Functions

◆ cleanHandle()

void QSystemSemaphorePrivate::cleanHandle ( )
Warning
This function is not part of the public interface.

Clean up the semaphore

Definition at line 242 of file qsystemsemaphore_unix.cpp.

Referenced by handle(), makeKeyFileName(), modifySemaphore(), QSystemSemaphore::setKey(), and QSystemSemaphore::~QSystemSemaphore().

243 {
244 #ifndef QT_POSIX_IPC
245  unix_key = -1;
246 
247  // remove the file if we made it
248  if (createdFile) {
250  createdFile = false;
251  }
252 
253  if (createdSemaphore) {
254  if (-1 != semaphore) {
255  if (-1 == semctl(semaphore, 0, IPC_RMID, 0)) {
256  setErrorString(QLatin1String("QSystemSemaphore::cleanHandle"));
257 #ifdef QSYSTEMSEMAPHORE_DEBUG
258  qDebug("QSystemSemaphore::cleanHandle semctl failed.");
259 #endif
260  }
261  semaphore = -1;
262  }
263  createdSemaphore = false;
264  }
265 #else
266  if (semaphore != SEM_FAILED) {
267  if (sem_close(semaphore) == -1) {
268  setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_close)"));
269 #ifdef QSYSTEMSEMAPHORE_DEBUG
270  qDebug() << QLatin1String("QSystemSemaphore::cleanHandle sem_close failed.");
271 #endif
272  }
273  semaphore = SEM_FAILED;
274  }
275 
276  if (createdSemaphore) {
277  if (sem_unlink(QFile::encodeName(fileName).constData()) == -1 && errno != ENOENT) {
278  setErrorString(QLatin1String("QSystemSemaphore::cleanHandle (sem_unlink)"));
279 #ifdef QSYSTEMSEMAPHORE_DEBUG
280  qDebug() << QLatin1String("QSystemSemaphore::cleanHandle sem_unlink failed.");
281 #endif
282  }
283  createdSemaphore = false;
284  }
285 #endif // QT_POSIX_IPC
286 }
void setErrorString(const QString &function)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void qDebug(const char *,...)
bool remove()
Removes the file specified by fileName().
Definition: qfile.cpp:715
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
int errno

◆ handle()

HANDLE QSystemSemaphorePrivate::handle ( QSystemSemaphore::AccessMode  mode = QSystemSemaphore::Open)
Warning
This function is not part of the public interface.

Initialise the semaphore

Definition at line 127 of file qsystemsemaphore_unix.cpp.

Referenced by makeKeyFileName(), modifySemaphore(), and QSystemSemaphore::setKey().

128 {
129  if (-1 != unix_key)
130  return unix_key;
131 
132  if (key.isEmpty()) {
133  errorString = QCoreApplication::tr("%1: key is empty", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
135  return -1;
136  }
137 
138  // ftok requires that an actual file exists somewhere
140  if (-1 == built) {
141  errorString = QCoreApplication::tr("%1: unable to make key", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
143  return -1;
144  }
145  createdFile = (1 == built);
146 
147  // Get the unix key for the created file
148  unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q');
149  if (-1 == unix_key) {
150  errorString = QCoreApplication::tr("%1: ftok failed", "QSystemSemaphore").arg(QLatin1String("QSystemSemaphore::handle"));
152  return -1;
153  }
154 
155  // Get semaphore
156  semaphore = semget(unix_key, 1, 0600 | IPC_CREAT | IPC_EXCL);
157  if (-1 == semaphore) {
158  if (errno == EEXIST)
159  semaphore = semget(unix_key, 1, 0600 | IPC_CREAT);
160  if (-1 == semaphore) {
161  setErrorString(QLatin1String("QSystemSemaphore::handle"));
162  cleanHandle();
163  return -1;
164  }
165  if (mode == QSystemSemaphore::Create) {
166  createdSemaphore = true;
167  createdFile = true;
168  }
169  } else {
170  createdSemaphore = true;
171  // Force cleanup of file, it is possible that it can be left over from a crash
172  createdFile = true;
173  }
174 
175  // Created semaphore so initialize its value.
176  if (createdSemaphore && initialValue >= 0) {
177  qt_semun init_op;
178  init_op.val = initialValue;
179  if (-1 == semctl(semaphore, 0, SETVAL, init_op)) {
180  setErrorString(QLatin1String("QSystemSemaphore::handle"));
181  cleanHandle();
182  return -1;
183  }
184  }
185 
186  return unix_key;
187 }
void setErrorString(const QString &function)
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static int createUnixKeyFile(const QString &fileName)
Creates the unix file if needed.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QSystemSemaphore::SystemSemaphoreError error
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
int errno

◆ makeKeyFileName()

QString QSystemSemaphorePrivate::makeKeyFileName ( ) const
inline

Definition at line 79 of file qsystemsemaphore_p.h.

Referenced by QSystemSemaphore::setKey().

80  {
82  }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QString makePlatformSafeKey(const QString &key, const QString &prefix=QLatin1String("qipc_sharedmemory_"))
Generate a string from the key which can be any unicode string into the subset that the win/unix kern...

◆ modifySemaphore()

bool QSystemSemaphorePrivate::modifySemaphore ( int  count)
Warning
This function is not part of the public interface.

Definition at line 291 of file qsystemsemaphore_unix.cpp.

Referenced by QSystemSemaphore::acquire(), makeKeyFileName(), and QSystemSemaphore::release().

292 {
293 #ifndef QT_POSIX_IPC
294  if (-1 == handle())
295  return false;
296 
297  struct sembuf operation;
298  operation.sem_num = 0;
299  operation.sem_op = count;
300  operation.sem_flg = SEM_UNDO;
301 
302  register int res;
303  EINTR_LOOP(res, semop(semaphore, &operation, 1));
304  if (-1 == res) {
305  // If the semaphore was removed be nice and create it and then modifySemaphore again
306  if (errno == EINVAL || errno == EIDRM) {
307  semaphore = -1;
308  cleanHandle();
309  handle();
310  return modifySemaphore(count);
311  }
312  setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore"));
313 #ifdef QSYSTEMSEMAPHORE_DEBUG
314  qDebug() << QLatin1String("QSystemSemaphore::modify failed") << count << semctl(semaphore, 0, GETVAL) << errno << EIDRM << EINVAL;
315 #endif
316  return false;
317  }
318 #else
319  if (!handle())
320  return false;
321 
322  if (count > 0) {
323  int cnt = count;
324  do {
325  if (sem_post(semaphore) == -1) {
326  setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_post)"));
327 #ifdef QSYSTEMSEMAPHORE_DEBUG
328  qDebug() << QLatin1String("QSystemSemaphore::modify sem_post failed") << count << errno;
329 #endif
330  // rollback changes to preserve the SysV semaphore behavior
331  for ( ; cnt < count; ++cnt) {
332  register int res;
333  EINTR_LOOP(res, sem_wait(semaphore));
334  }
335  return false;
336  }
337  --cnt;
338  } while (cnt > 0);
339  } else {
340  register int res;
341  EINTR_LOOP(res, sem_wait(semaphore));
342  if (res == -1) {
343  // If the semaphore was removed be nice and create it and then modifySemaphore again
344  if (errno == EINVAL || errno == EIDRM) {
345  semaphore = SEM_FAILED;
346  return modifySemaphore(count);
347  }
348  setErrorString(QLatin1String("QSystemSemaphore::modifySemaphore (sem_wait)"));
349 #ifdef QSYSTEMSEMAPHORE_DEBUG
350  qDebug() << QLatin1String("QSystemSemaphore::modify sem_wait failed") << count << errno;
351 #endif
352  return false;
353  }
354  }
355 #endif // QT_POSIX_IPC
356 
357  return true;
358 }
void setErrorString(const QString &function)
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void qDebug(const char *,...)
HANDLE handle(QSystemSemaphore::AccessMode mode=QSystemSemaphore::Open)
int errno

◆ setErrorString()

void QSystemSemaphorePrivate::setErrorString ( const QString function)

Definition at line 81 of file qsystemsemaphore_unix.cpp.

Referenced by cleanHandle(), handle(), makeKeyFileName(), and modifySemaphore().

82 {
83  // EINVAL is handled in functions so they can give better error strings
84  switch (errno) {
85  case EPERM:
86  case EACCES:
87  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: permission denied").arg(function);
89  break;
90  case EEXIST:
91  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: already exists").arg(function);
93  break;
94  case ENOENT:
95  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: does not exist").arg(function);
97  break;
98  case ERANGE:
99  case ENOMEM:
100  case ENOSPC:
101  case EMFILE:
102  case ENFILE:
103  case EOVERFLOW:
104  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: out of resources").arg(function);
106  break;
107  case ENAMETOOLONG:
108  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: name error").arg(function);
110  break;
111  default:
112  errorString = QCoreApplication::translate("QSystemSemaphore", "%1: unknown error %2").arg(function).arg(errno);
114 #ifdef QSYSTEMSEMAPHORE_DEBUG
115  qDebug() << errorString << "key" << key << "errno" << errno << EINVAL;
116 #endif
117  break;
118  }
119 }
static QString translate(const char *context, const char *key, const char *disambiguation=0, Encoding encoding=CodecForTr)
Q_CORE_EXPORT void qDebug(const char *,...)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QSystemSemaphore::SystemSemaphoreError error
int errno

Properties

◆ error

QSystemSemaphore::SystemSemaphoreError QSystemSemaphorePrivate::error

◆ errorString

QString QSystemSemaphorePrivate::errorString

◆ fileName

QString QSystemSemaphorePrivate::fileName

Definition at line 101 of file qsystemsemaphore_p.h.

Referenced by cleanHandle(), handle(), and QSystemSemaphore::setKey().

◆ initialValue

int QSystemSemaphorePrivate::initialValue

Definition at line 102 of file qsystemsemaphore_p.h.

Referenced by handle(), and QSystemSemaphore::setKey().

◆ key

QString QSystemSemaphorePrivate::key

◆ semaphore

HANDLE QSystemSemaphorePrivate::semaphore

Definition at line 104 of file qsystemsemaphore_p.h.

Referenced by cleanHandle(), handle(), and modifySemaphore().

◆ semaphoreLock

HANDLE QSystemSemaphorePrivate::semaphoreLock

Definition at line 105 of file qsystemsemaphore_p.h.


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