From 9d131b35b69da0c0e8ef95163fe85eeeb05db260 Mon Sep 17 00:00:00 2001 From: Terrence Cole Date: Thu, 3 Mar 2016 10:27:57 -0800 Subject: [PATCH] Bug 956899 - Add a std::lock_guard work-alike; r=froydnj --HG-- extra : rebase_source : c80c16798e2785f642a735bf4a24f859e202bee7 --- js/src/jsapi-tests/testThreadingMutex.cpp | 9 +++++++ js/src/threading/LockGuard.h | 31 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 js/src/threading/LockGuard.h diff --git a/js/src/jsapi-tests/testThreadingMutex.cpp b/js/src/jsapi-tests/testThreadingMutex.cpp index 24962788ea4a..404cb122d827 100644 --- a/js/src/jsapi-tests/testThreadingMutex.cpp +++ b/js/src/jsapi-tests/testThreadingMutex.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "jsapi-tests/tests.h" +#include "threading/LockGuard.h" #include "threading/Mutex.h" BEGIN_TEST(testThreadingMutex) @@ -16,3 +17,11 @@ BEGIN_TEST(testThreadingMutex) return true; } END_TEST(testThreadingMutex) + +BEGIN_TEST(testThreadingLockGuard) +{ + js::Mutex mutex; + js::LockGuard guard(mutex); + return true; +} +END_TEST(testThreadingLockGuard) diff --git a/js/src/threading/LockGuard.h b/js/src/threading/LockGuard.h new file mode 100644 index 000000000000..41e30c23c3f1 --- /dev/null +++ b/js/src/threading/LockGuard.h @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef threading_LockGuard_h +#define threading_LockGuard_h + +namespace js { + +template +class MOZ_RAII LockGuard +{ + Mutex& lock; + +public: + explicit LockGuard(Mutex& aLock) + : lock(aLock) + { + lock.lock(); + } + + ~LockGuard() { + lock.unlock(); + } +}; + +} // namespace js + +#endif // threading_LockGuard_h