-
Notifications
You must be signed in to change notification settings - Fork 3k
Add init reference count to all block devices #7648
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
Conversation
0821855
to
71fd582
Compare
if (_init_ref_count++) { | ||
return BD_ERROR_OK; | ||
} | ||
|
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.
@davidsaada, I think I'm missing something. Is this needed if we can expect the underlying block device to also be reference counted?
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.
Yes it is. If the block device has a resource (like BufferedBlockDevice
) it would make sense to protect it against the scenario described in the issue.
Now, while it is not needed in devices like ObservingBlockDevice
, ones not having any allocated resources, it is harmless, and it will become effective if one decides to add such a resource in the future. This is why I think it's a good practice to have this in all block devices, also for reference sake for future ones (which may need it).
@@ -36,6 +36,10 @@ BufferedBlockDevice::~BufferedBlockDevice() | |||
|
|||
int BufferedBlockDevice::init() | |||
{ | |||
if (_init_ref_count++) { |
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.
ref_count operations do not need to be atomic?
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.
Absolutely right. Fixed now.
044a52b
to
75afa44
Compare
That's fair, but would a comment convey the same info without the extra code cost? |
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.
Sorry I didn't see the above issue earlier, though I believe it's an easy fix.
features/filesystem/bd/BlockDevice.h
Outdated
@@ -217,6 +218,10 @@ class BlockDevice | |||
size % get_erase_size() == 0 && | |||
addr + size <= this->size()); | |||
} | |||
|
|||
protected: | |||
uint32_t _init_ref_count; |
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.
Sorry I didn't see this earlier. We should not have state in interfaces classes.
This adds a RAM cost to every block device class. Even ones with their own form of synchronization (ie SD, SPIF, others outside of tree).
At worst this can discourage users from applying the interface to their own code. Interfaces work best when a class satisfies as many interfaces as possible, allowing code reuse for functions that use that interface. If there's extra state in an interface, users may not adopt the interface because of the code cost. Even if it makes sense otherwise.
This is what happened with the FileLike/FileSystemLike classes. State was introduced for constructing the file tree, which on paper was a good idea, but now the interface is avoided and the poorer-named-but-stateless FileHandle class is used instead. (We still have to put up with this because of backwards compatibility, but it was a lesson learned.)
I believe this can be moved into each of the implementing classes without any cost?
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.
Will move. Certainly now that not all classes implement 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.
Thanks. The FileLike/FileSystemLike classes have been frustrating, and I didn't want to see us repeat it.
Fair enough. Will replace it with a comment in the resource-less classes (in the the spirit of the current efforts to reduce code size). Would suggest these include Observing, Profiling, ReadOnly & slicing. |
75afa44
to
235f175
Compare
Pushed fixes conforming to comments given by @geky. |
@geky Mind re-reviewing? |
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.
Looks great to me 👍
Thanks for taking care of my complaints
/morph build |
@ARMmbed/mbed-os-maintainers Targeting this for a patch even though a parameter has been added to block device constructors. Guidance: https://github.com/ARMmbed/mbed_os_release_notes/pull/7/files#diff-895497eee5018cac7ab732faa7d51128R33 |
Build : SUCCESSBuild number : 2713 Triggering tests/morph test |
Exporter Build : FAILUREBuild number : 2342 |
just a private member that gets initial value (can't be set via public API anyhow). This breaks ABI (fine for any release - recompilation needed) |
/morph export-build |
Exporter Build : SUCCESSBuild number : 2346 |
Test : SUCCESSBuild number : 2444 |
Add init reference count to all block devices
Description
This PR fixes the problem of multiple calls to BD init/deinit APIs, by simply adding an init ref count. This is required due to the fact that utility block devices, which include underlying block devices, call the underlying init/deinit APIs automatically.
This PR partially resolves #7455 (for all BDs included in mbed-os). External BDs should be handled in a different PR.
Pull request type