-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server.cc
64 lines (51 loc) · 1.58 KB
/
lock_server.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
// the lock server implementation
#include "lock_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <map>
lock_server::lock_server():
nacquire (0),request_map(),request_mutex(), cv_()
{
}
lock_protocol::status
lock_server::stat(int clt, lock_protocol::lockid_t lid, int &r)
{
lock_protocol::status ret = lock_protocol::OK;
printf("stat request from clt %d\n", clt);
r = nacquire;
return ret;
}
lock_protocol::status
lock_server::grant(int clt, lock_protocol::lockid_t lid, int & r) {
printf("grant request from clt %d\n", clt);
ScopedLock sl(&request_mutex);
lock_protocol::status ret = lock_protocol::OK;
std::map<lock_protocol::lockid_t,bool>::iterator iter = request_map.find(lid);
if (iter == request_map.end()) {
//new lid
request_map.insert(std::make_pair(lid,true));
} else {
while(iter->second) {
VERIFY(pthread_cond_wait(&cv_,&request_mutex) == 0);
}
//if here, the lock is free, so thread can take it
iter->second = true;
}
r = nacquire;
return ret;
}
lock_protocol::status lock_server::release(int clt, lock_protocol::lockid_t lid, int& r) {
printf("release request from clt %d\n", clt);
ScopedLock sl(&request_mutex);
std::map<lock_protocol::lockid_t,bool>::iterator iter = request_map.find(lid);
//we can trust that the client is only going to release locks it actually has:
VERIFY(iter != request_map.end());
VERIFY(iter->second);
//release the lock:
iter->second = false;
VERIFY(pthread_cond_broadcast(&cv_) == 0);
r = nacquire;
return lock_protocol::OK;
}