• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <chrono>
20 #include <condition_variable>
21 #include <mutex>
22 #include "internal_message.h"
23 
24 namespace OHOS {
25 namespace Wifi {
26 #define TIME_USEC_1000 1000
27 #define TIME_INTERVAL 30000
28 class MessageQueue {
29 public:
30     /**
31      * @Description : Construct a new Message Queue object.
32      *
33      */
34     MessageQueue();
35 
36     /**
37      * @Description : Destroy the Message Queue object.
38      *
39      */
40     ~MessageQueue();
41 
42     /**
43      * @Description : Inserting Messages into Queues.
44      *
45      * @param message - Message to be inserted.[in]
46      * @param handleTime - Message execution time.[in]
47      * @return true : success, false : failed.
48      */
49     bool AddMessageToQueue(InternalMessage *message, int64_t handleTime);
50 
51     /**
52      * @Description : Delete messages from the queue.
53      *
54      * @param messageName - Name of the message to be deleted.[in]
55      * @return true : success, false : failed.
56      */
57     bool DeleteMessageFromQueue(int messageName);
58 
59     /**
60      * @Description : Obtain messages from the queue for processing.
61      * If no message is found, the system blocks the messages.
62      *
63      */
64     InternalMessage *GetNextMessage();
65 
66     /**
67      * @Description : Obtain messages from the queue for processing.
68      * If no message is found, the system blocks the messages.
69      */
70     void StopQueueLoop();
71 
72 private:
73     /* Message Queuing */
74     InternalMessage *pMessageQueue;
75     /* No messages to be executed, blocking */
76     bool mIsBlocked;
77     /* Exit Loop */
78     bool mNeedQuit;
79     /* Thread lock of operation queue */
80     std::mutex mMtxQueue;
81     /* Blocked thread lock */
82     std::mutex mMtxBlock;
83     /* blocking condition variable */
84     std::condition_variable mCvQueue;
85 };
86 }  // namespace Wifi
87 }  // namespace OHOS
88 #endif