|
ADS
Algorithms and Data Structures
|
A pool of size classes shared by threads, beyond the book: an object made by one thread may be freed by any other, e.g., a message created by the thread that changes a model and released by the thread of a solver that has read it. More...
#include "ADS/Contract.h"#include "ADS/memory/PageHeap.h"#include "ADS/memory/PoolAllocator.h"#include <atomic>#include <cstddef>#include <cstdint>#include <new>#include <thread>#include <vector>#include "ADS/memory/impl/ConcurrentPool.tpp"Go to the source code of this file.
Classes | |
| class | ADS::ConcurrentPoolResource< M > |
| pools of the size classes of 8 bytes up to \( M \), a heap per thread, the objects freed by any thread More... | |
Namespaces | |
| namespace | ADS |
A pool of size classes shared by threads, beyond the book: an object made by one thread may be freed by any other, e.g., a message created by the thread that changes a model and released by the thread of a solver that has read it.
It is a resource of PoolAllocator [see PoolAllocator.h], with the classes of PoolResource: the multiples of 8 bytes up to \( M \), a request of \( b \) bytes aligned to at most 8 taking a slot of \( 8 \lceil b / 8 \rceil \) bytes, one aligned to 16 a slot of \( 16 \lceil b / 16 \rceil \) bytes, and any larger or more aligned one going to operator new.
Each thread that uses the resource has a heap of its own, found by a cache in its thread-local storage (one comparison with the identity of the resource) and, the first time, on a list of the heaps that grows by a compare-and-swap and is read with no lock. A heap is a PageHeap [see PageHeap.h]: pages of \( 2^{16} \) bytes of one class each, cut from segments of \( 2^{21} \) bytes, with a free list per page and a header that names the heap owning the page. The free of a pointer \( p \) finds its page as \( p \wedge \neg ( 2^{16} - 1 ) \), i.e., by masking the low bits of \( p \), and then:
Thus an allocation costs the pop of the list of the current page or the bump of its pointer, or, when the page is exhausted, one exchange that takes all the returns at once; a free costs a push on the list of a page, or a push on a batch and a compare-and-swap per batch, i.e., per up to 32 frees of a producer and a consumer that work in pairs. The lists of returns of the classes sit on separate lines of cache, so that the threads that free into different classes do not share a line. A slot waits in a batch for at most 31 other frees, so that each thread holds at most 31 slots per class freed and not yet back. A heap is never destroyed before the resource; a thread that ends leaves its heap, whose returns keep coming, to the next thread that has the same identity, and its memory goes back to the system with the resource.
global() is a resource that lives as long as the program, the default one of PoolAllocator< T , ConcurrentPoolResource<> >.
The contracts [see Contract.h]: a pointer freed was given by the resource and is freed once, with the size and the alignment of its allocation; the resource outlives its objects and its allocators, and heaps() and bytes() are read while no other thread uses it. The operations are defined in ConcurrentPool.tpp.