tools/profiler/LulRWLock.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/Assertions.h"
michael@0 8 #include "mozilla/DebugOnly.h"
michael@0 9 #include "mozilla/NullPtr.h"
michael@0 10
michael@0 11 #include "LulRWLock.h"
michael@0 12
michael@0 13
michael@0 14 namespace lul {
michael@0 15
michael@0 16 // An implementation for targets where libpthread does provide
michael@0 17 // pthread_rwlock_t. These are straight wrappers around the
michael@0 18 // equivalent pthread functions.
michael@0 19
michael@0 20 #if defined(LUL_OS_linux)
michael@0 21
michael@0 22 LulRWLock::LulRWLock() {
michael@0 23 mozilla::DebugOnly<int> r = pthread_rwlock_init(&mLock, nullptr);
michael@0 24 MOZ_ASSERT(!r);
michael@0 25 }
michael@0 26
michael@0 27 LulRWLock::~LulRWLock() {
michael@0 28 mozilla::DebugOnly<int>r = pthread_rwlock_destroy(&mLock);
michael@0 29 MOZ_ASSERT(!r);
michael@0 30 }
michael@0 31
michael@0 32 void
michael@0 33 LulRWLock::WrLock() {
michael@0 34 mozilla::DebugOnly<int>r = pthread_rwlock_wrlock(&mLock);
michael@0 35 MOZ_ASSERT(!r);
michael@0 36 }
michael@0 37
michael@0 38 void
michael@0 39 LulRWLock::RdLock() {
michael@0 40 mozilla::DebugOnly<int>r = pthread_rwlock_rdlock(&mLock);
michael@0 41 MOZ_ASSERT(!r);
michael@0 42 }
michael@0 43
michael@0 44 void
michael@0 45 LulRWLock::Unlock() {
michael@0 46 mozilla::DebugOnly<int>r = pthread_rwlock_unlock(&mLock);
michael@0 47 MOZ_ASSERT(!r);
michael@0 48 }
michael@0 49
michael@0 50
michael@0 51 // An implementation for cases where libpthread does not provide
michael@0 52 // pthread_rwlock_t. Currently this is a kludge in that it uses
michael@0 53 // normal mutexes, resulting in the following limitations: (1) at most
michael@0 54 // one reader is allowed at once, and (2) any thread that tries to
michael@0 55 // read-acquire the lock more than once will deadlock. (2) could be
michael@0 56 // avoided if it were possible to use recursive pthread_mutex_t's.
michael@0 57
michael@0 58 #elif defined(LUL_OS_android)
michael@0 59
michael@0 60 LulRWLock::LulRWLock() {
michael@0 61 mozilla::DebugOnly<int> r = pthread_mutex_init(&mLock, nullptr);
michael@0 62 MOZ_ASSERT(!r);
michael@0 63 }
michael@0 64
michael@0 65 LulRWLock::~LulRWLock() {
michael@0 66 mozilla::DebugOnly<int>r = pthread_mutex_destroy(&mLock);
michael@0 67 MOZ_ASSERT(!r);
michael@0 68 }
michael@0 69
michael@0 70 void
michael@0 71 LulRWLock::WrLock() {
michael@0 72 mozilla::DebugOnly<int>r = pthread_mutex_lock(&mLock);
michael@0 73 MOZ_ASSERT(!r);
michael@0 74 }
michael@0 75
michael@0 76 void
michael@0 77 LulRWLock::RdLock() {
michael@0 78 mozilla::DebugOnly<int>r = pthread_mutex_lock(&mLock);
michael@0 79 MOZ_ASSERT(!r);
michael@0 80 }
michael@0 81
michael@0 82 void
michael@0 83 LulRWLock::Unlock() {
michael@0 84 mozilla::DebugOnly<int>r = pthread_mutex_unlock(&mLock);
michael@0 85 MOZ_ASSERT(!r);
michael@0 86 }
michael@0 87
michael@0 88
michael@0 89 #else
michael@0 90 # error "Unsupported OS"
michael@0 91 #endif
michael@0 92
michael@0 93 } // namespace lul

mercurial