@@ -46,31 +46,50 @@ CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_InitChipStack(void)
46
46
47
47
vTaskSetTimeOutState (&mNextTimerBaseTime );
48
48
mNextTimerDurationTicks = 0 ;
49
- mEventLoopTask = NULL ;
50
- mChipTimerActive = false ;
49
+ // TODO: This nulling out of mEventLoopTask should happen when we shut down
50
+ // the task, not here!
51
+ mEventLoopTask = NULL ;
52
+ mChipTimerActive = false ;
51
53
54
+ // We support calling Shutdown followed by InitChipStack, because some tests
55
+ // do that. To keep things simple for existing consumers, we keep not
56
+ // destroying our lock and queue in shutdown, but rather check whether they
57
+ // already exist here before trying to create them.
58
+
59
+ if (mChipStackLock == NULL )
60
+ {
52
61
#if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_SEMAPHORE) && CHIP_CONFIG_FREERTOS_USE_STATIC_SEMAPHORE
53
- mChipStackLock = xSemaphoreCreateMutexStatic (&mChipStackLockMutex );
62
+ mChipStackLock = xSemaphoreCreateMutexStatic (&mChipStackLockMutex );
54
63
#else
55
- mChipStackLock = xSemaphoreCreateMutex ();
64
+ mChipStackLock = xSemaphoreCreateMutex ();
56
65
#endif // CHIP_CONFIG_FREERTOS_USE_STATIC_SEMAPHORE
57
66
58
- if (mChipStackLock == NULL )
59
- {
60
- ChipLogError (DeviceLayer, " Failed to create CHIP stack lock" );
61
- ExitNow (err = CHIP_ERROR_NO_MEMORY);
67
+ if (mChipStackLock == NULL )
68
+ {
69
+ ChipLogError (DeviceLayer, " Failed to create CHIP stack lock" );
70
+ ExitNow (err = CHIP_ERROR_NO_MEMORY);
71
+ }
62
72
}
63
73
74
+ if (mChipEventQueue == NULL )
75
+ {
64
76
#if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_QUEUE) && CHIP_CONFIG_FREERTOS_USE_STATIC_QUEUE
65
- mChipEventQueue =
66
- xQueueCreateStatic (CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE, sizeof (ChipDeviceEvent), mEventQueueBuffer , &mEventQueueStruct );
77
+ mChipEventQueue = xQueueCreateStatic (CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE, sizeof (ChipDeviceEvent), mEventQueueBuffer ,
78
+ &mEventQueueStruct );
67
79
#else
68
- mChipEventQueue = xQueueCreate (CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE, sizeof (ChipDeviceEvent));
80
+ mChipEventQueue = xQueueCreate (CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE, sizeof (ChipDeviceEvent));
69
81
#endif
70
- if (mChipEventQueue == NULL )
82
+ if (mChipEventQueue == NULL )
83
+ {
84
+ ChipLogError (DeviceLayer, " Failed to allocate CHIP event queue" );
85
+ ExitNow (err = CHIP_ERROR_NO_MEMORY);
86
+ }
87
+ }
88
+ else
71
89
{
72
- ChipLogError (DeviceLayer, " Failed to allocate CHIP event queue" );
73
- ExitNow (err = CHIP_ERROR_NO_MEMORY);
90
+ // Clear out any events that might be stuck in the queue, so we start
91
+ // with a clean slate, as if we had just re-created the queue.
92
+ xQueueReset (mChipEventQueue );
74
93
}
75
94
76
95
mShouldRunEventLoop .store (false );
@@ -145,7 +164,7 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
145
164
ChipDeviceEvent event;
146
165
147
166
// Lock the CHIP stack.
148
- Impl ()-> LockChipStack () ;
167
+ StackLock lock ;
149
168
150
169
bool oldShouldRunEventLoop = false ;
151
170
if (!mShouldRunEventLoop .compare_exchange_strong (oldShouldRunEventLoop /* expected */ , true /* desired */ ))
@@ -198,13 +217,13 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
198
217
waitTime = portMAX_DELAY;
199
218
}
200
219
201
- // Unlock the CHIP stack, allowing other threads to enter CHIP while the event loop thread is sleeping.
202
- Impl ()-> UnlockChipStack ();
203
-
204
- BaseType_t eventReceived = xQueueReceive ( mChipEventQueue , & event, waitTime);
205
-
206
- // Lock the CHIP stack.
207
- Impl ()-> LockChipStack ();
220
+ BaseType_t eventReceived = pdFALSE;
221
+ {
222
+ // Unlock the CHIP stack, allowing other threads to enter CHIP while
223
+ // the event loop thread is sleeping.
224
+ StackUnlock unlock;
225
+ eventReceived = xQueueReceive ( mChipEventQueue , &event, waitTime);
226
+ }
208
227
209
228
// If an event was received, dispatch it. Continue receiving events from the queue and
210
229
// dispatching them until the queue is empty.
@@ -236,6 +255,9 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::EventLoopTaskMain(void * ar
236
255
{
237
256
ChipLogDetail (DeviceLayer, " CHIP task running" );
238
257
static_cast <GenericPlatformManagerImpl_FreeRTOS<ImplClass> *>(arg)->Impl ()->RunEventLoop ();
258
+ // TODO: At this point, should we not
259
+ // vTaskDelete(static_cast<GenericPlatformManagerImpl_FreeRTOS<ImplClass> *>(arg)->mEventLoopTask)?
260
+ // Or somehow get our caller to do it once this thread is joined?
239
261
}
240
262
241
263
template <class ImplClass >
@@ -275,6 +297,7 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::PostEventFromISR(const Chip
275
297
template <class ImplClass >
276
298
void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_Shutdown(void )
277
299
{
300
+ GenericPlatformManagerImpl<ImplClass>::_Shutdown ();
278
301
}
279
302
280
303
template <class ImplClass >
0 commit comments