MySQL 8.3.0
Source Code Documentation File Reference

wait-free concurrent allocator based on pinning addresses. More...

#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <atomic>
#include "lf.h"
#include "my_atomic.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "my_thread.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys/mysys_priv.h"


struct  st_match_and_save_arg


#define LF_PINBOX_MAX_PINS   65536
#define pnext_node(P, X)   (*((void **)(((char *)(X)) + (P)->free_ptr_offset)))
#define anext_node(X)   next_node(&allocator->pinbox, (X))


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_PINSlf_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)

Detailed Description

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:

  1. 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.
  2. Create a local pointer LOCAL_PTR.
  3. Pin the PTR in a loop: do { LOCAL_PTR= PTR; pin(PTR, PIN_NUMBER); } while (LOCAL_PTR != PTR)
  4. 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)
  5. When done working with the object, remove the pin: unpin(PIN_NUMBER)
  6. 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.
  7. 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.


  1. 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.
  2. 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.

Macro Definition Documentation

◆ anext_node

#define anext_node (   X)    next_node(&allocator->pinbox, (X))


#define LF_PINBOX_MAX_PINS   65536

◆ pnext_node

#define pnext_node (   P,
)    (*((void **)(((char *)(X)) + (P)->free_ptr_offset)))

Function Documentation

◆ add_to_purgatory()

static void add_to_purgatory ( LF_PINS pins,
void *  addr 

◆ alloc_free()

static void alloc_free ( void *  v_first,
void *  v_last,
void *  v_allocator 

◆ lf_alloc_destroy()

void lf_alloc_destroy ( LF_ALLOCATOR allocator)

◆ lf_alloc_init2()

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.

allocatorAllocator structure to initialize.
sizeA size of an object to allocate.
free_ptr_offsetAn offset inside the object to a sizeof(void *) memory that is guaranteed to be unused after the object is put in the purgatory. Unused by ANY thread, not only the purgatory owner. This memory will be used to link waiting-to-be-freed objects in a purgatory list.
ctorFunction to be called after object was malloc()'ed.
dtorFunction to be called before object is free()'d.

◆ lf_alloc_new()

void * lf_alloc_new ( LF_PINS pins)

◆ lf_alloc_pool_count()

uint lf_alloc_pool_count ( LF_ALLOCATOR allocator)

◆ lf_pinbox_destroy()

void lf_pinbox_destroy ( LF_PINBOX pinbox)

◆ lf_pinbox_free()

void lf_pinbox_free ( LF_PINS pins,
void *  addr 

◆ lf_pinbox_get_pins()

LF_PINS * lf_pinbox_get_pins ( LF_PINBOX pinbox)

◆ lf_pinbox_init()

void lf_pinbox_init ( LF_PINBOX pinbox,
uint  free_ptr_offset,
lf_pinbox_free_func free_func,
void *  free_func_arg 

◆ lf_pinbox_put_pins()

void lf_pinbox_put_pins ( LF_PINS pins)

◆ lf_pinbox_real_free()

static void lf_pinbox_real_free ( LF_PINS pins)

◆ match_and_save()

static int match_and_save ( void *  v_el,
void *  v_arg 

◆ next_node()

static std::atomic< uchar * > & next_node ( LF_PINBOX P,
uchar X