Skip to content

Add ThisThread namespace and deprecate static Thread methods #7872

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 3 commits into from
Sep 2, 2018

Conversation

kjbracey
Copy link
Contributor

@kjbracey kjbracey commented Aug 23, 2018

Description

The static methods of Thread have repeatedly caused confusion - while it's very clear in C that osThreadGetId() must be returning the current thread ID, as it takes no parameters, it's not at all obvious that thread->gettid() is static and will return the current thread's ID, not thread's

Remove the confusion by

  • deprecating the existing static methods,
  • adding updated replacements in namespace ThisThread and namespace Kernel,
  • adding a new non-static Thread::get_id().

The distinction between ThisThread::get_id() and thread->get_id() is clear, and they cannot be confused.

Syntax and naming follows C++11, which has namespace std::this_thread and corresponding methods.

Most visible change to typical code is that Thread::wait() (which people often use because of problems with mbed::wait()) is deprecated in favour of ThisThread::sleep_for().

Effectively fixes #1831

Pull request type

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

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

Looks very good. I'm a bit worried about naming conventions for namespace: on one hand we got mbed, event and rtos and on the other we have Kernel and ThisThread.


/** Returns the Thread Flags currently set for the currently running thread.
@return current thread flags or 0 if not in a valid thread.
@note You may call this function from ISR context.
Copy link
Member

Choose a reason for hiding this comment

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

That's a bit strange to call this function from ISR; even if it is legal.
More generally this pack of function are not meant to be called from ISR context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually it isn't legal, according to docs.

The RTX implementation of osThreadFlagsGet does return 0 without error if in ISR context, but the CMSIS-RTOS 2.1.2 documentation says it can't be called from interrupt context. Will modify.

*/
namespace ThisThread {
/** Clears the specified Thread Flags of the currently running thread.
@param flags specifies the thread flags of the thread that should be cleared.
Copy link
Member

Choose a reason for hiding this comment

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

of the thread may be redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made it "flags of the thread", matching CMSIS-RTOS 2 doc wording. Still feels a bit wonky for something acting only on current thread, but it matches the wording of Thread::flags_set / osThreadFlagsSet, either intentionally or by copy-and-paste.

#include "mbed_assert.h"

namespace rtos {

Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to enclose function implementations into a namespace. With the prefix it looks like a member function implementation.

Copy link
Contributor Author

@kjbracey kjbracey Aug 27, 2018

Choose a reason for hiding this comment

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

That was intentional because that's how I was mainly expecting them to be used - as if they were static member functions. So people are likely to be searching for ThisThread::sleep_for, having seen it in code.

Does that make sense?

@cmonr cmonr requested a review from 0xc0170 August 24, 2018 04:58
@0xc0170 0xc0170 requested a review from a team August 24, 2018 11:06
Copy link

@deepikabhavnani deepikabhavnani 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 👍

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 27, 2018

Please review the astyle job (fix them).

@kjbracey
Copy link
Contributor Author

Looks very good. I'm a bit worried about naming conventions for namespace: on one hand we got mbed, event and rtos and on the other we have Kernel and ThisThread.

I think there's a reasonable distinction to be made. mbed, rtos and event are namespaces in the traditional sense, like std or boost, covering a complete system, although event is a tad small, and possibly should just have been in mbed.

Kernel and ThisThread are pseudo-static-classes (within a traditional namespace) covering a specific small API. As such, they're named to match the classes they slot in alongside.

C++11 has class std::thread and namespace std::this_thread, which are analogues sharing same-named functions or members - I've directly mirrored that with class rtos::Thread and namespace rtos::ThisThread.

I would say for these pseudo-static-classes the namespace is very much to be read as part of the API name, and they are normally expected to be called explicitly as Kernel::get_ms_count or ThisThread::get_id(), not via a using-declaration.

@kjbracey kjbracey changed the title Add ThisThread class and deprecate static Thread methods Add ThisThread namespace and deprecate static Thread methods Aug 27, 2018
@kjbracey kjbracey force-pushed the thisthread branch 3 times, most recently from 3d5baa7 to 37433b0 Compare August 27, 2018 10:22
Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

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

Thanks Kevin, it all makes sense. I've opened an issue in the docs. It would be nice to document in our guidelines what the pseudo-static-classes pattern is and what conventions it uses.

@cmonr
Copy link
Contributor

cmonr commented Aug 27, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Aug 27, 2018

Build : SUCCESS

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

Triggering tests

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

@mbed-ci
Copy link

mbed-ci commented Aug 27, 2018

@mbed-ci
Copy link

mbed-ci commented Aug 28, 2018

@kjbracey
Copy link
Contributor Author

Broken testcase corrected.

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 29, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Aug 29, 2018

Build : SUCCESS

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

Triggering tests

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

@mbed-ci
Copy link

mbed-ci commented Aug 29, 2018

@mbed-ci
Copy link

mbed-ci commented Aug 29, 2018

@kjbracey
Copy link
Contributor Author

Seems to be a spurious failure unconnected to PR - passed in previous run.

@cmonr
Copy link
Contributor

cmonr commented Aug 31, 2018

Correct. We're currently running Testing CI on other PRs. Will restart this PR when able to.

@cmonr
Copy link
Contributor

cmonr commented Sep 2, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Sep 2, 2018

@cmonr cmonr merged commit 48232be into ARMmbed:master Sep 2, 2018
@mattbrown015
Copy link
Contributor

(I believe) This changed has created some warnings.

Compile [ 60.4%]: mbed_wait_api_rtos.cpp
[Warning] mbed_wait_api_rtos.cpp@45,17: 'static osStatus rtos::Thread::wait(uint32_t)' is deprecated: Static methods only affecting current thread cause confusion. Replaced by ThisThread::sleep_for. [since mbed-os-5.10] [-Wdeprecated-declarations]
[Warning] mbed_wait_api_rtos.cpp@45,41: 'static osStatus rtos::Thread::wait(uint32_t)' is deprecated: Static methods only affecting current thread cause confusion. Replaced by ThisThread::sleep_for. [since mbed-os-5.10] [-Wdeprecated-declarations]

Is someone on the case?

It has also created warnings in the LoRa radio drivers but I guess no one here is interested in those! I'll open an issue there.

@kjbracey
Copy link
Contributor Author

kjbracey commented Sep 4, 2018

Thanks for the poke. Created PR #7980 to clean up warnings.

@kjbracey kjbracey deleted the thisthread branch September 4, 2018 12:12
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.

Thread::gettid() does not return the ID of the object it is invoked on
8 participants