Skip to content

Commit b6e1a48

Browse files
Improve documentation of ScheduleLambda (#33967)
* Improve documentation of ScheduleLambda Problem: - SystemLayer::ScheduleLambda is critical to allow correct context updates to data, but it was claimed it had to be executed in Matter context already, which is the opposite of the point of the method. Fixes #26538 This PR: - Improves the documentation of several methods in SystemLayer.h - Makes ScheduleLambdaBridge private (not called elsewhere) - Adds a static assert to avoid arguments on the lambda Testing done: - All unit tests still pass * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 1cbb109 commit b6e1a48

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/system/SystemLayer.h

+19-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#pragma once
2727

28+
#include <type_traits>
29+
#include <utility>
30+
2831
// Include configuration headers
2932
#include <system/SystemConfig.h>
3033

@@ -47,8 +50,6 @@
4750
#include <ev.h>
4851
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH/LIBEV
4952

50-
#include <utility>
51-
5253
namespace chip {
5354
namespace System {
5455

@@ -181,9 +182,11 @@ class DLL_EXPORT Layer
181182

182183
/**
183184
* @brief
184-
* Schedules a function with a signature identical to `OnCompleteFunct` to be run as soon as possible in the Matter context.
185-
* This must only be called when already in the Matter context (from the Matter event loop, or while holding the Matter
186-
* stack lock).
185+
* Schedules a `TimerCompleteCallback` to be run as soon as possible in the Matter context.
186+
*
187+
* WARNING: This must only be called when already in the Matter context (from the Matter event loop, or
188+
* while holding the Matter stack lock). The `PlatformMgr::ScheduleWork()` equivalent method
189+
* is safe to call outside Matter context.
187190
*
188191
* @param[in] aComplete A pointer to a callback function to be called when this timer fires.
189192
* @param[in] aAppState A pointer to an application state object to be passed to the callback function as argument.
@@ -196,31 +199,29 @@ class DLL_EXPORT Layer
196199

197200
/**
198201
* @brief
199-
* Schedules a lambda even to be run as soon as possible in the CHIP context. This function is not thread-safe,
200-
* it must be called with in the CHIP context
202+
* Schedules a lambda object to be run as soon as possible in the Matter context.
201203
*
202-
* @param[in] event A object encapsulate the context of a lambda
204+
* This is safe to call from any context and will guarantee execution in Matter context.
205+
* Note that the Lambda's capture have to fit within `CHIP_CONFIG_LAMBDA_EVENT_SIZE` bytes.
203206
*
204-
* @retval CHIP_NO_ERROR On success.
205-
* @retval other Platform-specific errors generated indicating the reason for failure.
206-
*/
207-
CHIP_ERROR ScheduleLambdaBridge(LambdaBridge && event);
208-
209-
/**
210-
* @brief
211-
* Schedules a lambda object to be run as soon as possible in the CHIP context. This function is not thread-safe,
212-
* it must be called with in the CHIP context
207+
* @param[in] lambda The Lambda to execute in Matter context.
208+
*
209+
* @retval CHIP_NO_ERROR On success.
210+
* @retval other Platform-specific errors generated indicating the reason for failure.
213211
*/
214212
template <typename Lambda>
215213
CHIP_ERROR ScheduleLambda(const Lambda & lambda)
216214
{
215+
static_assert(std::is_invocable_v<Lambda>, "lambda argument must be an invocable with no arguments");
217216
LambdaBridge bridge;
218217
bridge.Initialize(lambda);
219218
return ScheduleLambdaBridge(std::move(bridge));
220219
}
221220

222221
private:
223-
// Copy and assignment NOT DEFINED
222+
CHIP_ERROR ScheduleLambdaBridge(LambdaBridge && bridge);
223+
224+
// Not copyable
224225
Layer(const Layer &) = delete;
225226
Layer & operator=(const Layer &) = delete;
226227
};

0 commit comments

Comments
 (0)