-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POSIX Simulator: Handle pthread
s not created by FreeRTOS differently
#1223
POSIX Simulator: Handle pthread
s not created by FreeRTOS differently
#1223
Conversation
pthread_sigmask
on pthread
s not created by FreeRTOSpthread
s not created by FreeRTOS differently
6f630dc
to
aa8fcad
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1223 +/- ##
==========================================
- Coverage 91.64% 91.61% -0.03%
==========================================
Files 6 6
Lines 3254 3257 +3
Branches 903 901 -2
==========================================
+ Hits 2982 2984 +2
Misses 132 132
- Partials 140 141 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
pthread
s not created by FreeRTOS differentlypthread
s not created by FreeRTOS differently
The aim of the POSIX port is to aid development of FreeRTOS applications. Why would you want to create native pthreads in a FreeRTOS application? |
A simulator program may need other threads to simulate peripheral hardware. For example I need another thread to run my virtual display window that simulates my hardware display. My main FreeRTOS application does not need to know about my display simulator. On some platforms (I'm experimenting with iOS) the system frameworks create several threads at startup so there are already other threads existing when user code is run. Running my FreeRTOS code on iOS is useful to me as it allows me to easily prototype peripherals like touch screens and HDMI output without bringing up the target hardware (which we haven't received yet). The current FreeRTOS implementation eventually causes all other threads to lock up. |
We’re also using separate (non-FreeRTOS) pthreads to simulate various interrupt sources, and this change aligns well with a pull request we plan to submit. In our case, the interrupt pthreads call different FreeRTOS ISR APIs to inject data into the FreeRTOS application. For this to work, we’ve added an additional port mutex layer to prevent ISR APIs from being executed while FreeRTOS is in a critical section. It would be great to see this patch merged in some form so that we can rebase our planned pull request onto it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great change, and its clear that other users find it valuable as well. Thank you! I just had some minor comments regarding early returns - mainly just to make static analyzers happy since MISRA C will flag it.
Quality Gate passedIssues Measures |
Hmm a check is failing but it doesn't seem related to your change... I will look into it. |
For now, we will merge this as there is no action needed on this PR. Thanks @johnboiles! |
Thanks for getting this in there! |
Signed-off-by: Gaurav Aggarwal <[email protected]>
Are leak checks a port of the CI? After switching to
Edit: Seems to only happen in one of our test suites so I think it's something on our end. |
Thank you for sharing! Which tool are you using for finding these leaks? |
I think the problem is a pre-existing issue that is now showing up in your tools because of the added I've noticed this issue when trying to start and stop FreeRTOS multiple times within the same process (e.g. for running unit tests) -- some threads from the first execution stick around and cause problems with subsequent FreeRTOS runs. My workaround has been to execute each test that needs to start/stop FreeRTOS in a new process. |
We try to build as much as possible for host so we can enable ASAN for this very purpose. In this case is was the most redest herring I've ever seen.
I think the pure FreeRTOS implementation is safe in this case as I was unable to reproduce it without our C++ wrappers. Also after reading more about the thread local key/value mechanisms I get the impression that the way it's implemented in this PR is the correct way to do it. Edit: Try your test now that this PR is merged. We also saw sticky threads prior to that fix. |
Ok! I see the threads getting cleaned up! Though I also still see deadlocks sometimes. I'm testing with this example:
If I breakpoint at the
When I see deadlocks it's like this:
|
Looks to me like it
|
Also, I don't know if it's related, but I've never been able to start/stop the FreeRTOS POSIX simulator multiple times in the same process. If I wrap the contents of my above |
Ah I'm on macOS so I don't get the benefits of #1233 :(
|
Avoid calling
pthread_sigmask
onpthread
s not created by FreeRTOS. Also avoids waiting forever onvPortEndScheduler
if that's called from a non-FreeRTOS thread.Description
This PR modifies the behavior of the FreeRTOS POSIX simulator. The tick handler (via
sigaction
) might happen on any thread in the current process. This can cause hangs with non-FreeRTOS threads (because they can get hung whenprvSuspendSelf
is called on them.Test Steps
Run this program against the
main
branch, notice that it hangs on shutdown.Now run it again with this change and notice that it shuts down cleanly.
Checklist:
I still need to read up on the test suite. Looking for directional feedback first.