• 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 #ifndef FRAMEWORKS_SERVICE_MEDIA_PERIOD_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_
17 #define FRAMEWORKS_SERVICE_MEDIA_PERIOD_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <vector>
22 #include <map>
23 #include <mutex>
24 #include <thread>
25 
26 namespace OHOS {
27 namespace Media {
28 
29 #define EXPORT __attribute__ ((visibility ("default")))
30 #define COMPILE_HIDDEN __attribute__ ((visibility ("hidden")))
31 
32 class PeriodTaskData {
33 public:
PeriodTaskData()34     PeriodTaskData() {};
~PeriodTaskData()35     virtual ~PeriodTaskData() {};
36 };
37 
38 enum PeriodTaskType {
39     COMMON_NOTIFY,
40     CLOUD_ANALYSIS_ALBUM
41 };
42 
43 using PeriodExecute = void (*)(PeriodTaskData *data);
44 class MedialibraryPeriodTask {
45 public:
MedialibraryPeriodTask(PeriodTaskType periodTaskType,int32_t period,std::string threadName)46     MedialibraryPeriodTask(PeriodTaskType periodTaskType, int32_t period, std::string threadName)
47         : periodTaskType_(periodTaskType), period_(period), threadName_(threadName) {}
~MedialibraryPeriodTask()48     virtual ~MedialibraryPeriodTask() {}
49     PeriodExecute executor_{nullptr};
50     PeriodTaskData *data_{nullptr};
51     PeriodTaskType periodTaskType_;
52     std::atomic<bool> isThreadRunning_{false};
53     std::atomic<bool> isStop_{true};
54     int32_t period_{0};
55     std::string threadName_;
56 };
57 
58 
59 class MediaLibraryPeriodWorker {
60 public:
61     virtual ~MediaLibraryPeriodWorker();
62     EXPORT static std::shared_ptr<MediaLibraryPeriodWorker> GetInstance();
63     EXPORT void StopThread(PeriodTaskType periodTaskType);
64     EXPORT bool IsThreadRunning(PeriodTaskType periodTaskType);
65     EXPORT bool StartTask(PeriodTaskType periodTaskType, PeriodExecute executor, PeriodTaskData *data);
66 
67 private:
68     COMPILE_HIDDEN MediaLibraryPeriodWorker();
69     COMPILE_HIDDEN void Init();
70     COMPILE_HIDDEN void HandleTask(PeriodTaskType periodTaskType);
71 
72     COMPILE_HIDDEN static std::atomic<bool> stop_;
73     COMPILE_HIDDEN static std::shared_ptr<MediaLibraryPeriodWorker> periodWorkerInstance_;
74     COMPILE_HIDDEN static std::mutex instanceMtx_;
75     COMPILE_HIDDEN static std::map<int32_t, std::shared_ptr<MedialibraryPeriodTask>> tasks_;
76     COMPILE_HIDDEN static std::mutex mtx_;
77 };
78 
79 } // namespace Media
80 } // namespace OHOS
81 #endif // OHOS_MEDIALIBRARY_NOTIFY_H