More clean-ups
[invirt/third/libt4.git] / lock_client.h
1 // lock client interface.
2
3 #ifndef lock_client_h
4
5 #define lock_client_h
6
7 #ifdef __cplusplus
8
9 #include <string>
10 #include "lock_protocol.h"
11 #include "rpc/rpc.h"
12 #include "lang/verify.h"
13 #include "rpc/fifo.h"
14 #include "rsm_client.h"
15
16 class lock_release_user {
17     public:
18         virtual void dorelease(lock_protocol::lockid_t) = 0;
19         virtual ~lock_release_user() {};
20 };
21
22 using std::string;
23 using std::thread;
24 using std::list;
25 using std::map;
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     std::thread::id held_by;
41     list<std::thread::id> wanted_by;
42     mutex m;
43     map<std::thread::id, std::condition_variable> c;
44     lock_protocol::xid_t xid;
45     void wait();
46     void signal();
47     void signal(std::thread::id who);
48 };
49
50 typedef map<lock_protocol::lockid_t, lock_state> lock_map;
51
52 // Clients that caches locks.  The server can revoke locks using
53 // lock_revoke_server.
54 class lock_client {
55     private:
56         rpcc *cl;
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(string xdst, class lock_release_user *l = 0);
72         ~lock_client() {};
73         lock_protocol::status acquire(lock_protocol::lockid_t);
74         lock_protocol::status release(lock_protocol::lockid_t);
75         int stat(lock_protocol::lockid_t);
76         void releaser();
77         rlock_protocol::status revoke_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
78         rlock_protocol::status retry_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
79 };
80
81 #endif // C++
82
83 extern "C" {
84
85 struct _t4_lock_client;
86 typedef struct _t4_lock_client t4_lock_client;
87
88 typedef enum {
89     T4_OK,
90     T4_RETRY,
91     T4_RPCERR,
92     T4_NOENT,
93     T4_IOERR
94 } t4_xxstatus;
95
96 typedef int t4_status;
97
98 typedef const char * t4_lockid_t;
99
100 t4_lock_client *t4_lock_client_new(const char *dst);
101 void t4_lock_client_delete(t4_lock_client *);
102 t4_status t4_lock_client_acquire(t4_lock_client *, t4_lockid_t);
103 t4_status t4_lock_client_release(t4_lock_client *, t4_lockid_t);
104 t4_status t4_lock_client_stat(t4_lock_client *, t4_lockid_t);
105
106 }
107
108 #endif