• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MLOG_TAG "EnhancementThreadManager"
17 
18 #include "enhancement_thread_manager.h"
19 
20 #include "enhancement_service_callback.h"
21 #include "media_log.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace Media {
27 static constexpr int32_t WAIT_TIME = 30;
28 static constexpr int32_t WAIT_RELEASE = 50;
29 
EnhancementThreadManager()30 EnhancementThreadManager::EnhancementThreadManager()
31 {
32     stop = false;
33 #ifdef ABILITY_CLOUD_ENHANCEMENT_SUPPORT
34     isThreadAlive = true;
35     thread(&EnhancementThreadManager::DealWithTasks, this).detach();
36 #endif
37 }
38 
~EnhancementThreadManager()39 EnhancementThreadManager::~EnhancementThreadManager()
40 {
41     stop = true;
42     condVar_.notify_all();
43     unique_lock<mutex> lock(releaseMutex_);
44     releaseVar_.wait_for(lock, chrono::milliseconds(WAIT_RELEASE), [this]() {
45         return isThreadAlive == false;
46     });
47 }
48 
StartConsumerThread()49 void EnhancementThreadManager::StartConsumerThread()
50 {
51     if (!isThreadAlive) {
52         isThreadAlive = true;
53         thread(&EnhancementThreadManager::DealWithTasks, this).detach();
54     }
55 }
56 
OnProducerCallback(CloudEnhancementThreadTask & task)57 void EnhancementThreadManager::OnProducerCallback(CloudEnhancementThreadTask& task)
58 {
59     {
60         lock_guard<mutex> lock(queueMutex_);
61         taskQueue_.push(task);
62         StartConsumerThread();
63     }
64     condVar_.notify_one();
65 }
66 
DealWithTasks()67 void EnhancementThreadManager::DealWithTasks()
68 {
69     MEDIA_INFO_LOG("cloud enhancement consumer thread start");
70     bool loopCondition = true;
71     while (loopCondition) {
72         bool needExtraWork = false;
73         CloudEnhancementThreadTask task("", 0, nullptr, 0, false);
74         {
75             unique_lock<mutex> lock(queueMutex_);
76             if (condVar_.wait_for(lock, chrono::seconds(WAIT_TIME), [this]() {
77                 return !taskQueue_.empty() || stop;
78             })) {
79                 if (stop && taskQueue_.empty()) {
80                     loopCondition = false;
81                     break;
82                 }
83                 task = taskQueue_.front();
84                 taskQueue_.pop();
85 
86                 if (taskQueue_.empty()) {
87                     needExtraWork = true;
88                 }
89             } else {
90                 loopCondition = false;
91                 break;
92             }
93         }
94         if (task.taskId.empty()) {
95             continue;
96         }
97         task.isSuccessed ? ExecSuccessedTask(task) : ExecFailedTask(task);
98         if (needExtraWork) {
99             ExecExtraWork();
100         }
101     }
102     MEDIA_INFO_LOG("cloud enhancement thread task queue is empty for %{public}d seconds", WAIT_TIME);
103     isThreadAlive = false;
104 }
105 
ExecSuccessedTask(CloudEnhancementThreadTask & task)106 void EnhancementThreadManager::ExecSuccessedTask(CloudEnhancementThreadTask& task)
107 {
108 #ifdef ABILITY_CLOUD_ENHANCEMENT_SUPPORT
109     EnhancementServiceCallback::DealWithSuccessedTask(task);
110 #endif
111 }
112 
ExecFailedTask(CloudEnhancementThreadTask & task)113 void EnhancementThreadManager::ExecFailedTask(CloudEnhancementThreadTask& task)
114 {
115 #ifdef ABILITY_CLOUD_ENHANCEMENT_SUPPORT
116     EnhancementServiceCallback::DealWithFailedTask(task);
117 #endif
118 }
119 
ExecExtraWork()120 void EnhancementThreadManager::ExecExtraWork()
121 {
122 #ifdef ABILITY_CLOUD_ENHANCEMENT_SUPPORT
123     EnhancementServiceCallback::UpdateAlbumsForCloudEnhancement();
124 #endif
125 }
126 } // namespace Media
127 } // namespace OHOS