-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use shared_ptr for Read/WriteLockGuard
- Loading branch information
Showing
6 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
|
||
#include "nameserver/file_lock.h" | ||
|
||
#include <common/logging.h> | ||
|
||
namespace baidu { | ||
namespace bfs { | ||
|
||
FileLockManager* WriteLock::file_lock_manager_ = NULL; | ||
FileLockManager* ReadLock::file_lock_manager_ = NULL; | ||
|
||
WriteLock::WriteLock(const std::string& file_path) { | ||
file_path_.push_back(file_path); | ||
file_lock_manager_->WriteLock(file_path); | ||
} | ||
|
||
WriteLock::WriteLock(const std::string& file_path_a, | ||
const std::string& file_path_b) { | ||
int r = strcmp(file_path_a.c_str(), file_path_b.c_str()); | ||
if (r == 0) { | ||
file_path_.push_back(file_path_a); | ||
file_lock_manager_->WriteLock(file_path_a); | ||
} else if (r < 0) { | ||
file_path_.push_back(file_path_a); | ||
file_path_.push_back(file_path_b); | ||
file_lock_manager_->WriteLock(file_path_a); | ||
file_lock_manager_->WriteLock(file_path_b); | ||
} else { | ||
file_path_.push_back(file_path_b); | ||
file_path_.push_back(file_path_a); | ||
file_lock_manager_->WriteLock(file_path_b); | ||
file_lock_manager_->WriteLock(file_path_a); | ||
} | ||
} | ||
|
||
WriteLock::~WriteLock() { | ||
if (file_path_.size() == 1) { | ||
file_lock_manager_->Unlock(file_path_[0]); | ||
} else { | ||
file_lock_manager_->Unlock(file_path_[1]); | ||
file_lock_manager_->Unlock(file_path_[0]); | ||
} | ||
} | ||
|
||
void WriteLock::SetFileLockManager(FileLockManager* file_lock_manager) { | ||
file_lock_manager_ = file_lock_manager; | ||
} | ||
|
||
ReadLock::ReadLock(const std::string& file_path) { | ||
file_path_ = file_path; | ||
file_lock_manager_->ReadLock(file_path); | ||
} | ||
|
||
ReadLock::~ReadLock() { | ||
file_lock_manager_->Unlock(file_path_); | ||
} | ||
|
||
void ReadLock::SetFileLockManager(FileLockManager* file_lock_manager) { | ||
file_lock_manager_ = file_lock_manager; | ||
} | ||
|
||
} // namespace bfs | ||
} // namespace baidu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
|
||
#ifndef BFS_FILE_LOCK_H_ | ||
#define BFS_FILE_LOCK_H_ | ||
|
||
#include "nameserver/file_lock_manager.h" | ||
|
||
#include <vector> | ||
#include <memory> | ||
|
||
namespace baidu { | ||
namespace bfs { | ||
|
||
class FileLockManager; | ||
|
||
class Lock { | ||
public: | ||
virtual ~Lock() {} | ||
}; | ||
|
||
class WriteLock : public Lock { | ||
public: | ||
WriteLock(const std::string& file_path); | ||
WriteLock(const std::string& file_path_a, | ||
const std::string& file_path_b); | ||
~WriteLock(); | ||
static void SetFileLockManager(FileLockManager* file_lock_manager); | ||
private: | ||
// will be initialized in NameServerImpl's constructor | ||
static FileLockManager* file_lock_manager_; | ||
std::vector<std::string> file_path_; | ||
}; | ||
|
||
class ReadLock : public Lock { | ||
public: | ||
ReadLock(const std::string& file_path); | ||
~ReadLock(); | ||
static void SetFileLockManager(FileLockManager* file_lock_manager); | ||
private: | ||
// will be initialized in NameServerImpl's constructor | ||
static FileLockManager* file_lock_manager_; | ||
std::string file_path_; | ||
}; | ||
|
||
typedef std::shared_ptr<Lock> FileLockGuard; | ||
|
||
} // namespace bfs | ||
} // namespace baidu | ||
|
||
#endif //BFS_FILE_LOCK_H_ | ||
|
||
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
// | ||
|
||
#define private public | ||
|
||
#include <nameserver/file_lock.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace baidu { | ||
namespace bfs { | ||
|
||
class FileLockTest : public ::testing::Test { | ||
public: | ||
FileLockTest() {} | ||
protected: | ||
}; | ||
FileLockManager file_lock_manager; | ||
|
||
void SetFileLockManager() { | ||
WriteLock::file_lock_manager_ = &file_lock_manager; | ||
ReadLock::file_lock_manager_ = &file_lock_manager; | ||
} | ||
|
||
TEST_F(FileLockTest, WriteLockForOneFile) { | ||
FileLockGuard guard1(new WriteLock("/home/dir1/file1")); | ||
FileLockGuard guard2(new WriteLock("/home/dir1/file2")); | ||
} | ||
|
||
TEST_F(FileLockTest, WriteLockForTwoFile) { | ||
FileLockGuard guard2(new WriteLock("/home/dir1/file2", "/home/dir1/file1")); | ||
} | ||
|
||
TEST_F(FileLockTest, ReadLock) { | ||
FileLockGuard guard1(new ReadLock("/home/dir1/file1")); | ||
FileLockGuard guard2(new ReadLock("/home/dir1/file2")); | ||
} | ||
|
||
} // namespace bfs | ||
} // namespace baidu | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
baidu::bfs::SetFileLockManager(); | ||
baidu::common::SetLogLevel(2); | ||
return RUN_ALL_TESTS(); | ||
} |