[NEWS] Delegate threads

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

[NEWS] Delegate threads

Post 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
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: [NEWS] Delegate threads

Post 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.
TKorho
Posts: 18
Joined: Mon Sep 13, 2021 6:46 pm
Has thanked: 2 times
Been thanked: 1 time

Re: [NEWS] Delegate threads

Post by TKorho »

Post Reply