Fixed two major bugs in paxos.cc.
[invirt/third/libt4.git] / lock_client.h
1 // lock client interface.
2
3 #ifndef lock_client_h
4 #define lock_client_h
5
6 #ifdef __cplusplus
7
8 #include "types.h"
9 #include "lock_protocol.h"
10 #include "rpc/fifo.h"
11 #include "rsm_client.h"
12
13 class lock_release_user {
14     public:
15         virtual void dorelease(lock_protocol::lockid_t) = 0;
16         virtual ~lock_release_user() {}
17 };
18
19 class lock_state {
20 public:
21     enum {
22         none = 0,
23         retrying,
24         free,
25         locked,
26         acquiring,
27         releasing
28     } state = none;
29     thread::id held_by;
30     list<thread::id> wanted_by;
31     mutex m;
32     map<thread::id, cond> c;
33     lock_protocol::xid_t xid;
34     void wait(lock & mutex_lock);
35     void signal();
36     void signal(thread::id who);
37 };
38
39 typedef map<lock_protocol::lockid_t, lock_state> lock_map;
40
41 // Clients that caches locks.  The server can revoke locks using
42 // lock_revoke_server.
43 class lock_client {
44     private:
45         rpcc *cl;
46         thread releaser_thread;
47         rsm_client *rsmc;
48         lock_release_user *lu;
49         in_port_t rlock_port;
50         string hostname;
51         string id;
52         mutex xid_mutex;
53         lock_protocol::xid_t next_xid;
54         fifo<lock_protocol::lockid_t> release_fifo;
55         mutex lock_table_lock;
56         lock_map lock_table;
57         lock_state &get_lock_state(lock_protocol::lockid_t lid);
58     public:
59         static in_port_t last_port;
60         lock_client(string xdst, lock_release_user *l = 0);
61         ~lock_client() {}
62         lock_protocol::status acquire(lock_protocol::lockid_t);
63         lock_protocol::status release(lock_protocol::lockid_t);
64         int stat(lock_protocol::lockid_t);
65         void releaser();
66         rlock_protocol::status revoke_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
67         rlock_protocol::status retry_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
68 };
69
70 #endif // C++
71
72 extern "C" {
73
74 struct _t4_lock_client;
75 typedef struct _t4_lock_client t4_lock_client;
76
77 typedef enum {
78     T4_OK,
79     T4_RETRY,
80     T4_RPCERR,
81     T4_NOENT,
82     T4_IOERR
83 } t4_xxstatus;
84
85 typedef int t4_status;
86
87 typedef const char * t4_lockid_t;
88
89 t4_lock_client *t4_lock_client_new(const char *dst);
90 void t4_lock_client_delete(t4_lock_client *);
91 t4_status t4_lock_client_acquire(t4_lock_client *, t4_lockid_t);
92 t4_status t4_lock_client_release(t4_lock_client *, t4_lockid_t);
93 t4_status t4_lock_client_stat(t4_lock_client *, t4_lockid_t);
94
95 }
96
97 #endif