pthreads linux simulator port
Posted: Wed Dec 27, 2023 5:33 am
https://github.com/faisal-shah/ChibiOS. ... or-pthread
Check out demos/various/RT-Posix-Simulator-Pthread
I wrote a pthreads based simulator port. This has 2 benefits:
1. No more limitation on integer only code being safe (as 'user space' threading was used in the existing SIMIA32 port with manual context switching which did not manage the FPU).
2. More portable - can run on any cpu architecture. Previous port was for x86 specifically.
3. Can be extended to be preemptive (using posix signals and alarms).
How it works:
Each ChibiOS thread is a pthread.
All threads are pinned to the same CPU.
Only one thread executing at a time, and all other pthreads are sleeping (waiting on cond var). As a result, ChibiOS scheduler is deciding which thread is running at any given time.
The port_context struct contains synchronization objects (mutex and conditional variable) for each pthread.
The context switch consists of waking up incoming thread by signaling its cond var, and putting the outgoing thread to sleep by waiting on its cond var.
When a thread exits, the context switch function detects that the state of the outgoing thread is CH_STATE_FINAL - and it signals the incoming thread to cancel the outgoing pthread when it wakes up.
I also modified hal_lld.c _sim_check_for_interrupts to sleep between ticks to dramatically reduce the cpu consumption to almost nothing (before it was pinned at 100%).
https://github.com/faisal-shah/ChibiOS. ... or-pthread
Check out demos/various/RT-Posix-Simulator-Pthread
I wrote a pthreads based simulator port. This has 2 benefits:
1. No more limitation on integer only code being safe (as 'user space' threading was used in the existing SIMIA32 port with manual context switching which did not manage the FPU).
2. More portable - can run on any cpu architecture. Previous port was for x86 specifically.
3. Can be extended to be preemptive (using posix signals and alarms).
How it works:
Each ChibiOS thread is a pthread.
All threads are pinned to the same CPU.
Only one thread executing at a time, and all other pthreads are sleeping (waiting on cond var). As a result, ChibiOS scheduler is deciding which thread is running at any given time.
The port_context struct contains synchronization objects (mutex and conditional variable) for each pthread.
The context switch consists of waking up incoming thread by signaling its cond var, and putting the outgoing thread to sleep by waiting on its cond var.
When a thread exits, the context switch function detects that the state of the outgoing thread is CH_STATE_FINAL - and it signals the incoming thread to cancel the outgoing pthread when it wakes up.
I also modified hal_lld.c _sim_check_for_interrupts to sleep between ticks to dramatically reduce the cpu consumption to almost nothing (before it was pinned at 100%).
https://github.com/faisal-shah/ChibiOS. ... or-pthread