X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/26ade07ab0e62b98b452fbbd18edba0450035e35..02967a43024ce81912cd1ec96a800397457f8066:/rpc/rpc.h diff --git a/rpc/rpc.h b/rpc/rpc.h index b44c057..9464e57 100644 --- a/rpc/rpc.h +++ b/rpc/rpc.h @@ -2,32 +2,39 @@ #define rpc_h #include "types.h" -#include -#include #include "rpc_protocol.h" -#include "thr_pool.h" +#include "thread_pool.h" #include "marshall.h" #include "marshall_wrap.h" #include "connection.h" +#include "threaded_log.h" + +using std::chrono::milliseconds; namespace rpc { static constexpr milliseconds to_max{12000}; static constexpr milliseconds to_min{100}; } -template struct is_valid_call : false_type {}; +template +struct is_valid_call : false_type {}; template struct is_valid_call : true_type {}; -template struct is_valid_registration : false_type {}; +template +struct is_valid_registration : false_type {}; template -struct is_valid_registration::type...), S(R &, Args...)> : true_type {}; +struct is_valid_registration< + S(R &, typename std::decay::type...), + S(R &, Args...)> : true_type {}; template -struct is_valid_registration : is_valid_registration {}; +struct is_valid_registration< + P, + S(C::*)(R &, Args...)> : is_valid_registration {}; // rpc client endpoint. // manages a xid space per destination socket @@ -35,8 +42,6 @@ struct is_valid_registration : is_valid_registration

- using proc_t = rpc_protocol::proc_t; using nonce_t = rpc_protocol::nonce_t; using xid_t = rpc_protocol::xid_t; @@ -48,7 +53,7 @@ class rpcc : private connection_delegate { string *rep; int intret; bool done = false; - mutex m; + std::mutex m; cond c; }; @@ -65,17 +70,18 @@ class rpcc : private connection_delegate { shared_ptr chan_; - mutex m_; // protect insert/delete to calls[] - mutex chan_m_; + std::mutex m_; // protect insert/delete to calls[] + std::mutex chan_m_; + std::mutex bind_m_; // protect bind operations bool destroy_wait_ = false; cond destroy_wait_c_; - map calls_; + std::map calls_; // xid starts with 1 and latest received reply starts with 0 xid_t xid_ = 1; - list xid_rep_window_ = {0}; + std::list xid_rep_window_ = {0}; struct request { void clear() { buf.clear(); xid = -1; } @@ -86,22 +92,7 @@ class rpcc : private connection_delegate { request dup_req_; int xid_rep_done_ = -1; - int call1(proc_id_t proc, milliseconds to, string & rep, marshall & req); - - template - inline int call_m(proc_id_t proc, milliseconds to, R & r, marshall && req) { - string rep; - int intret = call1(proc, to, rep, req); - if (intret < 0) return intret; - unmarshall u(rep, true, r); - if (u.okdone() != true) { - LOG << "rpcc::call_m: failed to unmarshall the reply. You are probably " - << "calling RPC 0x" << std::hex << proc << " with the wrong return type."; - VERIFY(0); - return rpc_protocol::unmarshall_reply_failure; - } - return intret; - } + int call_marshalled(const rpc_protocol::proc_t & proc, milliseconds to, string & rep, const marshall & req); bool got_pdu(const shared_ptr & c, const string & b); @@ -110,23 +101,34 @@ class rpcc : private connection_delegate { rpcc(const string & d); ~rpcc(); - nonce_t id() { return clt_nonce_; } - int bind(milliseconds to = rpc::to_max); + // Manages a cache of RPC connections. Usage: + // if (auto cl = rpcc::bind_cached(dst)) + // ret = cl->call(...); + // where the string dst has the form "host:port". Because bind_cached() + // may block, callers should probably not hold mutexes. + static shared_ptr bind_cached(const string & destination); + static void unbind_cached(const string & destination); + void set_reachable(bool r) { reachable_ = r; } - void cancel(); + void cancel(lock & m_lock); template - inline int call(proc_t

proc, R & r, const Args & ... args) { + inline int call(const rpc_protocol::proc_checked_t

& proc, R & r, const Args & ... args) { return call_timeout(proc, rpc::to_max, r, args...); } template - inline int call_timeout(proc_t

proc, milliseconds to, R & r, const Args & ... args) { + inline int call_timeout(const rpc_protocol::proc_checked_t

& proc, milliseconds to, R & r, const Args & ... args) { static_assert(is_valid_call::value, "RPC called with incorrect argument types"); - return call_m(proc.id, to, r, forward(marshall(args...))); + string rep; + int intret = call_marshalled(proc, to, rep, marshall(args...)); + if (intret >= 0) { + VERIFY(unmarshall(rep, true, r).okdone()); // guaranteed by static type checking + } + return intret; } }; @@ -134,8 +136,6 @@ class rpcc : private connection_delegate { class rpcs : private connection_delegate { private: using proc_id_t = rpc_protocol::proc_id_t; - template - using proc_t = rpc_protocol::proc_t; using nonce_t = rpc_protocol::nonce_t; using xid_t = rpc_protocol::xid_t; @@ -164,7 +164,7 @@ class rpcs : private connection_delegate { // provide at most once semantics by maintaining a window of replies // per client that that client hasn't acknowledged receiving yet. // indexed by client nonce. - map> reply_window_; + std::map> reply_window_; void add_reply(nonce_t clt_nonce, xid_t xid, const string & b); @@ -172,20 +172,20 @@ class rpcs : private connection_delegate { xid_t rep_xid, string & b); // latest connection to the client - map> conns_; + std::map> conns_; bool reachable_ = true; // map proc # to function - map procs_; + std::map procs_; - mutex procs_m_; // protect insert/delete to procs[] - mutex reply_window_m_; // protect reply window et al - mutex conns_m_; // protect conns_ + std::mutex procs_m_; // protect insert/delete to procs[] + std::mutex reply_window_m_; // protect reply window et al + std::mutex conns_m_; // protect conns_ void dispatch(shared_ptr c, const string & buf); - unique_ptr dispatchpool_{new thread_pool(6, false)}; + unique_ptr dispatchpool_{new thread_pool(6)}; unique_ptr listener_; // RPC handler for clients binding @@ -200,7 +200,8 @@ class rpcs : private connection_delegate { void set_reachable(bool r) { reachable_ = r; } - template inline void reg(proc_t

proc, F f, C *c=nullptr) { + template + inline void reg(const rpc_protocol::proc_checked_t

& proc, F f, C *c=nullptr) { static_assert(is_valid_registration::value, "RPC handler registered with incorrect argument types"); struct ReturnOnFailure { static inline int unmarshall_args_failure() {