-
Notifications
You must be signed in to change notification settings - Fork 5
/
lock_client_cache_rsm.h
88 lines (68 loc) · 2.44 KB
/
lock_client_cache_rsm.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
// lock client interface.
#ifndef lock_client_cache_rsm_h
#define lock_client_cache_rsm_h
#include <string>
#include <map>
#include "lock_protocol.h"
#include "rpc.h"
#include "lock_client.h"
#include "lang/verify.h"
#include "rsm_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_client_cache_rsm;
// Clients that caches locks. The server can revoke locks using
// lock_revoke_server.
class lock_client_cache_rsm {
private:
rsm_client *rsmc;
enum lock_status {
none, // client knows nothing about this lock
free, // client owns the lock and no thread has it
locked, // client owns the lock and a thread has it
acquiring, // the client is acquiring ownership
releasing, // the client is releasing ownership
};
struct lock_t {
lock_status status;
bool revoked;
bool should_retry;
pthread_cond_t free_c; // notify when status == lock_status::free || none
pthread_cond_t retry_c; // notify when should_retry == true
pthread_t owner; // thread id of owner
lock_t()
: status(lock_status::none),
revoked(false), should_retry(false), owner(0) {
pthread_cond_init(&free_c, NULL);
pthread_cond_init(&retry_c, NULL);
}
};
class lock_release_user *lu;
std::string id;
lock_protocol::xid_t xid;
std::map<lock_protocol::lockid_t, lock_t> locks;
pthread_mutex_t m;
public:
static int last_port;
lock_client_cache_rsm(std::string xdst, class lock_release_user *l = NULL);
virtual ~lock_client_cache_rsm() { }
lock_protocol::status acquire(lock_protocol::lockid_t);
lock_protocol::status release(lock_protocol::lockid_t);
lock_protocol::status release(lock_protocol::lockid_t, bool);
rlock_protocol::status revoke_handler(lock_protocol::lockid_t, lock_protocol::xid_t, int &);
rlock_protocol::status retry_handler(lock_protocol::lockid_t, lock_protocol::xid_t, int &);
private:
lock_protocol::status acquire_impl(
lock_protocol::lockid_t,
std::map<lock_protocol::lockid_t, lock_t>::iterator);
lock_protocol::status release_impl(
lock_protocol::lockid_t lid,
std::map<lock_protocol::lockid_t, lock_t>::iterator);
};
#endif