1 /* 2 * Copyright (c) 2023 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 #ifndef FFRT_CONCURRENT_QUEUE_H 16 #define FFRT_CONCURRENT_QUEUE_H 17 18 #include "base_queue.h" 19 20 namespace ffrt { 21 class ConcurrentQueue : public BaseQueue { 22 public: 23 explicit ConcurrentQueue(const int maxConcurrency = 1) maxConcurrency_(maxConcurrency)24 : maxConcurrency_(maxConcurrency) 25 { 26 dequeFunc_ = QueueStrategy<QueueTask>::DequeSingleByPriority; 27 } 28 ~ConcurrentQueue() override; 29 30 int Push(QueueTask* task) override; 31 QueueTask* Pull() override; GetActiveStatus()32 bool GetActiveStatus() override 33 { 34 return concurrency_.load(); 35 } 36 GetQueueType()37 int GetQueueType() const override 38 { 39 return ffrt_queue_concurrent; 40 } 41 42 void Stop() override; 43 bool SetLoop(Loop* loop); 44 void Remove() override; 45 int Remove(const char* name) override; 46 int Remove(const QueueTask* task) override; 47 bool HasTask(const char* name) override; 48 ClearLoop()49 inline bool ClearLoop() 50 { 51 if (loop_ == nullptr) { 52 return false; 53 } 54 55 loop_ = nullptr; 56 return true; 57 } 58 IsOnLoop()59 bool IsOnLoop() override 60 { 61 return isOnLoop_.load(); 62 } 63 64 private: 65 int PushDelayTaskToTimer(QueueTask* task); 66 67 Loop* loop_ { nullptr }; 68 std::atomic_bool isOnLoop_ { false }; 69 70 int maxConcurrency_ {1}; 71 std::atomic_int concurrency_ {0}; 72 std::multimap<uint64_t, QueueTask*> whenMapVec_[4]; 73 }; 74 75 std::unique_ptr<BaseQueue> CreateConcurrentQueue(const ffrt_queue_attr_t* attr); 76 } // namespace ffrt 77 #endif // FFRT_CONCURRENT_QUEUE_H 78