Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / FreeRTOS / GenericThreadStackManagerImpl_FreeRTOS.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2018 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *          Provides an generic implementation of ThreadStackManager features
23  *          for use on FreeRTOS platforms.
24  */
25
26 #pragma once
27
28 #if defined(ESP_PLATFORM)
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/semphr.h"
31 #include "freertos/task.h"
32 #include "timers.h"
33 #else
34 #include "FreeRTOS.h"
35 #include "semphr.h"
36 #include "task.h"
37 #include "timers.h"
38 #endif
39
40 namespace chip {
41 namespace DeviceLayer {
42
43 class ThreadStackManagerImpl;
44
45 namespace Internal {
46
47 /**
48  * Provides a generic implementation of ThreadStackManager features that works on FreeRTOS platforms.
49  *
50  * This template contains implementations of select features from the ThreadStackManager abstract
51  * interface that are suitable for use on FreeRTOS-based platforms.  It is intended to be
52  * inherited, directly or indirectly, by the ThreadStackManagerImpl class, which also appears as
53  * the template's ImplClass parameter.
54  */
55 template <class ImplClass>
56 class GenericThreadStackManagerImpl_FreeRTOS
57 {
58
59 protected:
60     // ===== Methods that implement the ThreadStackManager abstract interface.
61
62     CHIP_ERROR _StartThreadTask(void);
63     void _LockThreadStack(void);
64     bool _TryLockThreadStack(void);
65     void _UnlockThreadStack(void);
66
67     // ===== Members available to the implementation subclass.
68
69     SemaphoreHandle_t mThreadStackLock;
70     TaskHandle_t mThreadTask;
71
72     CHIP_ERROR DoInit();
73     void SignalThreadActivityPending();
74     BaseType_t SignalThreadActivityPendingFromISR();
75
76 private:
77     // ===== Private members for use by this class only.
78
79     inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
80
81     static void ThreadTaskMain(void * arg);
82     static void OnJoinerTimer(TimerHandle_t xTimer);
83
84     portTickType mJoinerExpire;
85     bool mJoinerStartPending = false;
86
87 #if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_TASK) && CHIP_CONFIG_FREERTOS_USE_STATIC_TASK
88     StackType_t mThreadStack[CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE / sizeof(StackType_t)];
89     StaticTask_t mThreadTaskStruct;
90 #endif
91 };
92
93 // Instruct the compiler to instantiate the template only when explicitly told to do so.
94 extern template class GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>;
95
96 } // namespace Internal
97 } // namespace DeviceLayer
98 } // namespace chip