From a21b6047460a28e5def9df3b6fda48aed385b39c Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 5 Jan 2016 11:08:08 -0800 Subject: [PATCH] Simplified implementation of ThreadSafeValueCache --- interface/src/avatar/Avatar.cpp | 16 +++-------- libraries/shared/src/ThreadSafeValueCache.h | 30 +++------------------ 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 5405540e4c..5c69ad100c 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -1198,16 +1198,8 @@ void Avatar::updatePalms() { rightPalmPosition += HAND_TO_PALM_OFFSET * glm::inverse(rightPalmRotation); // update thread-safe caches - _leftPalmRotationCache.merge([&](const glm::quat& value, bool hasPending, const glm::quat& pendingValue) { - return leftPalmRotation; - }); - _rightPalmRotationCache.merge([&](const glm::quat& value, bool hasPending, const glm::quat& pendingValue) { - return rightPalmRotation; - }); - _leftPalmPositionCache.merge([&](const glm::vec3& value, bool hasPending, const glm::vec3& pendingValue) { - return leftPalmPosition; - }); - _rightPalmPositionCache.merge([&](const glm::vec3& value, bool hasPending, const glm::vec3& pendingValue) { - return rightPalmPosition; - }); + _leftPalmRotationCache.set(leftPalmRotation); + _rightPalmRotationCache.set(rightPalmRotation); + _leftPalmPositionCache.set(leftPalmPosition); + _rightPalmPositionCache.set(rightPalmPosition); } diff --git a/libraries/shared/src/ThreadSafeValueCache.h b/libraries/shared/src/ThreadSafeValueCache.h index 5e9a7e8c4f..e4e78ca3d7 100644 --- a/libraries/shared/src/ThreadSafeValueCache.h +++ b/libraries/shared/src/ThreadSafeValueCache.h @@ -17,53 +17,29 @@ // It allows many threads to get or set a value atomically. // This provides cache semantics, any get will return the last set value. // -// It also provides a mechanism for the owner of the cached value to reconcile -// the cached value with it's own internal values, via the merge method. -// // For example: This can be used to copy values between C++ code running on the application thread // and JavaScript which is running on a different thread. template class ThreadSafeValueCache { public: - ThreadSafeValueCache(const T& v) : _value { v }, _pending { v }, _hasPending { false } {} - - // The callback function should have the following prototype. - // T func(const T& value, bool hasPending, const T& pendingValue); - // It will be called synchronously on the current thread possibly blocking it for a short time. - // it gives thread-safe access to the internal cache value, as well as the pending cache value - // that was set via the last set call. This gives the cache's owner the opportunity to update - // the cached value by resolving it with it's own internal state. The owner should then return - // the resolved value which will be atomically reflected into the cache. - template - void merge(F func) { - std::lock_guard guard(_mutex); - _value = func((const T&)_value, _hasPending, (const T&)_pending); - _hasPending = false; - } + ThreadSafeValueCache(const T& v) : _value { v } {} // returns atomic copy of the cached value. T get() const { std::lock_guard guard(_mutex); - if (_hasPending) { - return _pending; - } else { - return _value; - } + return _value; } // will reflect copy of value into the cache. void set(const T& v) { std::lock_guard guard(_mutex); - _hasPending = true; - _pending = v; + _value = v; } private: mutable std::mutex _mutex; T _value; - T _pending; - bool _hasPending; // no copies ThreadSafeValueCache(const ThreadSafeValueCache&) = delete;