1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/profiler/LulRWLock.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,93 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "mozilla/Assertions.h" 1.11 +#include "mozilla/DebugOnly.h" 1.12 +#include "mozilla/NullPtr.h" 1.13 + 1.14 +#include "LulRWLock.h" 1.15 + 1.16 + 1.17 +namespace lul { 1.18 + 1.19 +// An implementation for targets where libpthread does provide 1.20 +// pthread_rwlock_t. These are straight wrappers around the 1.21 +// equivalent pthread functions. 1.22 + 1.23 +#if defined(LUL_OS_linux) 1.24 + 1.25 +LulRWLock::LulRWLock() { 1.26 + mozilla::DebugOnly<int> r = pthread_rwlock_init(&mLock, nullptr); 1.27 + MOZ_ASSERT(!r); 1.28 +} 1.29 + 1.30 +LulRWLock::~LulRWLock() { 1.31 + mozilla::DebugOnly<int>r = pthread_rwlock_destroy(&mLock); 1.32 + MOZ_ASSERT(!r); 1.33 +} 1.34 + 1.35 +void 1.36 +LulRWLock::WrLock() { 1.37 + mozilla::DebugOnly<int>r = pthread_rwlock_wrlock(&mLock); 1.38 + MOZ_ASSERT(!r); 1.39 +} 1.40 + 1.41 +void 1.42 +LulRWLock::RdLock() { 1.43 + mozilla::DebugOnly<int>r = pthread_rwlock_rdlock(&mLock); 1.44 + MOZ_ASSERT(!r); 1.45 +} 1.46 + 1.47 +void 1.48 +LulRWLock::Unlock() { 1.49 + mozilla::DebugOnly<int>r = pthread_rwlock_unlock(&mLock); 1.50 + MOZ_ASSERT(!r); 1.51 +} 1.52 + 1.53 + 1.54 +// An implementation for cases where libpthread does not provide 1.55 +// pthread_rwlock_t. Currently this is a kludge in that it uses 1.56 +// normal mutexes, resulting in the following limitations: (1) at most 1.57 +// one reader is allowed at once, and (2) any thread that tries to 1.58 +// read-acquire the lock more than once will deadlock. (2) could be 1.59 +// avoided if it were possible to use recursive pthread_mutex_t's. 1.60 + 1.61 +#elif defined(LUL_OS_android) 1.62 + 1.63 +LulRWLock::LulRWLock() { 1.64 + mozilla::DebugOnly<int> r = pthread_mutex_init(&mLock, nullptr); 1.65 + MOZ_ASSERT(!r); 1.66 +} 1.67 + 1.68 +LulRWLock::~LulRWLock() { 1.69 + mozilla::DebugOnly<int>r = pthread_mutex_destroy(&mLock); 1.70 + MOZ_ASSERT(!r); 1.71 +} 1.72 + 1.73 +void 1.74 +LulRWLock::WrLock() { 1.75 + mozilla::DebugOnly<int>r = pthread_mutex_lock(&mLock); 1.76 + MOZ_ASSERT(!r); 1.77 +} 1.78 + 1.79 +void 1.80 +LulRWLock::RdLock() { 1.81 + mozilla::DebugOnly<int>r = pthread_mutex_lock(&mLock); 1.82 + MOZ_ASSERT(!r); 1.83 +} 1.84 + 1.85 +void 1.86 +LulRWLock::Unlock() { 1.87 + mozilla::DebugOnly<int>r = pthread_mutex_unlock(&mLock); 1.88 + MOZ_ASSERT(!r); 1.89 +} 1.90 + 1.91 + 1.92 +#else 1.93 +# error "Unsupported OS" 1.94 +#endif 1.95 + 1.96 +} // namespace lul