Skip to content

Re-add Shared Pointer Class into platform features #7815

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

Merged
merged 9 commits into from
Aug 23, 2018

Conversation

donatieng
Copy link
Contributor

Description

This PR re-adds a shared pointer class to mbed OS (similar to the std::shared_ptr class introduced in C++11). This class (or a variation of) is used by different projects and used to be part of the "core-util" classes. The only change from this previous revision is the name: SharedPtr (to avoid any clashes with existing projects and be consistent with the SingletonPtr class).

Pull request type

[ ] Fix
[ ] Refactor
[ ] Target update
[X] Functionality change
[ ] Breaking change

@donatieng donatieng requested review from geky and a team August 17, 2018 11:25

// increment new counter
if (counter) {
(*counter)++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use an atomic so we get the same guarantee as the regular shared_ptr ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hadn't realised it wasn't done atomically, will update the implementation :)

*
* Similar to std::shared_ptr in C++11.
*
* Usage: SharedPtr<class> POINTER(new class())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention that it is not a full feature shared_ptr:

  • no weak ptr
  • no make_shared
  • no custom deleter
  • no cast operator
  • no enable_shared_from_this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

*
* When POINTER is passed around by value the copy constructor and
* destructor counts the number of references to the original object.
* If the counter reaches zero, delete is called on the object pointed to.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange text?

Copy link
Contributor

@cmonr cmonr Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like many more 'L's were accidentally capitalized.

@cmonr cmonr requested a review from a team August 17, 2018 16:02
@donatieng
Copy link
Contributor Author

I've cleaned up the class and its doc.

Copy link
Contributor

@geky geky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

I just had a bunch of nits

SharedPtr(T* ptr): _ptr(ptr), _counter(NULL) {
// allocate counter on the heap so it can be shared
if(_ptr != NULL) {
_counter = (uint32_t*) malloc(sizeof(uint32_t));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be new uint32_t so OOM asserts?

if (_ptr != NULL) {
core_util_critical_section_enter();
(*_counter)++;
core_util_critical_section_exit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is core_util_atomic_incr_u32 applicable here?

Not a blocker, adopting atomic increments could be an optimization for later

void decrement_counter() {
if (_counter) {
core_util_critical_section_enter();
if (*_counter == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny tiny tiny micro-optimization, so feel free to ignore. But comparing with zero is slightly better in terms of code size. Once you hit zero there's also no race conditions possible.

@donatieng
Copy link
Contributor Author

Thanks @geky - should be a bit more optimised now :)

Copy link
Contributor

@geky geky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

Copy link
Contributor

@SenRamakri SenRamakri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donatieng - Good work. Should any tests be added for SharedPtr class?

@cmonr cmonr removed the needs: work label Aug 17, 2018
@0xc0170
Copy link
Contributor

0xc0170 commented Aug 20, 2018

Should any tests be added for SharedPtr class?

Definitely. And documentation (I'll add docs team to review).

@0xc0170 0xc0170 requested a review from AnotherButler August 20, 2018 09:46
Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having Shared pointer +1

Can you check astyle travis job and fix the styling?

@0xc0170 0xc0170 dismissed their stale review August 20, 2018 12:04

Astyle updated, waiting for tests

@AnotherButler
Copy link
Contributor

AnotherButler commented Aug 20, 2018

Is there any reason this API shouldn't have a user API page added to the handbook?
Update: Edited for clarity

Copy edit for consistent capitalization and minor grammar nits.
@donatieng
Copy link
Contributor Author

@AnotherButler created this PR for the handbook: ARMmbed/mbed-os-5-docs#671

{
if (_ptr != NULL) {
core_util_critical_section_enter();
return *_counter;
Copy link
Contributor

@0xc0170 0xc0170 Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you return prior exiting from critical section (is there an intention?)

@donatieng
Copy link
Contributor Author

donatieng commented Aug 21, 2018

Urgh! Thanks for spotting this @0xc0170!

@donatieng
Copy link
Contributor Author

LittleFS test failure seems quite unrelated to this PR's content

@cmonr
Copy link
Contributor

cmonr commented Aug 21, 2018

@donatieng Correct. We found something external that appears to have affected Mbed OS CI. Breaking change has been reverted, and Travis CI jobs are being restarted.

@donatieng
Copy link
Contributor Author

Thanks @cmonr - unless anyone has any objections, I think this one is good for morphing :)

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 22, 2018

/morph build

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 22, 2018

@studavekar @cmonr IllegalStateException again the last build run

/morph build

@mbed-ci
Copy link

mbed-ci commented Aug 22, 2018

Build : SUCCESS

Build number : 2862
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7815/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Aug 22, 2018

Build : SUCCESS

Build number : 2863
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7815/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Aug 22, 2018

@mbed-ci
Copy link

mbed-ci commented Aug 22, 2018

Exporter Build : FAILURE

Build number : 2493
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/exporter/7815/

@mbed-ci
Copy link

mbed-ci commented Aug 23, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 23, 2018

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Aug 23, 2018

@mbed-ci
Copy link

mbed-ci commented Aug 23, 2018

Exporter Build : SUCCESS

Build number : 2501
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/exporter/7815/

@cmonr cmonr merged commit deb905d into ARMmbed:master Aug 23, 2018
@0xc0170 0xc0170 removed the needs: CI label Aug 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants