|
static void | lf_pinbox_real_free (LF_PINS *pins) |
|
void | lf_pinbox_init (LF_PINBOX *pinbox, uint free_ptr_offset, lf_pinbox_free_func *free_func, void *free_func_arg) |
|
void | lf_pinbox_destroy (LF_PINBOX *pinbox) |
|
LF_PINS * | lf_pinbox_get_pins (LF_PINBOX *pinbox) |
|
void | lf_pinbox_put_pins (LF_PINS *pins) |
|
static void | add_to_purgatory (LF_PINS *pins, void *addr) |
|
void | lf_pinbox_free (LF_PINS *pins, void *addr) |
|
static int | match_and_save (void *v_el, void *v_arg) |
|
static std::atomic< uchar * > & | next_node (LF_PINBOX *P, uchar *X) |
|
static void | alloc_free (void *v_first, void *v_last, void *v_allocator) |
|
void | lf_alloc_init2 (LF_ALLOCATOR *allocator, uint size, uint free_ptr_offset, lf_allocator_func *ctor, lf_allocator_func *dtor) |
| Initialize lock-free allocator. More...
|
|
void | lf_alloc_destroy (LF_ALLOCATOR *allocator) |
|
void * | lf_alloc_new (LF_PINS *pins) |
|
uint | lf_alloc_pool_count (LF_ALLOCATOR *allocator) |
|
wait-free concurrent allocator based on pinning addresses.
It works as follows: every thread (strictly speaking - every CPU, but it's too difficult to do) has a small array of pointers. They're called "pins". Before using an object its address must be stored in this array (pinned). When an object is no longer necessary its address must be removed from this array (unpinned). When a thread wants to free() an object it scans all pins of all threads to see if somebody has this object pinned. If yes - the object is not freed (but stored in a "purgatory"). To reduce the cost of a single free() pins are not scanned on every free() but only added to (thread-local) purgatory. On every LF_PURGATORY_SIZE free() purgatory is scanned and all unpinned objects are freed.
Pins are used to solve ABA problem. To use pins one must obey a pinning protocol:
- Let's assume that PTR is a shared pointer to an object. Shared means that any thread may modify it anytime to point to a different object and free the old object. Later the freed object may be potentially allocated by another thread. If we're unlucky that other thread may set PTR to point to this object again. This is ABA problem.
- Create a local pointer LOCAL_PTR.
- Pin the PTR in a loop: do { LOCAL_PTR= PTR; pin(PTR, PIN_NUMBER); } while (LOCAL_PTR != PTR)
- It is guaranteed that after the loop has ended, LOCAL_PTR points to an object (or NULL, if PTR may be NULL), that will never be freed. It is not guaranteed though that LOCAL_PTR == PTR (as PTR can change any time)
- When done working with the object, remove the pin: unpin(PIN_NUMBER)
- When copying pins (as in the list traversing loop: pin(CUR, 1); while () { do // standard { // pinning NEXT=CUR->next; // loop pin(NEXT, 0); // see #3 } while (NEXT != CUR->next); // above ... ... CUR=NEXT; pin(CUR, 1); // copy pin[0] to pin[1] } which keeps CUR address constantly pinned), note than pins may be copied only upwards (!!!), that is pin[N] to pin[M], M > N.
- Don't keep the object pinned longer than necessary - the number of pins you have is limited (and small), keeping an object pinned prevents its reuse and cause unnecessary mallocs.
Explanations:
- The loop is important. The following can occur: thread1> LOCAL_PTR= PTR thread2> free(PTR); PTR=0; thread1> pin(PTR, PIN_NUMBER); now thread1 cannot access LOCAL_PTR, even if it's pinned, because it points to a freed memory. That is, it must verify that it has indeed pinned PTR, the shared pointer.
- When a thread wants to free some LOCAL_PTR, and it scans all lists of pins to see whether it's pinned, it does it upwards, from low pin numbers to high. Thus another thread must copy an address from one pin to another in the same direction - upwards, otherwise the scanning thread may miss it.
Implementation details:
Pins are given away from a "pinbox". Pinbox is stack-based allocator. It used dynarray for storing pins, new elements are allocated by dynarray as necessary, old are pushed in the stack for reuse. ABA is solved by versioning a pointer - because we use an array, a pointer to pins is 16 bit, upper 16 bits are used for a version.