Global destructor clean-ups and python test fixes
[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 #include "maybe.h"
13
14 class lock_release_user {
15     public:
16         virtual void dorelease(lock_protocol::lockid_t) = 0;
17         virtual ~lock_release_user() {}
18 };
19
20 class lock_state {
21 public:
22     enum {
23         none = 0,
24         retrying,
25         free,
26         locked,
27         acquiring,
28         releasing
29     } state = none;
30     std::thread::id held_by;
31     std::list<thread::id> wanted_by;
32     std::mutex m;
33     std::map<thread::id, cond> c;
34     lock_protocol::xid_t xid;
35     void wait(lock & mutex_lock);
36     void signal();
37     void signal(thread::id who);
38 };
39
40 typedef std::map<lock_protocol::lockid_t, lock_state> lock_map;
41
42 // Clients that caches locks.  The server can revoke locks using
43 // lock_revoke_server.
44 class lock_client {
45     private:
46         unique_ptr<rpcc> cl;
47         unique_ptr<rpcs> rlsrpc;
48         thread releaser_thread;
49         unique_ptr<rsm_client> rsmc;
50         lock_release_user *lu;
51         in_port_t rlock_port;
52         string hostname;
53         string id;
54         std::mutex xid_mutex;
55         lock_protocol::xid_t next_xid;
56         fifo<maybe<lock_protocol::lockid_t>> release_fifo;
57         std::mutex lock_table_lock;
58         lock_map lock_table;
59         lock_state & get_lock_state(lock_protocol::lockid_t lid);
60     public:
61         static in_port_t last_port;
62         lock_client(string xdst, lock_release_user *l = 0);
63         ~lock_client();
64         lock_protocol::status acquire(lock_protocol::lockid_t);
65         lock_protocol::status release(lock_protocol::lockid_t);
66         int stat(lock_protocol::lockid_t);
67         void releaser();
68         rlock_protocol::status revoke_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
69         rlock_protocol::status retry_handler(int &, lock_protocol::lockid_t, lock_protocol::xid_t);
70 };
71
72 #endif // C++
73
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77
78 struct _t4_lock_client;
79 typedef struct _t4_lock_client t4_lock_client;
80
81 typedef enum {
82     T4_OK,
83     T4_RETRY,
84     T4_RPCERR,
85     T4_NOENT,
86     T4_IOERR
87 } t4_xxstatus;
88
89 typedef int t4_status;
90
91 typedef const char * t4_lockid_t;
92
93 t4_lock_client *t4_lock_client_new(const char *dst);
94 void t4_lock_client_delete(t4_lock_client *);
95 t4_status t4_lock_client_acquire(t4_lock_client *, t4_lockid_t);
96 t4_status t4_lock_client_release(t4_lock_client *, t4_lockid_t);
97 t4_status t4_lock_client_stat(t4_lock_client *, t4_lockid_t);
98
99 #ifdef __cplusplus
100 }
101 #endif
102
103 #endif