Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmutex_mac.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qplatformdefs.h"
6#include "qmutex.h"
7#include "qmutex_p.h"
8
9#include "private/qcore_unix_p.h"
10
11#include <mach/mach.h>
12#include <mach/task.h>
13
14#include <errno.h>
15
16QT_BEGIN_NAMESPACE
17
18QMutexPrivate::QMutexPrivate()
19{
20 kern_return_t r = semaphore_create(mach_task_self(), &mach_semaphore, SYNC_POLICY_FIFO, 0);
21 if (r != KERN_SUCCESS)
22 qWarning("QMutex: failed to create semaphore, error %d", r);
23}
24
25QMutexPrivate::~QMutexPrivate()
26{
27 kern_return_t r = semaphore_destroy(mach_task_self(), mach_semaphore);
28 if (r != KERN_SUCCESS)
29 qWarning("QMutex: failed to destroy semaphore, error %d", r);
30}
31
32bool QMutexPrivate::wait(QDeadlineTimer timeout)
33{
34 kern_return_t r;
35 if (timeout.isForever()) {
36 do {
37 r = semaphore_wait(mach_semaphore);
38 } while (r == KERN_ABORTED);
39 Q_ASSERT(r == KERN_SUCCESS);
40 } else {
41 timespec tv = durationToTimespec(timeout.remainingTimeAsDuration());
42 mach_timespec_t ts;
43 ts.tv_nsec = tv.tv_nsec;
44 ts.tv_sec = tv.tv_sec;
45 r = semaphore_timedwait(mach_semaphore, ts);
46 }
47 return (r == KERN_SUCCESS);
48}
49
50void QMutexPrivate::wakeUp() noexcept
51{
52 semaphore_signal(mach_semaphore);
53}
54
55
56QT_END_NAMESPACE