Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / GenericPlatformManagerImpl_POSIX.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2018 Nest Labs, Inc.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 /**
20  *    @file
21  *          Provides an generic implementation of PlatformManager features
22  *          for use on Linux platforms.
23  */
24
25 #pragma once
26
27 #include <platform/internal/GenericPlatformManagerImpl.h>
28
29 #include <fcntl.h>
30 #include <sched.h>
31 #include <sys/select.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34
35 #include <atomic>
36 #include <pthread.h>
37 #include <queue>
38
39 namespace chip {
40 namespace DeviceLayer {
41 namespace Internal {
42
43 /**
44  * Provides a generic implementation of PlatformManager features that works on any OSAL platform.
45  *
46  * This template contains implementations of select features from the PlatformManager abstract
47  * interface that are suitable for use on OSAL-based platforms.  It is intended to be inherited
48  * (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's
49  * ImplClass parameter.
50  */
51 template <class ImplClass>
52 class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl<ImplClass>
53 {
54 protected:
55     // Members for select loop
56     int mMaxFd;
57     fd_set mReadSet;
58     fd_set mWriteSet;
59     fd_set mErrorSet;
60     struct timeval mNextTimeout;
61
62     // OS-specific members (pthread)
63     pthread_mutex_t mChipStackLock;
64     std::queue<ChipDeviceEvent> mChipEventQueue;
65
66     pthread_t mChipTask;
67     pthread_attr_t mChipTaskAttr;
68     struct sched_param mChipTaskSchedParam;
69
70     // ===== Methods that implement the PlatformManager abstract interface.
71
72     CHIP_ERROR
73     _InitChipStack();
74     void _LockChipStack();
75     bool _TryLockChipStack();
76     void _UnlockChipStack();
77     void _PostEvent(const ChipDeviceEvent * event);
78     void _RunEventLoop();
79     CHIP_ERROR _StartEventLoopTask();
80     CHIP_ERROR _StartChipTimer(int64_t durationMS);
81     CHIP_ERROR _Shutdown();
82
83     // ===== Methods available to the implementation subclass.
84
85 private:
86     // ===== Private members for use by this class only.
87
88     inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
89
90     void SysUpdate();
91     void SysProcess();
92     static void SysOnEventSignal(void * arg);
93
94     void ProcessDeviceEvents();
95
96     std::atomic<bool> mShouldRunEventLoop;
97     static void * EventLoopTaskMain(void * arg);
98 };
99
100 // Instruct the compiler to instantiate the template only when explicitly told to do so.
101 extern template class GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>;
102
103 } // namespace Internal
104 } // namespace DeviceLayer
105 } // namespace chip