• 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 
16 #ifndef FRAMEWORKS_SERVICE_MEDIA_ASYNC_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_
17 #define FRAMEWORKS_SERVICE_MEDIA_ASYNC_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 
25 #define ASYNC_WORKER_API_EXPORT __attribute__ ((visibility ("default")))
26 namespace OHOS {
27 namespace Media {
28 class AsyncTaskData {
29 public:
AsyncTaskData()30     AsyncTaskData() {};
~AsyncTaskData()31     virtual ~AsyncTaskData() {};
32     std::string dataDisplay;
33 };
34 
35 using MediaLibraryExecute = void (*)(AsyncTaskData *data);
36 
37 class MediaLibraryAsyncTask {
38 public:
MediaLibraryAsyncTask(MediaLibraryExecute executor,AsyncTaskData * data)39     MediaLibraryAsyncTask(MediaLibraryExecute executor, AsyncTaskData *data) : executor_(executor), data_(data) {}
MediaLibraryAsyncTask()40     MediaLibraryAsyncTask() : MediaLibraryAsyncTask(nullptr, nullptr) {}
~MediaLibraryAsyncTask()41     virtual ~MediaLibraryAsyncTask()
42     {
43         delete data_;
44         data_ = nullptr;
45     }
46 
47     MediaLibraryExecute executor_;
48     AsyncTaskData *data_;
49 };
50 
51 class MediaLibraryAsyncWorker {
52 public:
53     virtual ~MediaLibraryAsyncWorker();
54     ASYNC_WORKER_API_EXPORT static std::shared_ptr<MediaLibraryAsyncWorker> GetInstance();
55     ASYNC_WORKER_API_EXPORT void Interrupt();
56     ASYNC_WORKER_API_EXPORT void Stop();
57     ASYNC_WORKER_API_EXPORT int32_t AddTask(const std::shared_ptr<MediaLibraryAsyncTask> &task, bool isFg);
58 
59 private:
60     MediaLibraryAsyncWorker();
61     void StartWorker();
62     void Init();
63     std::shared_ptr<MediaLibraryAsyncTask> GetFgTask();
64     std::shared_ptr<MediaLibraryAsyncTask> GetBgTask();
65     void ReleaseFgTask();
66     void ReleaseBgTask();
67     bool WaitForTask();
68     bool IsFgQueueEmpty();
69     bool IsBgQueueEmpty();
70     void SleepFgWork();
71     void SleepBgWork();
72 
73     static std::mutex instanceLock_;
74     static std::shared_ptr<MediaLibraryAsyncWorker> asyncWorkerInstance_;
75     std::atomic<bool> isThreadExist_;
76     bool isContinue_;
77     std::mutex bgTaskLock_;
78     std::queue<std::shared_ptr<MediaLibraryAsyncTask>> bgTaskQueue_;
79 
80     std::mutex fgTaskLock_;
81     std::queue<std::shared_ptr<MediaLibraryAsyncTask>> fgTaskQueue_;
82 
83     std::mutex bgWorkLock_;
84     std::condition_variable bgWorkCv_;
85     std::atomic<uint32_t> doneTotal_;
86 };
87 } // namespace Media
88 } // namespace OHOS
89 
90 #endif  // FRAMEWORKS_SERVICE_MEDIA_ASYNC_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_