Skip to content
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

System timer API does not document what thread the callback is called on #7497

Closed
bzbarsky-apple opened this issue Jun 9, 2021 · 3 comments · Fixed by #15047
Closed

System timer API does not document what thread the callback is called on #7497

bzbarsky-apple opened this issue Jun 9, 2021 · 3 comments · Fixed by #15047
Assignees
Labels
documentation Improvements or additions to documentation V1.0

Comments

@bzbarsky-apple
Copy link
Contributor

Problem

The system timer API does not define what thread the timer callback will happen on. I think it's always the "message thread", though I don't know whether we have that clearly defined as a concept. But, importantly, the callback is NOT necessarily going to happen on the thread that started the timer, as far as I can tell. This is pretty non-obvious.

Proposed Solution

Document exactly how this API works, so consumers don't get surprised.

@tcarmelveilleux @andy31415 @msandstedt

@msandstedt
Copy link
Contributor

Problem

The system timer API does not define what thread the timer callback will happen on. I think it's always the "message thread", though I don't know whether we have that clearly defined as a concept. But, importantly, the callback is NOT necessarily going to happen on the thread that started the timer, as far as I can tell. This is pretty non-obvious.

Proposed Solution

Document exactly how this API works, so consumers don't get surprised.

@tcarmelveilleux @andy31415 @msandstedt

My assumption was that this was how it would work, simply because that's the most straight forward implementation. And in the absence of a truly platform-agnostic mechanism to dispatch work onto other threads (e.g. something that does what delegates do in .NET, what signals and slots do in Qt, etc., but with a generic and consistent interface for all of our platforms), that seems pretty tough to do. I certainly don't know how to do it in a platform-agnostic way.

I think that's OK though. It just means you need to understand this and dispatch to your particular thread context from the callback. So two steps to get code executing in your desired thread context instead of one.

One observation. This interface:

Error StartTimer(uint32_t aMilliseconds, TimerCompleteFunct aComplete, void * aAppState);

I have personally found it more natural to have a delegate class type defined and pass this instead of function and data pointers. It doesn't really matter. You can accomplish the same with both. But that's become a pattern elsewhere in the stack. It just feels easier (to me) to be able to reference this on yourself to get whatever context you need when called back. Not a big deal though.

@bzbarsky-apple
Copy link
Contributor Author

Right, to be clear I am not asking to change what the API does, just to have clear documentation about the contract.

@mrjerryjohns
Copy link
Contributor

From my coarse analysis, it looks like it runs on the CHIP thread.

Tracing through Timer::Start in SystemTimer.cpp, I found:

On POSIX:

  • Calls WakeSelect() in Line 173, which just wakes up select on the CHIP thread and makes it re-assess the timer pool and dispatch any expired timer callbacks.

On FreeRTOS:

  • Eventually calls into GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_StartChipTimer, which posts a dummy event to the CHIP event queue which causes it to unblock, and handle timers in GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop.

On Zephr:

  • I believe it uses sockets, and makes the same WakeSelect() call, which should go through the POSIX flow above.

@woody-apple woody-apple added V1.0 documentation Improvements or additions to documentation labels Jan 25, 2022
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Feb 10, 2022
#### Problem

Fixes  project-chip#7497 System timer API does not document what thread the callback is called on

#### Change overview

Added comment:

> The SDK is not generally thread safe. System::Layer methods should
> only be called from a single context, or otherwise externally
> synchronized. For platforms that use a CHIP event loop thread, timer
> callbacks are invoked on that thread; for platforms that use a CHIP
> lock, the lock is held.

#### Testing

Changes comment only.
kpschoedel added a commit to kpschoedel/connectedhomeip that referenced this issue Feb 11, 2022
#### Problem

Fixes  project-chip#7497 System timer API does not document what thread the callback is called on

#### Change overview

Added comment:

> The SDK is not generally thread safe. System::Layer methods should
> only be called from a single context, or otherwise externally
> synchronized. For platforms that use a CHIP event loop thread, timer
> callbacks are invoked on that thread; for platforms that use a CHIP
> lock, the lock is held.

#### Testing

Changes comment only.
andy31415 pushed a commit that referenced this issue Feb 11, 2022
#### Problem

Fixes  #7497 System timer API does not document what thread the callback is called on

#### Change overview

Added comment:

> The SDK is not generally thread safe. System::Layer methods should
> only be called from a single context, or otherwise externally
> synchronized. For platforms that use a CHIP event loop thread, timer
> callbacks are invoked on that thread; for platforms that use a CHIP
> lock, the lock is held.

#### Testing

Changes comment only.
jamesluo11 pushed a commit to jamesluo11/connectedhomeip that referenced this issue Apr 26, 2022
#### Problem

Fixes  project-chip#7497 System timer API does not document what thread the callback is called on

#### Change overview

Added comment:

> The SDK is not generally thread safe. System::Layer methods should
> only be called from a single context, or otherwise externally
> synchronized. For platforms that use a CHIP event loop thread, timer
> callbacks are invoked on that thread; for platforms that use a CHIP
> lock, the lock is held.

#### Testing

Changes comment only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation V1.0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants