-
Notifications
You must be signed in to change notification settings - Fork 16
/
lock_client_cache_rsm.h
118 lines (96 loc) · 4.23 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
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
// lock client interface.
#ifndef lock_client_cache_rsm_h
#define lock_client_cache_rsm_h
#include <string>
#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.
//
//
// 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.
/*
Lock Client Cache
-----------------
clientcacheMap: Stores the state of locks(as per below struct) in a Map.
struct lock_cache_value {
l_cache_state lock_cache_state; // State of the lock cache
thread_mutex_t client_lock_mutex; // To protect this structure
pthread_cond_t client_lock_cv; // CV to be notified of state chang
pthread_cond_t client_revoke_cv; // CV to be notified if lock is revoked
};
client_cache_mutex: To protect clientcacheMap
It supports acquire, release and listens on revoke and retry.
Releaser, Retryer Threads:
-On init, we spawn two threads, releaser and retryer (mutex and CVs are dedicated for each for synchronization)
-Releaser will listen on a list and release the lock if it is in FREE state or set the state to RELEASING and
wait on client_revoke_cv to be set by the next release request from the client. Once it is released by the client,
it sends release request to server
-Retryer will listen on a list and sends acquire request to server for each lockid. Upon successful response, it notifies
threads that are waiting on client_lock_cv.
Acquire:
The course of action depends on the state of the lockid.
1)NONE: We send acquire to server and set the state to ACQUIRING. On OK response, state is set to LOCKED.
On RETRY response, we wait on client_lock_cv. Upon wakeup, if the state is not FREE, we loop back so that the next course of
action depends on the current state
2)FREE: State is set to LOCKED
3)Otherwise: we wait on client_lock_cv. Upon wakeup, if the state is not FREE, we loop back so that the next course of
action depends on the current state
Release:
If the state of lock is RELEASING, we signal client_revoke_cv. For LOCKED, we signal client_lock_cv.
*/
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 : public lock_client {
private:
rsm_client *rsmc;
class lock_release_user *lu;
int rlock_port;
std::string hostname;
std::string id;
lock_protocol::xid_t xid;
protected:
enum lock_cache_state { NONE, FREE, LOCKED, ACQUIRING, RELEASING };
typedef int l_cache_state;
struct lock_cache_value {
bool doflush; // This is used to track if the lock has been locked atleast once
l_cache_state lock_cache_state; // State of the lock cache
pthread_mutex_t client_lock_mutex; // To protect this structure
pthread_cond_t client_lock_cv; // CV to be notified of state change
pthread_cond_t client_revoke_cv; // CV to be notified if lock is revoked
};
typedef std::map<lock_protocol::lockid_t, lock_cache_value *> TLockCacheStateMap;
TLockCacheStateMap clientcacheMap;
pthread_mutex_t client_cache_mutex;
pthread_cond_t client_cache_cv;
pthread_mutex_t client_retry_mutex, client_releaser_mutex;
pthread_cond_t client_retry_cv, client_releaser_cv;
std::list<lock_protocol::lockid_t> retry_list;
std::list<lock_protocol::lockid_t> revoke_list;
lock_cache_value* get_lock_obj(lock_protocol::lockid_t lid);
public:
static int last_port;
lock_client_cache_rsm(std::string xdst, class lock_release_user *l = 0);
virtual ~lock_client_cache_rsm() {};
lock_protocol::status acquire(lock_protocol::lockid_t);
virtual lock_protocol::status release(lock_protocol::lockid_t);
void releaser();
void retryer(void);
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 &);
};
#endif