[haiku-development] Re: Glass Elevator proposal: templated BControl

  • From: Zenja Solaja <solaja@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Thu, 12 Jul 2018 11:56:34 +1000

On Wed, 11 Jul 2018 at 22:01, Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>
wrote:


For R2, we could likely augment this with a way to register a callback
directly (last time I experimented with this, I needed to use variadic
templates, but I'd think C++17 may have even better ways now). And we
could do away with writing MessageReceived manually completely.

--
Adrien.


Hi Adrien.

Regarding callbacks and C++17 features, may I point you towards
 std::invoke and friends for function forwarding.

I've been working on a Actor model framework, which uses c++17 std::invoke
for function forwarding.  In the actor model, each actor has a message
queue which in my framework is a std::deque of templated member pointers.
ie.

class Actor
{
private:
std::deque<std::function<void ()> > fMessageQueue;

public:
        template <class F, class ... Args>
void AsyncMessage(F &&fn, Args && ... args) noexcept
{
// ... some locking / house keeping
fMessageQueue.emplace_back(std::bind(std::forward<F>(fn),
std::forward<Args>(args)...));
// ... schedule work thread
}
};

And in one of the work thread:

auto msg = std::move(wt->fLastActor->fMessageQueue.front());
wt->fLastActor->fMessageQueue.pop_front();
// locks, work stealing and other stuff
msg();

So in essence I keep a list of std::functions bound to member methods (with
full inheritance transversal) as C++17 callbacks.  The benefits are
variable function arguments, and inheritance transversal.

Message observer pattern can be implemented on top of actors, and
suprisingly the BLooper/BHandler mechanism has the hallmarks of the  actor
pattern (if you squint hard enough).  It misses out on work stealing and
load balancing, but that is a topic for another day.  In Bennoits defense,
back in 1993 not many people knew about Carl Hewitts actor model, and
Stepanov still hadn't unleashed templates to the world.

std::invoke works on GCC 7.3, but I had to do use a hack from
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3727.html to
implement std::invoke in GCC 5.6.  I dont even know if GCC 2.9x will
support any of this, so this is strictly a R2 thing.

Other related posts: