diff options
Diffstat (limited to 'src/foundation/macos')
-rw-r--r-- | src/foundation/macos/Qt3DSUnixAtomic.cpp | 89 | ||||
-rw-r--r-- | src/foundation/macos/Qt3DSUnixFPU.cpp | 61 | ||||
-rw-r--r-- | src/foundation/macos/Qt3DSUnixMutex.cpp | 120 | ||||
-rw-r--r-- | src/foundation/macos/Qt3DSUnixSemaphore.cpp | 146 | ||||
-rw-r--r-- | src/foundation/macos/Qt3DSUnixSync.cpp | 137 | ||||
-rw-r--r-- | src/foundation/macos/Qt3DSUnixTime.cpp | 108 |
6 files changed, 661 insertions, 0 deletions
diff --git a/src/foundation/macos/Qt3DSUnixAtomic.cpp b/src/foundation/macos/Qt3DSUnixAtomic.cpp new file mode 100644 index 0000000..c1ac396 --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixAtomic.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2001-2004 NovodeX AG. +** Copyright (C) 2004-2008 AGEIA Technologies, Inc. +** Copyright (C) 2008-2013 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSAtomic.h" + +#define PAUSE() asm("nop") + +namespace qt3ds { +namespace foundation { + + void *atomicCompareExchangePointer(volatile void **dest, void *exch, void *comp) + { + return __sync_val_compare_and_swap((void **)dest, comp, exch); + } + + QT3DSI32 atomicCompareExchange(volatile QT3DSI32 *dest, QT3DSI32 exch, QT3DSI32 comp) + { + return __sync_val_compare_and_swap(dest, comp, exch); + } + + QT3DSI32 atomicIncrement(volatile QT3DSI32 *val) { return __sync_add_and_fetch(val, 1); } + + QT3DSI32 atomicDecrement(volatile QT3DSI32 *val) { return __sync_sub_and_fetch(val, 1); } + + QT3DSI32 atomicAdd(volatile QT3DSI32 *val, QT3DSI32 delta) { return __sync_add_and_fetch(val, delta); } + + QT3DSI32 atomicMax(volatile QT3DSI32 *val, QT3DSI32 val2) + { + QT3DSI32 oldVal, newVal; + + do { + PAUSE(); + oldVal = *val; + + if (val2 > oldVal) + newVal = val2; + else + newVal = oldVal; + + } while (atomicCompareExchange(val, newVal, oldVal) != oldVal); + + return *val; + } + + QT3DSI32 atomicExchange(volatile QT3DSI32 *val, QT3DSI32 val2) + { + QT3DSI32 newVal, oldVal; + + do { + PAUSE(); + oldVal = *val; + newVal = val2; + } while (atomicCompareExchange(val, newVal, oldVal) != oldVal); + + return oldVal; + } + +} // namespace foundation +} // namespace qt3ds diff --git a/src/foundation/macos/Qt3DSUnixFPU.cpp b/src/foundation/macos/Qt3DSUnixFPU.cpp new file mode 100644 index 0000000..59c9850 --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixFPU.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2001-2004 NovodeX AG. +** Copyright (C) 2004-2008 AGEIA Technologies, Inc. +** Copyright (C) 2008-2013 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DSFPU.h" +#include <fenv.h> + +#if !(defined(__CYGWIN__) || defined(PX_ANDROID)) +QT3DS_COMPILE_TIME_ASSERT(8 * sizeof(qt3ds::QT3DSU32) >= sizeof(fenv_t)); +#endif + +qt3ds::foundation::FPUGuard::FPUGuard() +{ +#if defined(__CYGWIN__) +#pragma message "FPUGuard::FPUGuard() is not implemented" +#elif defined(PX_ANDROID) +// not supported unless ARM_HARD_FLOAT is enabled. +#else + fegetenv(reinterpret_cast<fenv_t *>(mControlWords)); + fesetenv(FE_DFL_ENV); +#endif +} + +qt3ds::foundation::FPUGuard::~FPUGuard() +{ +#if defined(__CYGWIN__) +#pragma message "FPUGuard::~FPUGuard() is not implemented" +#elif defined(PX_ANDROID) +// not supported unless ARM_HARD_FLOAT is enabled. +#else + fesetenv(reinterpret_cast<fenv_t *>(mControlWords)); +#endif +} diff --git a/src/foundation/macos/Qt3DSUnixMutex.cpp b/src/foundation/macos/Qt3DSUnixMutex.cpp new file mode 100644 index 0000000..bbc5582 --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixMutex.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSMutex.h" +#include "foundation/Qt3DSAssert.h" +#include "foundation/Qt3DSAtomic.h" + +#include <pthread.h> + +namespace qt3ds { +namespace foundation { + + namespace { + pthread_mutex_t *getMutex(MutexImpl *impl) + { + return reinterpret_cast<pthread_mutex_t *>(impl); + } + } + + MutexImpl::MutexImpl() + { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(getMutex(this), &attr); + pthread_mutexattr_destroy(&attr); + } + + MutexImpl::~MutexImpl() { pthread_mutex_destroy(getMutex(this)); } + + bool MutexImpl::lock() { return !pthread_mutex_lock(getMutex(this)); } + + bool MutexImpl::trylock() { return !pthread_mutex_trylock(getMutex(this)); } + + bool MutexImpl::unlock() { return !pthread_mutex_unlock(getMutex(this)); } + + const QT3DSU32 MutexImpl::size = sizeof(pthread_mutex_t); + + class ReadWriteLockImpl + { + public: + ReadWriteLockImpl(NVAllocatorCallback &alloc) + : mAllocator(alloc) + { + } + NVAllocatorCallback &mAllocator; + pthread_mutex_t mutex; + volatile int readerCounter; + }; + + ReadWriteLock::ReadWriteLock(NVAllocatorCallback &alloc) + { + mImpl = QT3DS_NEW(alloc, ReadWriteLockImpl)(alloc); + + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mImpl->mutex, &attr); + pthread_mutexattr_destroy(&attr); + + mImpl->readerCounter = 0; + } + + ReadWriteLock::~ReadWriteLock() + { + pthread_mutex_destroy(&mImpl->mutex); + QT3DS_FREE(mImpl->mAllocator, mImpl); + } + + void ReadWriteLock::lockReader() + { + pthread_mutex_lock(&mImpl->mutex); + + atomicIncrement(&mImpl->readerCounter); + + pthread_mutex_unlock(&mImpl->mutex); + } + + void ReadWriteLock::lockWriter() + { + pthread_mutex_lock(&mImpl->mutex); + + while (mImpl->readerCounter != 0) + ; + } + + void ReadWriteLock::unlockReader() { atomicDecrement(&mImpl->readerCounter); } + + void ReadWriteLock::unlockWriter() { pthread_mutex_unlock(&mImpl->mutex); } + +} // namespace foundation +} // namespace qt3ds diff --git a/src/foundation/macos/Qt3DSUnixSemaphore.cpp b/src/foundation/macos/Qt3DSUnixSemaphore.cpp new file mode 100644 index 0000000..90aab79 --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixSemaphore.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSSemaphore.h" +#include "foundation/Qt3DSAssert.h" +#include "foundation/Qt3DSAllocator.h" +#include "foundation/Qt3DSAllocatorCallback.h" + +#include <errno.h> +#include <stdio.h> +#include <pthread.h> +#include <time.h> +#include <sys/time.h> + +namespace qt3ds { +namespace foundation { + + class SemaphoreImpl + { + public: + SemaphoreImpl(NVAllocatorCallback &alloc) + : mAllocator(alloc) + { + } + NVAllocatorCallback &mAllocator; + pthread_mutex_t mutex; + pthread_cond_t cond; + QT3DSU32 count; + QT3DSU32 maxCount; + }; + + struct NVLinuxScopeLock + { + NVLinuxScopeLock(pthread_mutex_t &m) + : mMutex(m) + { + pthread_mutex_lock(&mMutex); + } + + ~NVLinuxScopeLock() { pthread_mutex_unlock(&mMutex); } + private: + pthread_mutex_t &mMutex; + }; + + Semaphore::Semaphore(NVAllocatorCallback &alloc, QT3DSU32 initialCount, QT3DSU32 maxCount) + { + mImpl = QT3DS_NEW(alloc, SemaphoreImpl)(alloc); + int status = pthread_mutex_init(&mImpl->mutex, 0); + QT3DS_ASSERT(!status); + status = pthread_cond_init(&mImpl->cond, 0); + QT3DS_ASSERT(!status); + mImpl->count = initialCount; + mImpl->maxCount = maxCount; + QT3DS_ASSERT(initialCount <= maxCount); + } + + Semaphore::~Semaphore() + { + pthread_cond_destroy(&mImpl->cond); + pthread_mutex_destroy(&mImpl->mutex); + QT3DS_FREE(mImpl->mAllocator, mImpl); + } + + bool Semaphore::wait(QT3DSU32 milliseconds) + { + NVLinuxScopeLock lock(mImpl->mutex); + + if (mImpl->count > 0) { + mImpl->count--; + return true; + } + + if (milliseconds == 0) { + return false; + } + + if (milliseconds == QT3DSU32(-1)) { + int status = pthread_cond_wait(&mImpl->cond, &mImpl->mutex); + QT3DS_ASSERT(!status); + (void)status; + } else { + timespec ts; + timeval tp; + gettimeofday(&tp, NULL); + QT3DSU32 sec = milliseconds / 1000; + QT3DSU32 usec = (milliseconds - 1000 * sec) * 1000; + + // sschirm: taking into account that us might accumulate to a second + // otherwise the pthread_cond_timedwait complains on osx. + usec = tp.tv_usec + usec; + QT3DSU32 div_sec = usec / 1000000; + QT3DSU32 rem_usec = usec - div_sec * 1000000; + + ts.tv_sec = tp.tv_sec + sec + div_sec; + ts.tv_nsec = rem_usec * 1000; + + int ierr = pthread_cond_timedwait(&mImpl->cond, &mImpl->mutex, &ts); + QT3DS_ASSERT((ierr == 0) || (ierr == ETIMEDOUT)); + (void)ierr; + return false; + } + + return true; + } + + void Semaphore::post() + { + NVLinuxScopeLock lock(mImpl->mutex); + mImpl->count++; + if (mImpl->count > mImpl->maxCount) + mImpl->count = mImpl->maxCount; + else { + pthread_cond_broadcast(&mImpl->cond); + } + } + +} // namespace foundation +} // namespace qt3ds diff --git a/src/foundation/macos/Qt3DSUnixSync.cpp b/src/foundation/macos/Qt3DSUnixSync.cpp new file mode 100644 index 0000000..52f9d7a --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixSync.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2008-2012 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSSync.h" +#include "foundation/Qt3DSAssert.h" +#include "foundation/Qt3DSAllocator.h" +#include "foundation/Qt3DSAllocatorCallback.h" + +#include <errno.h> +#include <stdio.h> +#include <pthread.h> +#include <time.h> +#include <sys/time.h> + +namespace qt3ds { +namespace foundation { + + class SyncImpl + { + public: + SyncImpl(NVAllocatorCallback &alloc) + : mAllocator(alloc) + { + } + NVAllocatorCallback &mAllocator; + pthread_mutex_t mutex; + pthread_cond_t cond; + volatile bool is_set; + }; + + struct NVLinuxScopeLock + { + NVLinuxScopeLock(pthread_mutex_t &m) + : mMutex(m) + { + pthread_mutex_lock(&mMutex); + } + + ~NVLinuxScopeLock() { pthread_mutex_unlock(&mMutex); } + private: + pthread_mutex_t &mMutex; + }; + + Sync::Sync(NVAllocatorCallback &alloc) + { + mImpl = QT3DS_NEW(alloc, SyncImpl)(alloc); + int status = pthread_mutex_init(&mImpl->mutex, 0); + QT3DS_ASSERT(!status); + status = pthread_cond_init(&mImpl->cond, 0); + QT3DS_ASSERT(!status); + mImpl->is_set = false; + } + + Sync::~Sync() + { + pthread_cond_destroy(&mImpl->cond); + pthread_mutex_destroy(&mImpl->mutex); + QT3DS_FREE(mImpl->mAllocator, mImpl); + } + + void Sync::reset() + { + NVLinuxScopeLock lock(mImpl->mutex); + mImpl->is_set = false; + } + + void Sync::set() + { + NVLinuxScopeLock lock(mImpl->mutex); + if (!mImpl->is_set) { + mImpl->is_set = true; + pthread_cond_broadcast(&mImpl->cond); + } + } + + bool Sync::wait(QT3DSU32 ms) + { + NVLinuxScopeLock lock(mImpl->mutex); + if (!mImpl->is_set) { + if (ms == QT3DSU32(-1)) { + int status = pthread_cond_wait(&mImpl->cond, &mImpl->mutex); + QT3DS_ASSERT(!status); + (void)status; + } else { + timespec ts; + timeval tp; + gettimeofday(&tp, NULL); + QT3DSU32 sec = ms / 1000; + QT3DSU32 usec = (ms - 1000 * sec) * 1000; + + // sschirm: taking into account that us might accumulate to a second + // otherwise the pthread_cond_timedwait complains on osx. + usec = tp.tv_usec + usec; + QT3DSU32 div_sec = usec / 1000000; + QT3DSU32 rem_usec = usec - div_sec * 1000000; + + ts.tv_sec = tp.tv_sec + sec + div_sec; + ts.tv_nsec = rem_usec * 1000; + + int ierr = pthread_cond_timedwait(&mImpl->cond, &mImpl->mutex, &ts); + QT3DS_ASSERT((ierr == 0) || (ierr == ETIMEDOUT)); + (void)ierr; + } + } + return mImpl->is_set; + } + +} // namespace foundation +} // namespace qt3ds diff --git a/src/foundation/macos/Qt3DSUnixTime.cpp b/src/foundation/macos/Qt3DSUnixTime.cpp new file mode 100644 index 0000000..58eadcb --- /dev/null +++ b/src/foundation/macos/Qt3DSUnixTime.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2001-2004 NovodeX AG. +** Copyright (C) 2004-2008 AGEIA Technologies, Inc. +** Copyright (C) 2008-2013 NVIDIA Corporation. +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt 3D Studio. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "foundation/Qt3DS.h" +#include "foundation/Qt3DSTime.h" + +#include <time.h> +#include <sys/time.h> + +#if defined QT3DS_APPLE +#include <mach/mach_time.h> +#endif + +// Use real-time high-precision timer. +#ifndef QT3DS_APPLE +#define CLOCKID CLOCK_REALTIME +#endif + +namespace qt3ds { +namespace foundation { + + const CounterFrequencyToTensOfNanos Time::sCounterFreq = Time::getCounterFrequency(); + + static Time::Second getTimeSeconds() + { + static struct timeval _tv; + gettimeofday(&_tv, NULL); + return double(_tv.tv_sec) + double(_tv.tv_usec) * 0.000001; + } + + Time::Time() { mLastTime = getTimeSeconds(); } + + Time::Second Time::getElapsedSeconds() + { + Time::Second curTime = getTimeSeconds(); + Time::Second diff = curTime - mLastTime; + mLastTime = curTime; + return diff; + } + + Time::Second Time::peekElapsedSeconds() + { + Time::Second curTime = getTimeSeconds(); + Time::Second diff = curTime - mLastTime; + return diff; + } + + Time::Second Time::getLastTime() const { return mLastTime; } + +#ifdef QT3DS_APPLE + CounterFrequencyToTensOfNanos Time::getCounterFrequency() + { + mach_timebase_info_data_t info; + mach_timebase_info(&info); + // mach_absolute_time * (info.numer/info.denom) is in units of nano seconds + return CounterFrequencyToTensOfNanos(info.numer, info.denom * 10); + } + + QT3DSU64 Time::getCurrentCounterValue() { return mach_absolute_time(); } + +#else + + CounterFrequencyToTensOfNanos Time::getCounterFrequency() + { + return CounterFrequencyToTensOfNanos(1, 10); + } + + PxU64 Time::getCurrentCounterValue() + { + struct timespec mCurrTimeInt; + clock_gettime(CLOCKID, &mCurrTimeInt); + // Convert to nanos as this doesn't cause a large divide here + return (static_cast<PxU64>(mCurrTimeInt.tv_sec) * 1000000000) + + (static_cast<PxU64>(mCurrTimeInt.tv_nsec)); + } +#endif + +} // namespace foundation +} // namespace qt3ds |