fix: data race in PagedArray

(cherry picked from commit 70bfd5d065)
This commit is contained in:
Stuart Carnie 2023-12-22 11:00:43 +11:00 committed by Rémi Verschelde
parent 04c312fbc9
commit 486998bee5
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 12 additions and 9 deletions

View File

@ -53,7 +53,12 @@ class PagedArrayPool {
SpinLock spin_lock; SpinLock spin_lock;
public: public:
uint32_t alloc_page() { struct PageInfo {
T *page = nullptr;
uint32_t page_id = 0;
};
PageInfo alloc_page() {
spin_lock.lock(); spin_lock.lock();
if (unlikely(pages_available == 0)) { if (unlikely(pages_available == 0)) {
uint32_t pages_used = pages_allocated; uint32_t pages_used = pages_allocated;
@ -69,13 +74,11 @@ public:
} }
pages_available--; pages_available--;
uint32_t page = available_page_pool[pages_available]; uint32_t page_id = available_page_pool[pages_available];
T *page = page_pool[page_id];
spin_lock.unlock(); spin_lock.unlock();
return page; return PageInfo{ page, page_id };
}
T *get_page(uint32_t p_page_id) {
return page_pool[p_page_id];
} }
void free_page(uint32_t p_page_id) { void free_page(uint32_t p_page_id) {
@ -190,9 +193,9 @@ public:
_grow_page_array(); //keep out of inline _grow_page_array(); //keep out of inline
} }
uint32_t page_id = page_pool->alloc_page(); typename PagedArrayPool<T>::PageInfo page_info = page_pool->alloc_page();
page_data[page_count] = page_pool->get_page(page_id); page_data[page_count] = page_info.page;
page_ids[page_count] = page_id; page_ids[page_count] = page_info.page_id;
} }
// place the new value // place the new value