Page 1 of 1

[NEWS] Delegate threads

Posted: Sun Nov 17, 2019 11:33 am
by Giovanni
Hi,

I just committed a prototype of this new mechanism "Delegate Threads" in the OSLIB, in brief it is built on top of messages and allows to invoke C functions in the context of another thread.

Imagine encapsulating a whole non-thread-safe subsystem inside a thread, for example a graphic library or a file system. Other threads will be able to call functions of the subsystem from remote without having to implement an ad-hoc messages protocol or implementing mutual exclusion, calls are implicitly synchronized.

The delegate thread has to implement something like this:

Code: Select all

/* Messages dispatcher loop.*/
while (true) {
  chDelegateDispatchTimeout(TIME_INFINITE) {
}


Other threads will be able to call functions inside the delegate using:

Code: Select all

ret = chDelegateCallDirect2(tp, remote_func, p1, p2);


It is also possible to hide details by creating convenient proxy functions like this:

Code: Select all

static inline int my_remote_func(int p1, int p2) {

  return (int)chDelegateCallDirect2(tp, remote_func, p1, p2):
}


The general idea is to create wrappers for common external subsystems using an unified remote call layer. I am thinking to FatFS and LittlevGL. The mechanism has an overhead of two thread-to-thread context switch operations, which is very efficient in ChibiOS.

Giovanni

Re: [NEWS] Delegate threads

Posted: Mon Nov 18, 2019 9:52 am
by RoccoMarco
It sounds a powerful mechanism to deal with complex systems. With this, it is possible to easily create centralized handlers and there are plenty of use cases for this scenario.

Re: [NEWS] Delegate threads

Posted: Thu Feb 10, 2022 12:05 pm
by TKorho