|
25 | 25 |
|
26 | 26 | #pragma once
|
27 | 27 |
|
| 28 | +#include <type_traits> |
| 29 | + |
28 | 30 | // Include configuration headers
|
29 | 31 | #include <system/SystemConfig.h>
|
30 | 32 |
|
31 | 33 | #include <lib/core/CHIPCallback.h>
|
32 | 34 |
|
33 | 35 | #include <lib/support/CodeUtils.h>
|
34 | 36 | #include <lib/support/DLLUtil.h>
|
| 37 | +#include <lib/support/LambdaBridge.h> |
35 | 38 | #include <lib/support/ObjectLifeCycle.h>
|
36 | 39 | #include <system/SystemClock.h>
|
37 | 40 | #include <system/SystemError.h>
|
|
50 | 53 | namespace chip {
|
51 | 54 | namespace System {
|
52 | 55 |
|
53 |
| -struct LambdaBridge |
54 |
| -{ |
55 |
| - void (*LambdaProxy)(const void * context); |
56 |
| - alignas(CHIP_CONFIG_LAMBDA_EVENT_ALIGN) char LambdaBody[CHIP_CONFIG_LAMBDA_EVENT_SIZE]; |
57 |
| -}; |
58 |
| - |
59 | 56 | class Layer;
|
60 | 57 | using TimerCompleteCallback = void (*)(Layer * aLayer, void * appState);
|
61 | 58 |
|
@@ -148,6 +145,31 @@ class DLL_EXPORT Layer
|
148 | 145 | */
|
149 | 146 | virtual CHIP_ERROR ScheduleWork(TimerCompleteCallback aComplete, void * aAppState) = 0;
|
150 | 147 |
|
| 148 | + /** |
| 149 | + * @brief |
| 150 | + * Schedules a lambda even to be run as soon as possible in the CHIP context. This function is not thread-safe, |
| 151 | + * it must be called with in the CHIP context |
| 152 | + * |
| 153 | + * @param[in] event A object encapsulate the context of a lambda |
| 154 | + * |
| 155 | + * @retval CHIP_NO_ERROR On success. |
| 156 | + * @retval other Platform-specific errors generated indicating the reason for failure. |
| 157 | + */ |
| 158 | + CHIP_ERROR ScheduleLambdaBridge(LambdaBridge && event); |
| 159 | + |
| 160 | + /** |
| 161 | + * @brief |
| 162 | + * Schedules a lambda object to be run as soon as possible in the CHIP context. This function is not thread-safe, |
| 163 | + * it must be called with in the CHIP context |
| 164 | + */ |
| 165 | + template <typename Lambda> |
| 166 | + CHIP_ERROR ScheduleLambda(const Lambda & lambda) |
| 167 | + { |
| 168 | + LambdaBridge bridge; |
| 169 | + bridge.Initialize(lambda); |
| 170 | + return ScheduleLambdaBridge(std::move(bridge)); |
| 171 | + } |
| 172 | + |
151 | 173 | private:
|
152 | 174 | // Copy and assignment NOT DEFINED
|
153 | 175 | Layer(const Layer &) = delete;
|
@@ -202,36 +224,6 @@ class LayerLwIP : public Layer
|
202 | 224 | */
|
203 | 225 | virtual CHIP_ERROR PostEvent(Object & aTarget, EventType aEventType, uintptr_t aArgument) = 0;
|
204 | 226 |
|
205 |
| - /** |
206 |
| - * This posts an event / message of the specified type with the provided argument to this instance's platform-specific event |
207 |
| - * queue. |
208 |
| - * |
209 |
| - * @param[in] event A object encapsulate the context of a lambda |
210 |
| - * |
211 |
| - * @retval CHIP_NO_ERROR On success. |
212 |
| - * @retval CHIP_ERROR_INCORRECT_STATE If the state of the Layer object is incorrect. |
213 |
| - * @retval CHIP_ERROR_NO_MEMORY If the event queue is already full. |
214 |
| - * @retval other Platform-specific errors generated indicating the reason for failure. |
215 |
| - */ |
216 |
| - virtual CHIP_ERROR ScheduleLambdaBridge(const LambdaBridge & event) = 0; |
217 |
| - |
218 |
| - template <typename Lambda> |
219 |
| - CHIP_ERROR ScheduleLambda(const Lambda & lambda) |
220 |
| - { |
221 |
| - LambdaBridge event; |
222 |
| - |
223 |
| - // memcpy is used to move the lambda into the event queue, so it must be trivially copyable |
224 |
| - static_assert(std::is_trivially_copyable<Lambda>::value); |
225 |
| - static_assert(sizeof(Lambda) <= CHIP_CONFIG_LAMBDA_EVENT_SIZE); |
226 |
| - static_assert(alignof(Lambda) <= CHIP_CONFIG_LAMBDA_EVENT_ALIGN); |
227 |
| - |
228 |
| - // Implicit cast a capture-less lambda into a raw function pointer. |
229 |
| - event.LambdaProxy = [](const void * body) { (*static_cast<const Lambda *>(body))(); }; |
230 |
| - memcpy(event.LambdaBody, &lambda, sizeof(Lambda)); |
231 |
| - |
232 |
| - return ScheduleLambdaBridge(event); |
233 |
| - } |
234 |
| - |
235 | 227 | protected:
|
236 | 228 | // Provide access to private members of EventHandlerDelegate.
|
237 | 229 | struct LwIPEventHandlerDelegate : public EventHandlerDelegate
|
|
0 commit comments