diff options
Diffstat (limited to 'libutils')
| -rw-r--r-- | libutils/Looper.cpp | 4 | ||||
| -rw-r--r-- | libutils/SharedBuffer.cpp | 24 | ||||
| -rw-r--r-- | libutils/include/utils/SortedVector.h | 53 |
3 files changed, 55 insertions, 26 deletions
diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp index 77e69e4b3..84bc028f8 100644 --- a/libutils/Looper.cpp +++ b/libutils/Looper.cpp @@ -83,6 +83,7 @@ Looper::Looper(bool allowNonCallbacks) : Looper::~Looper() { close(mWakeEventFd); + mWakeEventFd = -1; if (mEpollFd >= 0) { close(mEpollFd); } @@ -412,7 +413,8 @@ void Looper::wake() { ssize_t nWrite = TEMP_FAILURE_RETRY(write(mWakeEventFd, &inc, sizeof(uint64_t))); if (nWrite != sizeof(uint64_t)) { if (errno != EAGAIN) { - ALOGW("Could not write wake signal: %s", strerror(errno)); + LOG_ALWAYS_FATAL("Could not write wake signal to fd %d: %s", + mWakeEventFd, strerror(errno)); } } } diff --git a/libutils/SharedBuffer.cpp b/libutils/SharedBuffer.cpp index 957aedb8f..1fa1d7a54 100644 --- a/libutils/SharedBuffer.cpp +++ b/libutils/SharedBuffer.cpp @@ -113,16 +113,26 @@ void SharedBuffer::acquire() const { int32_t SharedBuffer::release(uint32_t flags) const { - int32_t prev = 1; - if (onlyOwner() - || (((prev = mRefs.fetch_sub(1, std::memory_order_release)) == 1) - && (atomic_thread_fence(std::memory_order_acquire), true))) { + const bool useDealloc = ((flags & eKeepStorage) == 0); + if (onlyOwner()) { + // Since we're the only owner, our reference count goes to zero. mRefs.store(0, std::memory_order_relaxed); - if ((flags & eKeepStorage) == 0) { - free(const_cast<SharedBuffer*>(this)); + if (useDealloc) { + dealloc(this); + } + // As the only owner, our previous reference count was 1. + return 1; + } + // There's multiple owners, we need to use an atomic decrement. + int32_t prevRefCount = mRefs.fetch_sub(1, std::memory_order_release); + if (prevRefCount == 1) { + // We're the last reference, we need the acquire fence. + atomic_thread_fence(std::memory_order_acquire); + if (useDealloc) { + dealloc(this); } } - return prev; + return prevRefCount; } diff --git a/libutils/include/utils/SortedVector.h b/libutils/include/utils/SortedVector.h index 86f349645..d57465d33 100644 --- a/libutils/include/utils/SortedVector.h +++ b/libutils/include/utils/SortedVector.h @@ -37,18 +37,18 @@ class SortedVector : private SortedVectorImpl public: typedef TYPE value_type; - - /*! + + /*! * Constructors and destructors */ - + SortedVector(); SortedVector(const SortedVector<TYPE>& rhs); virtual ~SortedVector(); /*! copy operator */ - const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; - SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs); + const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; + SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs); /* * empty the vector @@ -56,7 +56,7 @@ public: inline void clear() { VectorImpl::clear(); } - /*! + /*! * vector stats */ @@ -69,11 +69,11 @@ public: //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } - /*! + /*! * C-style array access */ - - //! read-only C-style access + + //! read-only C-style access inline const TYPE* array() const; //! read-write C-style access. BE VERY CAREFUL when modifying the array @@ -82,12 +82,12 @@ public: //! finds the index of an item ssize_t indexOf(const TYPE& item) const; - + //! finds where this item should be inserted size_t orderOf(const TYPE& item) const; - - - /*! + + + /*! * accessors */ @@ -104,7 +104,7 @@ public: //! add an item in the right place (and replace the one that is there) ssize_t add(const TYPE& item); - + //! editItemAt() MUST NOT change the order of this item TYPE& editItemAt(size_t index) { return *( static_cast<TYPE *>(VectorImpl::editItemLocation(index)) ); @@ -113,7 +113,7 @@ public: //! merges a vector into this one ssize_t merge(const Vector<TYPE>& vector); ssize_t merge(const SortedVector<TYPE>& vector); - + //! removes an item ssize_t remove(const TYPE&); @@ -121,7 +121,24 @@ public: inline ssize_t removeItemsAt(size_t index, size_t count = 1); //! remove one item inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } - + + /* + * these inlines add some level of compatibility with STL. + */ + typedef TYPE* iterator; + typedef TYPE const* const_iterator; + + inline iterator begin() { return editArray(); } + inline iterator end() { return editArray() + size(); } + inline const_iterator begin() const { return array(); } + inline const_iterator end() const { return array() + size(); } + inline void reserve(size_t n) { setCapacity(n); } + inline bool empty() const{ return isEmpty(); } + inline iterator erase(iterator pos) { + ssize_t index = removeItemsAt(pos-array()); + return begin() + index; + } + protected: virtual void do_construct(void* storage, size_t num) const; virtual void do_destroy(void* storage, size_t num) const; @@ -159,13 +176,13 @@ SortedVector<TYPE>::~SortedVector() { template<class TYPE> inline SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) { SortedVectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline const SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const { SortedVectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline |
