| 72 | |
| 73 | template <class T, class IndexType, IndexType CAPACITY> |
| 74 | bool WriteOnlySet<T, IndexType, CAPACITY>::replace(Index idx, const Reference<T>& lineage) { |
| 75 | auto lineagePtr = reinterpret_cast<uintptr_t>(lineage.getPtr()); |
| 76 | if (lineage.isValid()) { |
| 77 | lineage->addref(); |
| 78 | } |
| 79 | ASSERT((lineagePtr % 2) == 0); // this needs to be at least 2-byte aligned |
| 80 | |
| 81 | while (true) { |
| 82 | auto ptr = _set[idx].load(); |
| 83 | if (ptr & LOCK) { |
| 84 | _set[idx].store(lineagePtr); |
| 85 | ASSERT(freeList.push(reinterpret_cast<T*>(ptr ^ LOCK))); |
| 86 | return false; |
| 87 | } else { |
| 88 | if (_set[idx].compare_exchange_strong(ptr, lineagePtr)) { |
| 89 | if (ptr) { |
| 90 | reinterpret_cast<T*>(ptr)->delref(); |
| 91 | } |
| 92 | return ptr != 0; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | template <class T, class IndexType, IndexType CAPACITY> |
| 99 | WriteOnlySet<T, IndexType, CAPACITY>::WriteOnlySet() : _set(CAPACITY) { |