-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathlock.cc
More file actions
126 lines (102 loc) · 3.34 KB
/
lock.cc
File metadata and controls
126 lines (102 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 "io/lock.h"
#include "io/io_utils.h"
namespace tera {
namespace io {
LockManager::LockManager() {
}
LockManager::~LockManager() {
}
bool LockManager::Lock(const std::string& key, uint64_t id,
const std::string& annotation, StatusCode* status) {
MutexLock l(&mutex_);
LockIterator it = lock_map_.find(key);
if (it == lock_map_.end()) {
LockNode& lock_node = lock_map_[key];
lock_node.id = id;
lock_node.annotation = annotation;
VLOG(10) << "lock key: " << key << " id: " << id
<< " annotation: " << annotation;
return true;
}
LockNode& ln = it->second;
if (ln.id == id) {
SetStatusCode(kLockDoubleLock, status);
} else {
SetStatusCode(kLockNotOwn, status);
}
VLOG(10) << "fail to lock key: " << key << " id: " << id
<< " annotation: " << annotation
<< " status: " << StatusCodeToString(*status);
return false;
}
bool LockManager::Unlock(const std::string& key, uint64_t id,
std::string* annotation, StatusCode* status) {
MutexLock l(&mutex_);
LockIterator it;
if (!GetLockNode(key, id, &it, status)) {
VLOG(10) << "fail to unlock key: " << key << " id: " << id
<< " status: " << StatusCodeToString(*status);
return false;
}
if (annotation != NULL) {
annotation->assign(it->second.annotation);
}
lock_map_.erase(it);
VLOG(10) << "unlock key: " << key << " id: " << id
<< " annotation: " << it->second.annotation;
return true;
}
bool LockManager::IsLocked(const std::string& key, uint64_t id,
std::string* annotation, StatusCode* status) {
MutexLock l(&mutex_);
LockIterator it;
if (!GetLockNode(key, id, &it, status)) {
return false;
}
LockNode& ln = it->second;
if (annotation != NULL) {
annotation->assign(ln.annotation);
}
return true;
}
bool LockManager::IsLocked(const std::string& key, uint64_t* id,
std::string* annotation, StatusCode* status) {
MutexLock l(&mutex_);
LockIterator it = lock_map_.find(key);
if (it == lock_map_.end()) {
SetStatusCode(kLockNotExist, status);
return false;
}
LockNode& ln = it->second;
if (id != NULL) {
*id = ln.id;
}
if (annotation != NULL) {
annotation->assign(ln.annotation);
}
return true;
}
bool LockManager::GetLockNode(const std::string& key, uint64_t id,
LockIterator* ret_it, StatusCode* status) {
mutex_.AssertHeld();
LockIterator it = lock_map_.find(key);
if (it == lock_map_.end()) {
VLOG(10) << "lock not exist, key: " << key << " id: " << id;
SetStatusCode(kLockNotExist, status);
return false;
}
LockNode& ln = it->second;
if (ln.id != id) {
VLOG(10) << "lock not own, key: " << key << " id: " << id
<< ", real id: " << ln.id << ", annotation: " << ln.annotation;
SetStatusCode(kLockNotOwn, status);
return false;
}
*ret_it = it;
return true;
}
} // namespace tabletnode
} // namespace tera