-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fdcan use RAII for reference counting. #4272
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
base: main
Are you sure you want to change the base?
Conversation
2f4d9b3
to
6de549f
Compare
… the common sender and receiver types
Nice to see that you are working on this one, too. But to be honest, I dislike that your solution adds more levels of indirection which makes it even harder to read and understand the code, which is already complicated enough IMHO. I have created an alternative approach (for now, only for FDCAN) which only adds two new types: What do you think about it? Edit: Sorry, I forgot to include the link 😄 https://github.com/embedded-rust-iml/embassy/tree/feature/raii-reference-counting-for-stm32-can |
Yeah, I was a bit concerned about the extra code but I was also trying to optimise for memory usage. I think my approach didn't add to memory usage where yours adds extra memory usage to the RxTx types as well as any of the non-buffered senders. I guess it's a compromise either way, that's why I sought to get your thoughts before I made it non-draft, I didn't know how long to wait to see if you were still paying attention :). My opinion isn't too strong either way. Possibly my patch can be improved to have the buffered 'senders' Just use the guarded info reference directly. Instead of also needing their own copy of waiter. This would reduce some of the additional code duplication/complexity. This would further increase consistency between how the buffered classes in 'common' and the main classes in 'not common'. This also means that the trait can be taken away... So then the 'Guard' classes would look a lot more like yours except for that they would old an info& instead of an internal_operation 'function pointer'. The complexity then lies in making sure that also works with bxcan :) |
That seems to be true (for the non-buffered types) but we are talking about one function pointer per guard, i.e. only 4 bytes on STM32, if I am not mistaken. In my opinion, that's negligible and definitely acceptable in order to achieve lower code complexity.
In my first commit/approach, the guards contained an For now, I guess you won't be able to let the buffered types use the guarded info directly, at least without massive code refactorings. But in the longer run, maybe it is even possible to do such a refactoring and reuse the same |
Actually, this seems to not be the case. fdcan vs bxcan is based on a feature flag and the Info struct of both is quite similar. I didn't seem to have any issue in using the info struct directly. Please check out a5eeec2 I guess it's somewhere in between the approaches. I will say, I'm struggled thinking about the best thing to call what was the &Info with RAII counting built in. As you saw, I was using InfoRef in my previous PR. In general, the approach of your original PR a few weeks ago (to reference count not only buffered but the non-buffered CAN) has a bigger impact on bxcan as is... This is because some of the implementations, construct Rx or Tx elements for the purpose of individual write calls. This is not super ideal. |
…cesses to the Info struct Until now, this is only done for one of three guard types
That's what I just haven't checked before. I expected that larger refactorings would be required. So using the Since the guards do contain the If you want to go that way, there are still some things I would change and some open questions:
|
…h their primary purpose. Reference counting is secondary. Remove duplicate info& from Rx+Tx version.
Deref works great! I'm fine with all of your points. Ive pushed another couple of changesets that takes care of them. I went with InfoRef. Seemed like being a Reference to Info is the most importnt part of what it did. I also found a couple of places where I missed removing a couple of the pre-RAII calls. I renamed internal_operation. Actually, I tried to just make it an impl on Info(as a way to remove the duplication that you mentioned), which worked great for FDCAN but not for BXCAN because I didn't do the critical_section->Mutex-of-CriticalSection transition there (yet). My changes are here: main...cschuhen:embassy:feature/raii-reference-counting-for-stm32-can |
Great work! Changing the One last remark: You have used the |
Ok, thanks for that. I just finished now a conversion of BxCan to use the Mutex. This should allow me to finish adjust_reference_counter-as-info-Impl. Thanks for chiming in on the Deref stuff. I'll look at fixing that up also. |
This changeset is equiverlent to f5658d6 that was done for FDCAN. This removes the unsound code that was giving out mut&. to State.
Just makes it nicer too because of better code checking.
6de549f
to
fceeba0
Compare
OK, pushed as above with the move of adjust_reference_counter to impl Info. |
That looks great, thanks for your effort! The last thing I have spotted now is that we should probably rename the |
As mentioned #4223, this is a proposal for using RAII for the reference counting of alive sender/receiver/transceiver instances.
Note, this is a draft PR, and also includes #4271 as a dep.
@DrTobe