Yufeng Wang | 0322eb1 | 2021-06-17 23:51:34 -0700 | [diff] [blame] | 1 | /* |
| 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 MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 30 | #include <lib/core/CHIPCore.h> |
Yufeng Wang | 0322eb1 | 2021-06-17 23:51:34 -0700 | [diff] [blame] | 31 | #include <platform/CHIPDeviceConfig.h> |
| 32 | #include <platform/CHIPDeviceEvent.h> |
| 33 | |
| 34 | namespace chip { |
| 35 | namespace DeviceLayer { |
| 36 | namespace 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 | */ |
| 47 | class DeviceSafeQueue |
| 48 | { |
| 49 | public: |
| 50 | DeviceSafeQueue() = default; |
| 51 | ~DeviceSafeQueue() = default; |
| 52 | |
| 53 | void Push(const ChipDeviceEvent & event); |
| 54 | bool Empty(); |
Andrei Litvin | 1145bac | 2022-11-03 17:39:11 -0400 | [diff] [blame] | 55 | ChipDeviceEvent PopFront(); |
Yufeng Wang | 0322eb1 | 2021-06-17 23:51:34 -0700 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | std::queue<ChipDeviceEvent> mEventQueue; |
| 59 | std::mutex mEventQueueLock; |
| 60 | |
Karsten Sperling | 38d6a48 | 2023-09-29 15:50:08 +1300 | [diff] [blame] | 61 | DeviceSafeQueue(const DeviceSafeQueue &) = delete; |
Yufeng Wang | 0322eb1 | 2021-06-17 23:51:34 -0700 | [diff] [blame] | 62 | DeviceSafeQueue & operator=(const DeviceSafeQueue &) = delete; |
| 63 | }; |
| 64 | |
| 65 | } // namespace Internal |
| 66 | } // namespace DeviceLayer |
| 67 | } // namespace chip |