-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server_cache.cc
189 lines (178 loc) · 5.13 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// the caching lock server implementation
#include "lock_server_cache.h"
#include <sstream>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <arpa/inet.h>
#include "lang/verify.h"
#include "handle.h"
#include "tprintf.h"
#include <queue>
#include <pthread.h>
lock_server_cache::lock_server_cache()
{
pthread_mutex_init(&m,NULL);
}
int lock_server_cache::acquire(lock_protocol::lockid_t lid, std::string id,
int &r)
{
tprintf("***client %s acquire lock %llu \n",id.c_str(),lid);
lock_protocol::status ret = lock_protocol::OK;
pthread_mutex_lock(&m);
if(locks.count(lid) == 0){
tprintf("****new lock\n");
lock l;
l.stat = false;
l.id = id;
locks.insert(std::map<lock_protocol::lockid_t, lock>::value_type(lid,l));
r = 1;
pthread_mutex_unlock(&m);
} else {
if(locks[lid].stat == false){
tprintf("****lock is occupied client %s has to wait lock %llu \n",id.c_str(),lid);
locks[lid].q.push(id);
if(locks[lid].q.size() > 1){
tprintf("***\ndo not need to revoke\n***\n");
pthread_mutex_unlock(&m);
return lock_protocol::RETRY;
}
std::string holder = locks[lid].id;
pthread_mutex_unlock(&m);
handle h(holder);
rpcc *cl = h.safebind();
lock_protocol::status ret2;
tprintf("****revoke %s", holder.c_str());
int a = 0;
if(cl){
ret2 = cl->call(rlock_protocol::revoke, lid, a);
tprintf("return %i a\n",a);
} else {
return lock_protocol::RPCERR;
}
tprintf("****\n%sacquire succeed retry\n***\n",id.c_str());
return lock_protocol::RETRY;
/*
locks[lid].q.push(id);
while(locks[lid].q.push(id).size() > 0){
std::string holder = locks[lid].id;
std::string nextholder = locks[lid].q.front();
locks[lid].q.pop();
pthread_mutex_unlock(&m);
handle h(holder);
rpcc *cl = h.safebind();
int a = 0;
lock_protocol::status ret2;
tprintf("****revoke %s", holder.c_str());
if(cl){
ret2 = cl->call(rlock_protocol::revoke, lid, a);
tprintf("return %i a\n",a);
} else {
return lock_protocol::RPCERR;
}
if(a == 0)
break;
else{
pthread_mutex_lock(&m);
handle h(holder);
rpcc *cl = h.safebind();
if(cl){
cl->call(rlock_protocol::retry, lid, a);
} else return lock_protocol::RPCERR;
}
pthread_mutex_lock(&m);
}
*/
/*
tprintf("****client %s has to wait lock %llu \n",id.c_str(),lid);
std::string holder = locks[lid].id;
ret = lock_protocol::RETRY;
pthread_mutex_unlock(&m);
handle h(holder);
rpcc *cl= h.safebind();
int a = 0;
lock_protocol::status ret2;
tprintf("****revoke %s", holder.c_str());
if(cl){
ret2 = cl->call(rlock_protocol::revoke, lid, a);
tprintf("return %i a\n",a);
} else {
return lock_protocol::RPCERR;
}
if(a == 1){
tprintf("****client %s lock %llu xidangdie size %i\n",id.c_str(),lid, locks[lid].q.size());
pthread_mutex_lock(&m);
locks[lid].stat = false;
locks[lid].id = id;
if(locks[lid].q.size() > 0){
pthread_mutex_unlock(&m);
handle h(id);
rpcc *cl= h.safebind();
if(cl){
ret2 = cl->call(rlock_protocol::revoke, lid, a);
} else {
return lock_protocol::RPCERR;
}
}
pthread_mutex_unlock(&m);
return lock_protocol::OK;
}
locks[lid].q.push(id);
tprintf("****queue grow %i \n",locks[lid].q.size());
*/
}else{
locks[lid].stat = false;
locks[lid].id = id;
pthread_mutex_unlock(&m);
}
}
tprintf("***%s*acquire succeed\n",id.c_str());
return ret;
}
int
lock_server_cache::release(lock_protocol::lockid_t lid, std::string id,
int &r)
{
tprintf("***client %s release lock %llu \n",id.c_str(),lid);
pthread_mutex_lock(&m);
if(locks.count(lid) != 0){
if(locks[lid].q.empty()){
locks[lid].stat = true;
pthread_mutex_unlock(&m);
}else{
//while(!locks[lid].q.empty()){
std::string nextowner = locks[lid].q.front();
locks[lid].q.pop();
locks[lid].id = nextowner;
tprintf("****lock %llu has a queue size %i and next is %s \n",lid,locks[lid].q.size(), nextowner.c_str());
pthread_mutex_unlock(&m);
handle h(nextowner);
rpcc *cl = h.safebind();
int a = 0;
if(cl){
cl->call(rlock_protocol::retry, lid, a);
pthread_mutex_lock(&m);
if(!locks[lid].q.empty()){
tprintf("****lock %llu 's queue is long\n",lid);
pthread_mutex_unlock(&m);
cl->call(rlock_protocol::revoke, lid,a);
}
} else {
return lock_protocol::RPCERR;
}
//if(a == 0){
// break;
//}
pthread_mutex_unlock(&m);
}
}
lock_protocol::status ret = lock_protocol::OK;
return ret;
}
lock_protocol::status
lock_server_cache::stat(lock_protocol::lockid_t lid, int &r)
{
tprintf("stat request\n");
r = nacquire;
return lock_protocol::OK;
}