blob: b7c339c83ab89fa81fdc369aabbed9be0d517fca [file] [log] [blame]
Yufeng Wang0322eb12021-06-17 23:51:34 -07001/*
2 *
3 * Copyright (c) 2021 Project CHIP Authors
4 * All rights reserved.
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 * This file declares the CHIP device event queue which operates in a FIFO context (first-in first-out),
22 * and provides a specific set of member functions to access its elements wth thread safety.
23 */
24
25#pragma once
26
27#include <mutex>
28#include <queue>
29
Zang MingJie53dd5832021-09-03 03:05:16 +080030#include <lib/core/CHIPCore.h>
Yufeng Wang0322eb12021-06-17 23:51:34 -070031#include <platform/CHIPDeviceConfig.h>
32#include <platform/CHIPDeviceEvent.h>
33
34namespace chip {
35namespace DeviceLayer {
36namespace Internal {
37
38/**
39 * @class DeviceSafeQueue
40 *
41 * @brief
42 * This class represents a thread-safe message queue implemented with C++ Standard Library, the message queue
43 * is used by the CHIP event loop to hold incoming messages. Each message is sequentially dequeued, decoded,
44 * and then an action is performed.
45 *
46 */
47class DeviceSafeQueue
48{
49public:
50 DeviceSafeQueue() = default;
51 ~DeviceSafeQueue() = default;
52
53 void Push(const ChipDeviceEvent & event);
54 bool Empty();
Andrei Litvin1145bac2022-11-03 17:39:11 -040055 ChipDeviceEvent PopFront();
Yufeng Wang0322eb12021-06-17 23:51:34 -070056
57private:
58 std::queue<ChipDeviceEvent> mEventQueue;
59 std::mutex mEventQueueLock;
60
Karsten Sperling38d6a482023-09-29 15:50:08 +130061 DeviceSafeQueue(const DeviceSafeQueue &) = delete;
Yufeng Wang0322eb12021-06-17 23:51:34 -070062 DeviceSafeQueue & operator=(const DeviceSafeQueue &) = delete;
63};
64
65} // namespace Internal
66} // namespace DeviceLayer
67} // namespace chip