|
| 1 | +///////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Tencent is pleased to support the open source community by making tgfx available. |
| 4 | +// |
| 5 | +// Copyright (C) 2025 Tencent. All rights reserved. |
| 6 | +// |
| 7 | +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 8 | +// in compliance with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// https://opensource.org/licenses/BSD-3-Clause |
| 11 | +// |
| 12 | +// unless required by applicable law or agreed to in writing, software distributed under the |
| 13 | +// license is distributed on an "as is" basis, without warranties or conditions of any kind, |
| 14 | +// either express or implied. see the license for the specific language governing permissions |
| 15 | +// and limitations under the license. |
| 16 | +// |
| 17 | +///////////////////////////////////////////////////////////////////////////////////////////////// |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <concurrentqueue.h> |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +namespace tgfx { |
| 25 | +class ReturnNode; |
| 26 | + |
| 27 | +/** |
| 28 | + * A thread-safe queue for storing ReturnNode objects that are no longer referenced. When a |
| 29 | + * ReturnNode's reference count drops to zero, it is added to this queue for later processing. |
| 30 | + * Any returned nodes that are not processed will be deleted when the ReturnQueue is destroyed. |
| 31 | + */ |
| 32 | +class ReturnQueue { |
| 33 | + public: |
| 34 | + /** |
| 35 | + * Creates a new ReturnQueue instance. |
| 36 | + */ |
| 37 | + static std::shared_ptr<ReturnQueue> Make(); |
| 38 | + |
| 39 | + ~ReturnQueue(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Wraps a ReturnNode in a shared_ptr that will add the node to this queue when its reference |
| 43 | + * count reaches zero. |
| 44 | + */ |
| 45 | + std::shared_ptr<ReturnNode> makeShared(ReturnNode* node); |
| 46 | + |
| 47 | + /** |
| 48 | + * Attempts to dequeue a ReturnNode from the queue. Returns nullptr if the queue is empty. |
| 49 | + */ |
| 50 | + ReturnNode* dequeue() { |
| 51 | + ReturnNode* node = nullptr; |
| 52 | + return queue.try_dequeue(node) ? node : nullptr; |
| 53 | + } |
| 54 | + |
| 55 | + private: |
| 56 | + std::weak_ptr<ReturnQueue> weakThis; |
| 57 | + moodycamel::ConcurrentQueue<ReturnNode*> queue; |
| 58 | + |
| 59 | + static void NotifyReferenceReachedZero(ReturnNode* node); |
| 60 | + |
| 61 | + ReturnQueue() = default; |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * A base class for objects that can be managed by a ReturnQueue. When the reference count of a |
| 66 | + * ReturnNode reaches zero, it is added to its associated ReturnQueue. |
| 67 | + */ |
| 68 | +class ReturnNode { |
| 69 | + public: |
| 70 | + virtual ~ReturnNode() = default; |
| 71 | + |
| 72 | + private: |
| 73 | + std::shared_ptr<ReturnQueue> unreferencedQueue = nullptr; |
| 74 | + |
| 75 | + friend class ReturnQueue; |
| 76 | +}; |
| 77 | +} // namespace tgfx |
0 commit comments