815224c16940ab5b32d7483ea48e9f9b2c08363b
[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/rpc.h"
10 #include "lock_client.h"
11 #include "lang/verify.h"
12 #include "rpc/fifo.h"
13 #include "rsm_client.h"
14
15 class lock_release_user {
16     public:
17         virtual void dorelease(lock_protocol::lockid_t) = 0;
18         virtual ~lock_release_user() {};
19 };
20
21 using std::string;
22 using std::thread;
23 using std::list;
24 using std::map;
25
26 typedef string callback;
27
28 class lock_state {
29 public:
30     lock_state();
31     enum {
32         none = 0,
33         retrying,
34         free,
35         locked,
36         acquiring,
37         releasing
38     } state;
39     std::thread::id held_by;
40     list<std::thread::id> wanted_by;
41     mutex m;
42     map<std::thread::id, std::condition_variable> c;
43     lock_protocol::xid_t xid;
44     void wait();
45     void signal();
46     void signal(std::thread::id who);
47 };
48
49 typedef map<lock_protocol::lockid_t, lock_state> lock_map;
50
51 class lock_client_cache_rsm;
52
53 // Clients that caches locks.  The server can revoke locks using
54 // lock_revoke_server.
55 class lock_client_cache_rsm : public lock_client {
56     private:
57         std::thread releaser_thread;
58         rsm_client *rsmc;
59         class lock_release_user *lu;
60         int rlock_port;
61         string hostname;
62         string id;
63         mutex xid_mutex;
64         lock_protocol::xid_t xid;
65         fifo<lock_protocol::lockid_t> release_fifo;
66         mutex lock_table_lock;
67         lock_map lock_table;
68         lock_state &get_lock_state(lock_protocol::lockid_t lid);
69     public:
70         static int last_port;
71         lock_client_cache_rsm(string xdst, class lock_release_user *l = 0);
72         virtual ~lock_client_cache_rsm() {};
73         lock_protocol::status acquire(lock_protocol::lockid_t);
74         virtual lock_protocol::status release(lock_protocol::lockid_t);
75         void releaser();
76         rlock_protocol::status revoke_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
77         rlock_protocol::status retry_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
78 };
79
80
81 #endif