В этом файле класс boost::lockfree::detail::freelist
используется для управления хранилищем безблокировочной структуры данных (например, очереди) с использованием свободного списка а>. Метод deallocate_impl
используется для освобождения узлов путем связывания их обратно со списком свободных узлов (освобожденный узел становится новым главой списка свободных, замещая старую главу). Этот метод должен быть потокобезопасным и не блокировать. Оригинальный исходный код одного экземпляра дублируется здесь с моими комментариями, аннотированными, чтобы указать на подозрительный код (потенциальную ошибку?):
void deallocate_impl (index_t index)
{
freelist_node * new_pool_node =
reinterpret_cast<freelist_node*>(NodeStorage::nodes() + index);
// Shouldn't this line be placed inside the loop?
// The loop continues as long as the compare-and-exchange fails,
// which happens if the pool head has been concurrently updated.
// In that case, we MUST reload the new value of "pool_" to
// reuse its tag, link the new free-list head to it and
// compare against in the compare_and_exchange call.
tagged_index old_pool = pool_.load(memory_order_consume);
for(;;) {
// The old pool head tag is reused here without causing
// any ABA problems. However, if "index" is the same as
// old_pool.get_index(), a self-reference is written.
tagged_index new_pool (index, old_pool.get_tag());
new_pool_node->next.set_index(old_pool.get_index());
if (pool_.compare_exchange_weak(old_pool, new_pool))
return;
}
}
Я видел такую же реализацию и в Boost 1.62.0.