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