michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: #include "LulRWLock.h" michael@0: michael@0: michael@0: namespace lul { michael@0: michael@0: // An implementation for targets where libpthread does provide michael@0: // pthread_rwlock_t. These are straight wrappers around the michael@0: // equivalent pthread functions. michael@0: michael@0: #if defined(LUL_OS_linux) michael@0: michael@0: LulRWLock::LulRWLock() { michael@0: mozilla::DebugOnly r = pthread_rwlock_init(&mLock, nullptr); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: LulRWLock::~LulRWLock() { michael@0: mozilla::DebugOnlyr = pthread_rwlock_destroy(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::WrLock() { michael@0: mozilla::DebugOnlyr = pthread_rwlock_wrlock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::RdLock() { michael@0: mozilla::DebugOnlyr = pthread_rwlock_rdlock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::Unlock() { michael@0: mozilla::DebugOnlyr = pthread_rwlock_unlock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: michael@0: // An implementation for cases where libpthread does not provide michael@0: // pthread_rwlock_t. Currently this is a kludge in that it uses michael@0: // normal mutexes, resulting in the following limitations: (1) at most michael@0: // one reader is allowed at once, and (2) any thread that tries to michael@0: // read-acquire the lock more than once will deadlock. (2) could be michael@0: // avoided if it were possible to use recursive pthread_mutex_t's. michael@0: michael@0: #elif defined(LUL_OS_android) michael@0: michael@0: LulRWLock::LulRWLock() { michael@0: mozilla::DebugOnly r = pthread_mutex_init(&mLock, nullptr); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: LulRWLock::~LulRWLock() { michael@0: mozilla::DebugOnlyr = pthread_mutex_destroy(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::WrLock() { michael@0: mozilla::DebugOnlyr = pthread_mutex_lock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::RdLock() { michael@0: mozilla::DebugOnlyr = pthread_mutex_lock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: void michael@0: LulRWLock::Unlock() { michael@0: mozilla::DebugOnlyr = pthread_mutex_unlock(&mLock); michael@0: MOZ_ASSERT(!r); michael@0: } michael@0: michael@0: michael@0: #else michael@0: # error "Unsupported OS" michael@0: #endif michael@0: michael@0: } // namespace lul