Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qmutex_unix.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qplatformdefs.h"
6#include "qmutex.h"
7#include "qstring.h"
8#include "qelapsedtimer.h"
9#include "qatomic.h"
10#include "qmutex_p.h"
11#include <errno.h>
12#include <sys/time.h>
13#include <time.h>
14#include "private/qcore_unix_p.h"
15
16#if defined(Q_OS_VXWORKS) && defined(wakeup)
17#undef wakeup
18#endif
19
21
22static void qt_report_error(int code, const char *where, const char *what)
23{
24 if (code != 0)
25 qErrnoWarning(code, "%s: %s failure", where, what);
26}
27
29{
30 qt_report_error(sem_init(&semaphore, 0, 0), "QMutex", "sem_init");
31}
32
34{
35
36 qt_report_error(sem_destroy(&semaphore), "QMutex", "sem_destroy");
37}
38
40{
41 int errorCode;
42 if (timeout.isForever()) {
43 do {
44 errorCode = sem_wait(&semaphore);
45 } while (errorCode && errno == EINTR);
46 qt_report_error(errorCode, "QMutex::lock()", "sem_wait");
47 } else {
48 do {
49 auto tp = timeout.deadline<std::chrono::system_clock>();
50 timespec ts = durationToTimespec(tp.time_since_epoch());
51 errorCode = sem_timedwait(&semaphore, &ts);
52 } while (errorCode && errno == EINTR);
53
54 if (errorCode && errno == ETIMEDOUT)
55 return false;
56 qt_report_error(errorCode, "QMutex::lock()", "sem_timedwait");
57 }
58 return true;
59}
60
61void QMutexPrivate::wakeUp() noexcept
62{
63 qt_report_error(sem_post(&semaphore), "QMutex::unlock", "sem_post");
64}
65
\inmodule QtCore
bool wait(QDeadlineTimer timeout=QDeadlineTimer::Forever)
void wakeUp() noexcept
void qErrnoWarning(const char *msg,...)
Combined button and popup list for selecting options.
timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept
static QT_BEGIN_NAMESPACE void qt_report_error(int code, const char *where, const char *what)
GLbitfield GLuint64 timeout
[4]