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 "thumbnail_generate_worker_manager.h"
17 
18 #include "medialibrary_errno.h"
19 #include "media_log.h"
20 
21 namespace OHOS {
22 namespace Media {
~ThumbnailGenerateWorkerManager()23 ThumbnailGenerateWorkerManager::~ThumbnailGenerateWorkerManager()
24 {
25     thumbnailWorkerMap_.Clear();
26 }
27 
GetThumbnailWorker(const ThumbnailTaskType & taskType)28 ThumbnailWorkerPtr ThumbnailGenerateWorkerManager::GetThumbnailWorker(const ThumbnailTaskType &taskType)
29 {
30     ThumbnailWorkerPtr ptr;
31     if (thumbnailWorkerMap_.Find(taskType, ptr)) {
32         return ptr;
33     }
34 
35     int status = InitThumbnailWorker(taskType);
36     if (status != E_OK) {
37         MEDIA_ERR_LOG("get thumbnail worker failed, status: %{public}d", status);
38         return nullptr;
39     }
40     thumbnailWorkerMap_.Find(taskType, ptr);
41     return ptr;
42 }
43 
InitThumbnailWorker(const ThumbnailTaskType & taskType)44 int32_t ThumbnailGenerateWorkerManager::InitThumbnailWorker(const ThumbnailTaskType &taskType)
45 {
46     std::lock_guard<std::mutex> lock(mutex_);
47     ThumbnailWorkerPtr ptr;
48     if (thumbnailWorkerMap_.Find(taskType, ptr)) {
49         return E_OK;
50     }
51 
52     ptr = std::make_shared<ThumbnailGenerateWorker>();
53     int32_t status = ptr->Init(taskType);
54     if (status != E_OK) {
55         MEDIA_ERR_LOG("init thumbnail worker failed, status: %{public}d", status);
56         return status;
57     }
58     thumbnailWorkerMap_.Insert(taskType, ptr);
59     return E_OK;
60 }
61 
ClearAllTask()62 void ThumbnailGenerateWorkerManager::ClearAllTask()
63 {
64     if (thumbnailWorkerMap_.IsEmpty()) {
65         MEDIA_INFO_LOG("thumbnail worker empty, no need to clear");
66         return;
67     }
68 
69     MEDIA_INFO_LOG("ClearAllTask in thumbnail thread pool");
70     thumbnailWorkerMap_.Iterate([](ThumbnailTaskType taskType, ThumbnailWorkerPtr &ptr) {
71         if (ptr != nullptr) {
72             ptr->ReleaseTaskQueue(ThumbnailTaskPriority::HIGH);
73             ptr->ReleaseTaskQueue(ThumbnailTaskPriority::MID);
74             ptr->ReleaseTaskQueue(ThumbnailTaskPriority::LOW);
75         }
76     });
77 }
78 
TryCloseThumbnailWorkerTimer()79 void ThumbnailGenerateWorkerManager::TryCloseThumbnailWorkerTimer()
80 {
81     if (thumbnailWorkerMap_.IsEmpty()) {
82         MEDIA_INFO_LOG("thumbnail worker empty, no need to close timer");
83         return;
84     }
85 
86     thumbnailWorkerMap_.Iterate([](ThumbnailTaskType taskType, ThumbnailWorkerPtr &ptr) {
87         if (ptr != nullptr) {
88             ptr->TryCloseTimer();
89         }
90     });
91 }
92 } // namespace Media
93 } // namespace OHOS