John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_POOL |
| 9 | #define SKSL_POOL |
| 10 | |
| 11 | #include <memory> |
| 12 | |
| 13 | namespace SkSL { |
| 14 | |
| 15 | class IRNode; |
| 16 | struct PoolData; |
| 17 | |
| 18 | class Pool { |
| 19 | public: |
| 20 | ~Pool(); |
| 21 | |
| 22 | // Creates a pool to store newly-created IRNodes during program creation and attaches it to the |
| 23 | // current thread. When your program is complete, call pool->detachFromThread() to transfer |
| 24 | // ownership of those nodes. Before destroying any of the program's nodes, reattach the pool via |
| 25 | // pool->attachToThread(). It is an error to call CreatePoolOnThread if a pool is already |
| 26 | // attached to the current thread. |
| 27 | static std::unique_ptr<Pool> CreatePoolOnThread(int nodesInPool); |
| 28 | |
| 29 | // Once a pool has been created and the ephemeral work has completed, detach it from its thread. |
| 30 | // It is an error to call this while no pool is attached. |
| 31 | void detachFromThread(); |
| 32 | |
| 33 | // Reattaches a pool to the current thread. It is an error to call this while a pool is already |
| 34 | // attached. |
| 35 | void attachToThread(); |
| 36 | |
| 37 | // Retrieves a node from the thread pool. If the pool is exhausted, this will allocate a node. |
| 38 | static void* AllocIRNode(); |
| 39 | |
| 40 | // Releases a node that was created by AllocIRNode. This will return it to the pool, or free it, |
| 41 | // as appropriate. Make sure to free all nodes, since some of them may be real allocations. |
| 42 | static void FreeIRNode(void* node_v); |
| 43 | |
| 44 | private: |
| 45 | Pool() = default; // use CreatePoolOnThread to make a pool |
| 46 | PoolData* fData = nullptr; |
| 47 | }; |
| 48 | |
| 49 | } // namespace SkSL |
| 50 | |
| 51 | #endif |