Added bindNoThread

bindNoThread in conjunction with OnDataAvailable will provide
users with a choice of thread to process requests on. This is
useful when the user relies on single threaded message loop based
design to avoid locking.
This commit is contained in:
Puneetha Ramachandra 2020-11-30 21:04:53 -08:00 committed by Ben Clayton
parent 1fd23dda91
commit ea6098df7f
2 changed files with 30 additions and 0 deletions

View File

@ -199,6 +199,16 @@ class Session {
const std::shared_ptr<Writer>&) = 0; const std::shared_ptr<Writer>&) = 0;
inline void bind(const std::shared_ptr<ReaderWriter>&); inline void bind(const std::shared_ptr<ReaderWriter>&);
// Alternative to bind() for control over which thread the request is processed.
// If bindNoThread is used, the user must call OnDataAvailable() whenever data is
// ready in the reader pipe. The processing will be done on the calling thread
// and a function to handle the request will be returned in some cases, which can
// be executed on any thread of user choice.
virtual std::function<void()> OnDataAvailable() = 0;
virtual void bindNoThread(const std::shared_ptr<dap::Reader>& r,
const std::shared_ptr<dap::Writer>& w) = 0;
protected: protected:
using RequestSuccessCallback = using RequestSuccessCallback =
std::function<void(const TypeInfo*, const void*)>; std::function<void(const TypeInfo*, const void*)>;

View File

@ -52,6 +52,26 @@ class Impl : public dap::Session {
handlers.put(typeinfo, handler); handlers.put(typeinfo, handler);
} }
void bindNoThread(const std::shared_ptr<dap::Reader>& r,
const std::shared_ptr<dap::Writer>& w) override {
if (isBound.exchange(true)) {
handlers.error("Session is already bound!");
return;
}
reader = dap::ContentReader(r);
writer = dap::ContentWriter(w);
}
std::function<void()> OnDataAvailable() override {
auto request = reader.read();
if (request.size() > 0) {
if (auto payload = processMessage(request)) {
return payload;
}
}
return {};
}
void bind(const std::shared_ptr<dap::Reader>& r, void bind(const std::shared_ptr<dap::Reader>& r,
const std::shared_ptr<dap::Writer>& w) override { const std::shared_ptr<dap::Writer>& w) override {
if (isBound.exchange(true)) { if (isBound.exchange(true)) {