• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #define MLOG_TAG "MtpMonitor"
16 #include "mtp_monitor.h"
17 #include "media_log.h"
18 #include "mtp_file_observer.h"
19 #include "mtp_medialibrary_manager.h"
20 #include "mtp_store_observer.h"
21 using namespace std;
22 namespace OHOS {
23 namespace Media {
24 constexpr int32_t IPC_SERVER_RELEASED = -204;
25 constexpr int32_t IPC_OBJECT_INVALID = -4;
26 constexpr int32_t ERROR_SLEEP_TIME = 10;
27 
Start()28 void MtpMonitor::Start()
29 {
30     MEDIA_INFO_LOG("MtpMonitor::Start begin threadRunning_:%{public}d", threadRunning_.load());
31     if (!threadRunning_.load()) {
32         threadRunning_.store(true);
33         if (thread_ == nullptr) {
34             thread_ = std::make_unique<std::thread>(&MtpMonitor::Run, this);
35         }
36     }
37     MEDIA_INFO_LOG("MtpMonitor::Start end threadRunning_:%{public}d", threadRunning_.load());
38 }
39 
Stop()40 void MtpMonitor::Stop()
41 {
42     MEDIA_INFO_LOG("MtpMonitor::Stop begin threadRunning_:%{public}d", threadRunning_.load());
43     threadRunning_.store(false);
44     if (thread_ != nullptr) {
45         if (thread_->joinable()) {
46             thread_->join();
47         }
48         thread_ = nullptr;
49     }
50     MEDIA_INFO_LOG("MtpMonitor::Stop end threadRunning_:%{public}d", threadRunning_.load());
51 }
52 
Run()53 void MtpMonitor::Run()
54 {
55     MEDIA_INFO_LOG("MtpMonitor::Run start");
56     pthread_setname_np(pthread_self(), "MtpMonitor::Run");
57     while (threadRunning_.load()) {
58         if (operationPtr_ == nullptr) {
59             operationPtr_ = make_shared<MtpOperation>();
60         }
61         if (operationPtr_ != nullptr) {
62             int32_t errorCode = operationPtr_->Execute();
63             if (errorCode == IPC_SERVER_RELEASED || errorCode == IPC_OBJECT_INVALID) {
64                 break;
65             }
66             if (errorCode != 0 && threadRunning_.load()) {
67                 std::this_thread::sleep_for(std::chrono::milliseconds(ERROR_SLEEP_TIME));
68             }
69         }
70     }
71     MEDIA_INFO_LOG("MtpMonitor::Run break");
72     {
73         MtpFileObserver::GetInstance().StopFileInotify();
74         MtpStoreObserver::StopObserver();
75         MtpMedialibraryManager::GetInstance()->Clear();
76     }
77     if (operationPtr_ != nullptr) {
78         operationPtr_->Stop();
79         operationPtr_.reset();
80     }
81     threadRunning_.store(false);
82     MEDIA_INFO_LOG("MtpMonitor::Run end");
83 }
84 } // namespace Media
85 } // namespace OHOS
86