Imported from 6.824 labs
[invirt/third/libt4.git] / lock_client_cache_rsm.h
1 // lock client interface.
2
3 #ifndef lock_client_cache_rsm_h
4
5 #define lock_client_cache_rsm_h
6
7 #include <string>
8 #include "lock_protocol.h"
9 #include "rpc.h"
10 #include "lock_client.h"
11 #include "lang/verify.h"
12 #include "mutex.h"
13 #include "rpc/fifo.h"
14 #include "rsm_client.h"
15
16 // Classes that inherit lock_release_user can override dorelease so that
17 // that they will be called when lock_client releases a lock.
18 // You will not need to do anything with this class until Lab 5.
19 class lock_release_user {
20     public:
21         virtual void dorelease(lock_protocol::lockid_t) = 0;
22         virtual ~lock_release_user() {};
23 };
24
25 using namespace std;
26
27 typedef string callback;
28
29 class lock_state {
30 public:
31     lock_state();
32     enum {
33         none = 0,
34         retrying,
35         free,
36         locked,
37         acquiring,
38         releasing
39     } state;
40     pthread_t held_by;
41     list<pthread_t> wanted_by;
42     mutex m;
43     map<pthread_t, cond> c;
44     lock_protocol::xid_t xid;
45     void wait();
46     void signal();
47     void signal(pthread_t who);
48 };
49
50 typedef map<lock_protocol::lockid_t, lock_state> lock_map;
51
52 class lock_client_cache_rsm;
53
54 // Clients that caches locks.  The server can revoke locks using
55 // lock_revoke_server.
56 class lock_client_cache_rsm : public lock_client {
57     private:
58         pthread_t releaser_thread;
59         rsm_client *rsmc;
60         class lock_release_user *lu;
61         int rlock_port;
62         string hostname;
63         string id;
64         mutex xid_mutex;
65         lock_protocol::xid_t xid;
66         fifo<lock_protocol::lockid_t> release_fifo;
67         mutex lock_table_lock;
68         lock_map lock_table;
69         lock_state &get_lock_state(lock_protocol::lockid_t lid);
70     public:
71         static int last_port;
72         lock_client_cache_rsm(string xdst, class lock_release_user *l = 0);
73         virtual ~lock_client_cache_rsm() {};
74         lock_protocol::status acquire(lock_protocol::lockid_t);
75         virtual lock_protocol::status release(lock_protocol::lockid_t);
76         void releaser();
77         rlock_protocol::status revoke_handler(lock_protocol::lockid_t, lock_protocol::xid_t, int &);
78         rlock_protocol::status retry_handler(lock_protocol::lockid_t, lock_protocol::xid_t, int &);
79 };
80
81
82 #endif