Loading libutils/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ cc_library { "StopWatch.cpp", "String8.cpp", "String16.cpp", "StrongPointer.cpp", "SystemClock.cpp", "Threads.cpp", "Timers.cpp", Loading libutils/StrongPointer.cpp 0 → 100644 +24 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "sp" #include <log/log.h> namespace android { void sp_report_race() { LOG_ALWAYS_FATAL("sp<> assignment detected data race"); } } libutils/include/utils/StrongPointer.h +26 −20 Original line number Diff line number Diff line Loading @@ -108,6 +108,9 @@ private: T* m_ptr; }; // For code size reasons, we do not want this inlined or templated. void sp_report_race(); #undef COMPARE // --------------------------------------------------------------------------- Loading Loading @@ -161,19 +164,21 @@ sp<T>::~sp() { template<typename T> sp<T>& sp<T>::operator =(const sp<T>& other) { // Force m_ptr to be read twice, to heuristically check for data races. T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); if (otherPtr) otherPtr->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = otherPtr; return *this; } template<typename T> sp<T>& sp<T>::operator =(sp<T>&& other) { if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other.m_ptr; other.m_ptr = nullptr; return *this; Loading @@ -181,29 +186,30 @@ sp<T>& sp<T>::operator =(sp<T>&& other) { template<typename T> sp<T>& sp<T>::operator =(T* other) { if (other) other->incStrong(this); if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (other) other->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other; return *this; } template<typename T> template<typename U> sp<T>& sp<T>::operator =(const sp<U>& other) { T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); if (otherPtr) otherPtr->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = otherPtr; return *this; } template<typename T> template<typename U> sp<T>& sp<T>::operator =(sp<U>&& other) { if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (m_ptr) m_ptr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other.m_ptr; other.m_ptr = nullptr; return *this; Loading @@ -211,10 +217,10 @@ sp<T>& sp<T>::operator =(sp<U>&& other) { template<typename T> template<typename U> sp<T>& sp<T>::operator =(U* other) { if (other) (static_cast<T*>(other))->incStrong(this); if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (other) (static_cast<T*>(other))->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other; return *this; } Loading Loading
libutils/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ cc_library { "StopWatch.cpp", "String8.cpp", "String16.cpp", "StrongPointer.cpp", "SystemClock.cpp", "Threads.cpp", "Timers.cpp", Loading
libutils/StrongPointer.cpp 0 → 100644 +24 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "sp" #include <log/log.h> namespace android { void sp_report_race() { LOG_ALWAYS_FATAL("sp<> assignment detected data race"); } }
libutils/include/utils/StrongPointer.h +26 −20 Original line number Diff line number Diff line Loading @@ -108,6 +108,9 @@ private: T* m_ptr; }; // For code size reasons, we do not want this inlined or templated. void sp_report_race(); #undef COMPARE // --------------------------------------------------------------------------- Loading Loading @@ -161,19 +164,21 @@ sp<T>::~sp() { template<typename T> sp<T>& sp<T>::operator =(const sp<T>& other) { // Force m_ptr to be read twice, to heuristically check for data races. T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); if (otherPtr) otherPtr->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = otherPtr; return *this; } template<typename T> sp<T>& sp<T>::operator =(sp<T>&& other) { if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other.m_ptr; other.m_ptr = nullptr; return *this; Loading @@ -181,29 +186,30 @@ sp<T>& sp<T>::operator =(sp<T>&& other) { template<typename T> sp<T>& sp<T>::operator =(T* other) { if (other) other->incStrong(this); if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (other) other->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other; return *this; } template<typename T> template<typename U> sp<T>& sp<T>::operator =(const sp<U>& other) { T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); T* otherPtr(other.m_ptr); if (otherPtr) otherPtr->incStrong(this); if (m_ptr) m_ptr->decStrong(this); if (otherPtr) otherPtr->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = otherPtr; return *this; } template<typename T> template<typename U> sp<T>& sp<T>::operator =(sp<U>&& other) { if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (m_ptr) m_ptr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other.m_ptr; other.m_ptr = nullptr; return *this; Loading @@ -211,10 +217,10 @@ sp<T>& sp<T>::operator =(sp<U>&& other) { template<typename T> template<typename U> sp<T>& sp<T>::operator =(U* other) { if (other) (static_cast<T*>(other))->incStrong(this); if (m_ptr) m_ptr->decStrong(this); T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); if (other) (static_cast<T*>(other))->incStrong(this); if (oldPtr) oldPtr->decStrong(this); if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); m_ptr = other; return *this; } Loading