-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
GenericPlatformManagerImpl_POSIX.h
122 lines (100 loc) · 3.5 KB
/
GenericPlatformManagerImpl_POSIX.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2018 Nest Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Provides an generic implementation of PlatformManager features
* for use on Linux platforms.
*/
#pragma once
#include <platform/DeviceSafeQueue.h>
#include <platform/internal/GenericPlatformManagerImpl.h>
#include <fcntl.h>
#include <sched.h>
#include <sys/time.h>
#include <unistd.h>
#include <atomic>
#include <pthread.h>
#include <queue>
namespace chip {
namespace DeviceLayer {
namespace Internal {
/**
* Provides a generic implementation of PlatformManager features that works on any OSAL platform.
*
* This template contains implementations of select features from the PlatformManager abstract
* interface that are suitable for use on OSAL-based platforms. It is intended to be inherited
* (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's
* ImplClass parameter.
*/
template <class ImplClass>
class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl<ImplClass>
{
protected:
// OS-specific members (pthread)
pthread_mutex_t mChipStackLock;
enum TaskType
{
kExternallyManagedTask = 0,
kInternallyManagedTask = 1
};
pthread_t mChipTask;
bool mHasValidChipTask = false;
TaskType mTaskType;
pthread_cond_t mEventQueueStoppedCond;
pthread_mutex_t mStateLock;
//
// TODO: This variable is very similar to mMainLoopIsStarted, track the
// cleanup and consolidation in this issue:
//
bool mEventQueueHasStopped = false;
pthread_attr_t mChipTaskAttr;
struct sched_param mChipTaskSchedParam;
#if CHIP_STACK_LOCK_TRACKING_ENABLED
bool mMainLoopStarted = false;
bool mChipStackIsLocked = false;
pthread_t mChipStackLockOwnerThread;
#endif
// ===== Methods that implement the PlatformManager abstract interface.
CHIP_ERROR
_InitChipStack();
void _LockChipStack();
bool _TryLockChipStack();
void _UnlockChipStack();
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
void _RunEventLoop();
CHIP_ERROR _StartEventLoopTask();
CHIP_ERROR _StopEventLoopTask();
CHIP_ERROR _StartChipTimer(int64_t durationMS);
CHIP_ERROR _Shutdown();
#if CHIP_STACK_LOCK_TRACKING_ENABLED
bool _IsChipStackLockedByCurrentThread() const;
#endif
// ===== Methods available to the implementation subclass.
private:
// ===== Private members for use by this class only.
inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
void ProcessDeviceEvents();
DeviceSafeQueue mChipEventQueue;
std::atomic<bool> mShouldRunEventLoop;
static void * EventLoopTaskMain(void * arg);
};
// Instruct the compiler to instantiate the template only when explicitly told to do so.
extern template class GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>;
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip