1 /*
2 * Copyright (c) 2024 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 #include <module_update_queue.h>
17
18 namespace OHOS {
19 namespace SysInstaller {
20 namespace {
21 constexpr int MAX_SIZE = 100;
22 }
23
ModuleUpdateQueue()24 ModuleUpdateQueue::ModuleUpdateQueue() : size_(0), head_(0), tail_(0)
25 {
26 queue_.resize(MAX_SIZE);
27 }
28
Stop()29 void ModuleUpdateQueue::Stop()
30 {
31 std::unique_lock<std::mutex> locker(mtx_);
32 isStop_ = true;
33 notFull_.notify_all();
34 notEmpty_.notify_all();
35 }
36
Put(std::pair<int32_t,std::string> & saStatus)37 void ModuleUpdateQueue::Put(std::pair<int32_t, std::string> &saStatus)
38 {
39 while (IsFull()) {
40 std::unique_lock<std::mutex> locker(mtx_);
41 if (isStop_) {
42 return;
43 }
44 notFull_.wait(locker);
45 }
46 std::unique_lock<std::mutex> locker(mtx_);
47 queue_[tail_] = saStatus;
48 tail_ = (tail_ + 1) % MAX_SIZE;
49 size_++;
50 notEmpty_.notify_one();
51 }
52
Pop()53 std::pair<int32_t, std::string> ModuleUpdateQueue::Pop()
54 {
55 while (IsEmpty()) {
56 std::unique_lock<std::mutex> locker(mtx_);
57 if (isStop_) {
58 return std::make_pair(0, "");
59 }
60 notEmpty_.wait(locker);
61 }
62 std::unique_lock<std::mutex> locker(mtx_);
63 std::pair<int32_t, std::string> saStatus = std::move(queue_[head_]);
64 head_ = (head_ + 1) % MAX_SIZE;
65 size_--;
66 notFull_.notify_one();
67 return saStatus;
68 }
69
IsEmpty()70 bool ModuleUpdateQueue::IsEmpty()
71 {
72 std::unique_lock<std::mutex> locker(mtx_);
73 return size_ == 0;
74 }
75
IsFull()76 bool ModuleUpdateQueue::IsFull()
77 {
78 std::unique_lock<std::mutex> locker(mtx_);
79 return size_ == MAX_SIZE;
80 }
81 } // SysInstaller
82 } // namespace OHOS