1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_MESSAGE_QUEUE_H
17 #define OHOS_MESSAGE_QUEUE_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <mutex>
23 #include "internal_message.h"
24 
25 namespace OHOS {
26 namespace Wifi {
27 #define TIME_USEC_1000 1000
28 #define WIFI_TIME_INTERVAL 30000
29 class MessageQueue {
30 public:
31     /**
32      * @Description : Construct a new Message Queue object.
33      *
34      */
35     MessageQueue();
36 
37     /**
38      * @Description : Destroy the Message Queue object.
39      *
40      */
41     ~MessageQueue();
42 
43     /**
44      * @Description : Inserting Messages into Queues.
45      *
46      * @param message - Message to be inserted.[in]
47      * @param handleTime - Message execution time.[in]
48      * @return true : success, false : failed.
49      */
50     bool AddMessageToQueue(InternalMessagePtr message, int64_t handleTime);
51 
52     /**
53      * @Description : Delete messages from the queue.
54      *
55      * @param messageName - Name of the message to be deleted.[in]
56      * @return true : success, false : failed.
57      */
58     bool DeleteMessageFromQueue(int messageName);
59 
60     /**
61      * @Description : Obtain messages from the queue for processing.
62      * If no message is found, the system blocks the messages.
63      *
64      */
65     InternalMessagePtr GetNextMessage();
66 
67     /**
68      * @Description : Obtain messages from the queue for processing.
69      * If no message is found, the system blocks the messages.
70      */
71     void StopQueueLoop();
72 
73 private:
74     /* Thread lock of operation queue */
75     std::mutex mMtxQueue;
76     /* Message Queuing */
77     InternalMessagePtr pMessageQueue;
78     /* No messages to be executed, blocking */
79     std::atomic<bool> mIsBlocked;
80     /* Exit Loop */
81     std::atomic<bool> mNeedQuit;
82     /* blocking condition variable */
83     std::condition_variable mCvQueue;
84 };
85 }  // namespace Wifi
86 }  // namespace OHOS
87 #endif