forked from bohaoist/yfs2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_client_cache.h
90 lines (81 loc) · 2.28 KB
/
lock_client_cache.h
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
// lock client interface.
#ifndef lock_client_cache_h
#define lock_client_cache_h
#include <string>
#include "lock_protocol.h"
#include "rpc.h"
#include "lock_client.h"
#include "lang/verify.h"
#include "extent_client.h"
// Classes that inherit lock_release_user can override dorelease so that
// that they will be called when lock_client releases a lock.
// You will not need to do anything with this class until Lab 5.
class lock_release_user {
public:
virtual void dorelease(lock_protocol::lockid_t) = 0;
virtual ~lock_release_user() {};
};
class lock_release_client : public lock_release_user
{
public:
lock_release_client(extent_client* ec)
: ec_(ec)
{
}
void dorelease(lock_protocol::lockid_t id) override
{
extent_protocol::extentid_t eid = id;
ec_->flush(eid);
}
~lock_release_client() override = default;
private:
extent_client* ec_;
};
class lock_client_cache : public lock_client {
private:
class lock_release_user *lu;
int rlock_port;
std::string hostname;
std::string id;
enum class CacheState
{
NONE,
FREE,
LOCKED,
ACQUIRING,
RELEASING
};
class CacheInfo
{
public:
CacheInfo()
: threadWaiterNum_(0),
threadOwner_(0),
cachestate_(CacheState::NONE),
cond_(),
isRevoked(false)
{
}
CacheInfo(CacheInfo&) = delete;
CacheInfo& operator=(const CacheInfo&) = delete;
uint32_t threadWaiterNum_{0};
pthread_t threadOwner_{0};
CacheState cachestate_{CacheState::NONE};
std::condition_variable cond_{};
bool isRevoked{false};
int retryR_{0};
};
std::map<lock_protocol::lockid_t,
lock_client_cache::CacheInfo> lockInfos_;
public:
static int last_port;
lock_client_cache(std::string xdst, class lock_release_user *l = 0);
virtual ~lock_client_cache() {};
lock_protocol::status acquire(lock_protocol::lockid_t) override;
lock_protocol::status release(lock_protocol::lockid_t) override;
rlock_protocol::status revoke_handler(lock_protocol::lockid_t,
int &);
rlock_protocol::status retry_handler(lock_protocol::lockid_t, int retryR,
int &);
};
#endif