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 <list> 22 #include <mutex> 23 #include <queue> 24 #include <thread> 25 26 #define ASYNC_WORKER_API_EXPORT __attribute__ ((visibility ("default"))) 27 namespace OHOS { 28 namespace Media { 29 class AsyncTaskData { 30 public: AsyncTaskData()31 AsyncTaskData() {}; ~AsyncTaskData()32 virtual ~AsyncTaskData() {}; 33 std::string dataDisplay; 34 }; 35 36 using MediaLibraryExecute = void (*)(AsyncTaskData *data); 37 38 class MediaLibraryAsyncTask { 39 public: MediaLibraryAsyncTask(MediaLibraryExecute executor,AsyncTaskData * data)40 MediaLibraryAsyncTask(MediaLibraryExecute executor, AsyncTaskData *data) : executor_(executor), data_(data) {} MediaLibraryAsyncTask()41 MediaLibraryAsyncTask() : MediaLibraryAsyncTask(nullptr, nullptr) {} ~MediaLibraryAsyncTask()42 virtual ~MediaLibraryAsyncTask() 43 { 44 delete data_; 45 data_ = nullptr; 46 } 47 48 MediaLibraryExecute executor_; 49 AsyncTaskData *data_; 50 }; 51 52 class MediaLibraryAsyncWorker { 53 public: 54 virtual ~MediaLibraryAsyncWorker(); 55 ASYNC_WORKER_API_EXPORT static std::shared_ptr<MediaLibraryAsyncWorker> GetInstance(); 56 ASYNC_WORKER_API_EXPORT void Interrupt(); 57 ASYNC_WORKER_API_EXPORT void Stop(); 58 ASYNC_WORKER_API_EXPORT int32_t AddTask(const std::shared_ptr<MediaLibraryAsyncTask> &task, bool isFg); 59 60 private: 61 MediaLibraryAsyncWorker(); 62 void StartWorker(int num); 63 void Init(); 64 std::shared_ptr<MediaLibraryAsyncTask> GetFgTask(); 65 std::shared_ptr<MediaLibraryAsyncTask> GetBgTask(); 66 void ReleaseFgTask(); 67 void ReleaseBgTask(); 68 void WaitForTask(); 69 bool IsFgQueueEmpty(); 70 bool IsBgQueueEmpty(); 71 void SleepFgWork(); 72 void SleepBgWork(); 73 74 static std::mutex instanceLock_; 75 static std::shared_ptr<MediaLibraryAsyncWorker> asyncWorkerInstance_; 76 std::atomic<bool> isThreadRunning_; 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 std::list<std::thread> threads_; 88 }; 89 } // namespace Media 90 } // namespace OHOS 91 92 #endif // FRAMEWORKS_SERVICE_MEDIA_ASYNC_WORKER_INCLUDE_MEDIALIBRARY_ASYNC_WORKER_H_