-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_server_cache.h
94 lines (79 loc) · 2.39 KB
/
lock_server_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
91
92
93
94
#ifndef lock_server_cache_h
#define lock_server_cache_h
#include <string>
#include <map>
#include <queue>
#include "lock_protocol.h"
#include "rpc.h"
#include "slock.h"
#include "rsm_state_transfer.h"
#include "rsm.h"
template<class T>
class events_queue {
private:
std::list<T> queue;
pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t consume_signal = PTHREAD_COND_INITIALIZER;
public:
void add(T t) {
ScopedLock guard(&queue_mutex);
queue.push_back(t);
pthread_cond_signal(&consume_signal);
}
T consume() {
ScopedLock guard(&queue_mutex);
while (queue.empty()) {
pthread_cond_wait(&consume_signal, &queue_mutex);
}
T retval = queue.front();
queue.pop_front();
return retval;
}
};
class Client {
public:
int clt;
unsigned int seq;
Client() {}
Client(int clt, unsigned int seq) {
this->clt = clt;
this->seq = seq;
}
friend marshall &operator<<(marshall &os, const Client &client);
friend unmarshall &operator>>(unmarshall &is, Client &client);
};
bool operator==(const Client &lhs, const Client &rhs);
bool operator!=(const Client &lhs, const Client &rhs);
class lock_info {
public:
enum Status { FREE, LOCKED, REVOKING };
lock_info::Status status;
Client owning_client;
std::queue<Client> waiting_clients;
lock_info() {
status = FREE;
}
friend marshall &operator<<(marshall &os, const lock_info &lock);
friend unmarshall &operator>>(unmarshall &is, lock_info &lock);
};
class lock_server_cache: public rsm_state_transfer {
private:
class rsm *rsm;
public:
lock_server_cache(class rsm *rsm = 0);
std::map<int, rpcc *> clients;
pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;
std::map<lock_protocol::lockid_t, lock_info> cached_locks;
events_queue<lock_protocol::lockid_t> revoke_queue;
events_queue<lock_protocol::lockid_t> retry_queue;
lock_server_cache();
lock_protocol::status stat(lock_protocol::lockid_t, int &);
void revoker();
void retryer();
lock_protocol::status subscribe(int clt, std::string dst, int &);
lock_protocol::status acquire(int clt, lock_protocol::lockid_t lid, unsigned int seq, int &);
lock_protocol::status release(int clt, lock_protocol::lockid_t lid, unsigned int seq, int &);
std::string marshal_state() override;
void unmarshal_state(std::string) override;
};
#endif