-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server_cache.cc
103 lines (90 loc) · 2.8 KB
/
lock_server_cache.cc
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
// the caching lock server implementation
#include "lock_server_cache.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "lang/verify.h"
#include "tprintf.h"
lock_server_cache::lock_server_cache()
{
VERIFY(pthread_mutex_init(&mutex, NULL) == 0);
}
void
lock_server_cache::revoke_owner(lock_protocol::lockid_t lid, handle &h){
rpcc* cl = h.safebind();
if(cl){
int r;
cl->call(rlock_protocol::revoke, lid, r);
}
}
int lock_server_cache::acquire(lock_protocol::lockid_t lid, std::string id,
int &)
{
tprintf("acquire request: lid=>%llu id=>%s\n", lid, id.c_str());
VERIFY(pthread_mutex_lock(&mutex) == 0);
if(locks.find(lid) == locks.end())
{
locks[lid].owner = id;
VERIFY(pthread_mutex_unlock(&mutex) == 0);
return lock_protocol::OK;
}else{
locks[lid].waiting.push(id);
if(locks[lid].waiting.size() > 1){
tprintf("waiting size is: %u\n", locks[lid].waiting.size());
VERIFY(pthread_mutex_unlock(&mutex) == 0);
return lock_protocol::RETRY;
}else{
// send a revoke to the owner of this lock
handle h(locks[lid].owner);
VERIFY(pthread_mutex_unlock(&mutex) == 0);
revoke_owner(lid, h);
return lock_protocol::RETRY;
}
}
return lock_protocol::NOENT;
}
int
lock_server_cache::release(lock_protocol::lockid_t lid, std::string id,
int &r)
{
tprintf("release request: lid=>%llu id=>%s\n", lid, id.c_str());
VERIFY(pthread_mutex_lock(&mutex) == 0);
if(locks.find(lid) != locks.end()){
locks[lid].owner = "";
if(locks[lid].waiting.size() == 0){ // remove this lock's info
locks.erase(lid);
VERIFY(pthread_mutex_unlock(&mutex) == 0);
}else{
// send a retry to the first waiting client
handle h(locks[lid].waiting.front());
VERIFY(pthread_mutex_unlock(&mutex) == 0);
rpcc* cl = h.safebind();
if(cl){
int r;
cl->call(rlock_protocol::retry, lid, r);
VERIFY(pthread_mutex_lock(&mutex) == 0);
locks[lid].owner = locks[lid].waiting.front();
locks[lid].waiting.pop();
tprintf("CHANGE WAITING TO OWNER NEW OWNER: %s\n", locks[lid].owner.c_str());
// if there are other clients waiting, send a reovke to the owner
if(locks[lid].waiting.size() > 0){
handle hh(locks[lid].owner);
VERIFY(pthread_mutex_unlock(&mutex) == 0);
revoke_owner(lid, hh);
}else{
VERIFY(pthread_mutex_unlock(&mutex) == 0);
}
}
}
return lock_protocol::OK;
}
return lock_protocol::NOENT;
}
lock_protocol::status
lock_server_cache::stat(lock_protocol::lockid_t lid, int &r)
{
tprintf("stat request: lid=>%llu\n", lid);
r = nacquire;
return lock_protocol::OK;
}