Use C++ 11 atomic_flag in MacOS as spin lock (#175)

* Use C++ 11 atomic_flag in MacOS as spin lock
* Add unittest for it.
avx_docs
gangliao 9 years ago committed by Yu Yang
parent 191fafe355
commit 9e11ca8096

@ -57,7 +57,7 @@ std::vector<ParameterPtr> trainerOnePassTest(const string& configFile,
<< " sparseUpdate=" << sparseUpdate;
srand(FLAGS_seed);
*ThreadLocalRand::getSeed() = FLAGS_seed;
ThreadLocalRandomEngine::get().seed(FLAGS_seed);
if (useGpu) {
CHECK_LE(trainerCount, gNumDevices);
}

@ -15,12 +15,9 @@ limitations under the License. */
#include "paddle/utils/Locks.h"
#include "paddle/utils/Logging.h"
#include <dispatch/dispatch.h>
#include <atomic>
#include <libkern/OSAtomic.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
#include <os/lock.h>
#endif
namespace paddle {
class SemaphorePrivate {
@ -55,12 +52,7 @@ void Semaphore::post() {
class SpinLockPrivate {
public:
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
os_unfair_lock lock_;
#else
SpinLockPrivate(): lock_(OS_SPINLOCK_INIT) {}
OSSpinLock lock_;
#endif
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
char padding_[64 - sizeof(lock_)]; // Padding to cache line size
};
@ -68,19 +60,11 @@ SpinLock::SpinLock(): m(new SpinLockPrivate()) {}
SpinLock::~SpinLock() { delete m; }
void SpinLock::lock() {
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
os_unfair_lock_lock(&m->lock_);
#else
OSSpinLockLock(&m->lock_);
#endif
while (m->lock_.test_and_set(std::memory_order_acquire)) {}
}
void SpinLock::unlock() {
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
os_unfair_lock_unlock(&m->lock_);
#else
OSSpinLockUnlock(&m->lock_);
#endif
m->lock_.clear(std::memory_order_release);
}

@ -4,6 +4,7 @@ add_simple_unittest(test_Thread)
add_simple_unittest(test_StringUtils)
add_simple_unittest(test_CustomStackTrace)
add_simple_unittest(test_ThreadBarrier)
add_simple_unittest(test_SpinLock)
add_executable(
test_CustomStackTracePrint

@ -0,0 +1,57 @@
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <gtest/gtest.h>
#include <vector>
#include "paddle/utils/Logging.h"
#include "paddle/utils/CommandLineParser.h"
#include "paddle/utils/Util.h"
#include "paddle/utils/Locks.h"
P_DEFINE_int32(test_thread_num, 100, "testing thread number");
void testNormalImpl(size_t thread_num, const std::function
<void(size_t, size_t&, paddle::SpinLock&)>& callback) {
paddle::SpinLock mutex;
std::vector<std::thread> threads;
threads.reserve(thread_num);
size_t count = 0;
for (size_t i = 0; i < thread_num; ++i) {
threads.emplace_back([&thread_num, &count, &mutex, &callback]{
callback(thread_num, count, mutex);
});
}
for (auto& thread : threads) {
thread.join();
}
// Check whether all threads reach this point or not
CHECK_EQ(count, thread_num);
}
TEST(ThreadSpinLock, normalTest) {
for (auto &thread_num : {10, 30, 50 , 100 , 300, 1000}) {
testNormalImpl(thread_num, [](size_t thread_num,
size_t& count, paddle::SpinLock& mutex){
std::lock_guard<paddle::SpinLock> lock(mutex);
++count;
});
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
paddle::initMain(argc, argv);
return RUN_ALL_TESTS();
}
Loading…
Cancel
Save