Run clang-format
[junction.git] / junction / ConcurrentMap_Linear.h
index 1de64b24b7e4301bada738ce353a2ca6b05cda43..6d0fa986abf281166a1de3541d429bcbe7062fcd 100644 (file)
@@ -85,7 +85,7 @@ public:
                     return;
                 m_value = m_cell->value.load(turf::Consume);
                 if (m_value != Value(ValueTraits::Redirect))
-                    return;                        // Found an existing value
+                    return; // Found an existing value
                 // We've encountered a Redirect value. Help finish the migration.
                 TURF_TRACE(ConcurrentMap_Linear, 1, "[Mutator] find was redirected", uptr(m_table), uptr(0));
                 m_table->jobCoordinator.participate();
@@ -94,30 +94,31 @@ public:
         }
 
         // Constructor: Insert cell
-        Mutator(ConcurrentMap_Linear& map, Key key) : m_map(map), m_table(map.m_root.load(turf::Consume)), m_value(Value(ValueTraits::NullValue)) {
+        Mutator(ConcurrentMap_Linear& map, Key key)
+            : m_map(map), m_table(map.m_root.load(turf::Consume)), m_value(Value(ValueTraits::NullValue)) {
             TURF_TRACE(ConcurrentMap_Linear, 2, "[Mutator] insert constructor called", uptr(m_table), uptr(key));
             Hash hash = KeyTraits::hash(key);
             for (;;) {
                 m_table = m_map.m_root.load(turf::Consume);
-                switch (Details::insert(hash, m_table, m_cell)) {   // Modifies m_cell
-                    case Details::InsertResult_InsertedNew: {
-                        // We've inserted a new cell. Don't load m_cell->value.
-                        return;     
-                    }
-                    case Details::InsertResult_AlreadyFound: {
-                        // The hash was already found in the table.
-                        m_value = m_cell->value.load(turf::Consume);
-                        if (m_value == Value(ValueTraits::Redirect)) {
-                            // We've encountered a Redirect value.
-                            TURF_TRACE(ConcurrentMap_Linear, 3, "[Mutator] insert was redirected", uptr(m_table), uptr(m_value));
-                            break;   // Help finish the migration.
-                        }
-                        return;     // Found an existing value
-                    }
-                    case Details::InsertResult_Overflow: {
-                        Details::beginTableMigration(m_map, m_table);
-                        break;
+                switch (Details::insert(hash, m_table, m_cell)) { // Modifies m_cell
+                case Details::InsertResult_InsertedNew: {
+                    // We've inserted a new cell. Don't load m_cell->value.
+                    return;
+                }
+                case Details::InsertResult_AlreadyFound: {
+                    // The hash was already found in the table.
+                    m_value = m_cell->value.load(turf::Consume);
+                    if (m_value == Value(ValueTraits::Redirect)) {
+                        // We've encountered a Redirect value.
+                        TURF_TRACE(ConcurrentMap_Linear, 3, "[Mutator] insert was redirected", uptr(m_table), uptr(m_value));
+                        break; // Help finish the migration.
                     }
+                    return; // Found an existing value
+                }
+                case Details::InsertResult_Overflow: {
+                    Details::beginTableMigration(m_map, m_table);
+                    break;
+                }
                 }
                 // A migration has been started (either by us, or another thread). Participate until it's complete.
                 m_table->jobCoordinator.participate();
@@ -133,7 +134,7 @@ public:
 
         Value exchangeValue(Value desired) {
             TURF_ASSERT(desired != Value(ValueTraits::NullValue));
-            TURF_ASSERT(m_cell);    // Cell must have been found or inserted
+            TURF_ASSERT(m_cell); // Cell must have been found or inserted
             TURF_TRACE(ConcurrentMap_Linear, 4, "[Mutator::exchangeValue] called", uptr(m_table), uptr(m_value));
             for (;;) {
                 Value oldValue = m_value;
@@ -143,15 +144,18 @@ public:
                     // Decrement remainingValues to ensure we have permission to (re)insert a value.
                     prevRemainingValues = m_table->valuesRemaining.fetchSub(1, turf::Relaxed);
                     if (prevRemainingValues <= 0) {
-                        TURF_TRACE(ConcurrentMap_Linear, 5, "[Mutator::exchangeValue] ran out of valuesRemaining", uptr(m_table), prevRemainingValues);
+                        TURF_TRACE(ConcurrentMap_Linear, 5, "[Mutator::exchangeValue] ran out of valuesRemaining", uptr(m_table),
+                                   prevRemainingValues);
                         // Can't (re)insert any more values.
                         // There are two ways this can happen. One with a TableMigration already in progress, and one without:
                         // 1. A TableMigration puts a cap on the number of values late-arriving threads are allowed to insert.
                         // 2. Two threads race to insert the same key, and it's the last free cell in the table.
-                        // (Note: We could get tid of the valuesRemaining counter by handling the possibility of migration failure,
+                        // (Note: We could get tid of the valuesRemaining counter by handling the possibility of migration
+                        // failure,
                         // as LeapFrog and Grampa do...)
-                        m_table->valuesRemaining.fetchAdd(1, turf::Relaxed);    // Undo valuesRemaining decrement
-                        // Since we don't know whether there's already a TableMigration in progress, always attempt to start one here:
+                        m_table->valuesRemaining.fetchAdd(1, turf::Relaxed); // Undo valuesRemaining decrement
+                        // Since we don't know whether there's already a TableMigration in progress, always attempt to start one
+                        // here:
                         Details::beginTableMigration(m_map, m_table);
                         goto helpMigrate;
                     }
@@ -160,15 +164,17 @@ public:
                     // Exchange was successful. Return previous value.
                     TURF_TRACE(ConcurrentMap_Linear, 6, "[Mutator::exchangeValue] exchanged Value", uptr(m_value), uptr(desired));
                     Value result = m_value;
-                    m_value = desired;  // Leave the mutator in a valid state
+                    m_value = desired; // Leave the mutator in a valid state
                     return result;
                 }
                 // The CAS failed and m_value has been updated with the latest value.
                 if (m_value != Value(ValueTraits::Redirect)) {
-                    TURF_TRACE(ConcurrentMap_Linear, 7, "[Mutator::exchangeValue] detected race to write value", uptr(m_table), uptr(m_value));
+                    TURF_TRACE(ConcurrentMap_Linear, 7, "[Mutator::exchangeValue] detected race to write value", uptr(m_table),
+                               uptr(m_value));
                     if (oldValue == Value(ValueTraits::NullValue) && m_value != Value(ValueTraits::NullValue)) {
-                        TURF_TRACE(ConcurrentMap_Linear, 8, "[Mutator::exchangeValue] racing write inserted new value", uptr(m_table), uptr(m_value));
-                        m_table->valuesRemaining.fetchAdd(1, turf::Relaxed);    // Undo valuesRemaining decrement
+                        TURF_TRACE(ConcurrentMap_Linear, 8, "[Mutator::exchangeValue] racing write inserted new value",
+                                   uptr(m_table), uptr(m_value));
+                        m_table->valuesRemaining.fetchAdd(1, turf::Relaxed); // Undo valuesRemaining decrement
                     }
                     // There was a racing write (or erase) to this cell.
                     // Pretend we exchanged with ourselves, and just let the racing write win.
@@ -185,25 +191,26 @@ public:
                     // Try again in the new table.
                     m_table = m_map.m_root.load(turf::Consume);
                     m_value = Value(ValueTraits::NullValue);
-                    switch (Details::insert(hash, m_table, m_cell)) {   // Modifies m_cell
+                    switch (Details::insert(hash, m_table, m_cell)) { // Modifies m_cell
                     case Details::InsertResult_AlreadyFound:
                         m_value = m_cell->value.load(turf::Consume);
                         if (m_value == Value(ValueTraits::Redirect)) {
-                            TURF_TRACE(ConcurrentMap_Linear, 10, "[Mutator::exchangeValue] was re-redirected", uptr(m_table), uptr(m_value));
+                            TURF_TRACE(ConcurrentMap_Linear, 10, "[Mutator::exchangeValue] was re-redirected", uptr(m_table),
+                                       uptr(m_value));
                             break;
                         }
                         goto breakOuter;
                     case Details::InsertResult_InsertedNew:
                         goto breakOuter;
                     case Details::InsertResult_Overflow:
-                        TURF_TRACE(ConcurrentMap_Linear, 11, "[Mutator::exchangeValue] overflow after redirect", uptr(m_table), 0);
+                        TURF_TRACE(ConcurrentMap_Linear, 11, "[Mutator::exchangeValue] overflow after redirect", uptr(m_table),
+                                   0);
                         Details::beginTableMigration(m_map, m_table);
                         break;
                     }
                     // We were redirected... again
                 }
-            breakOuter:
-                ;
+            breakOuter:;
                 // Try again in the new table.
             }
         }
@@ -213,30 +220,32 @@ public:
         }
 
         Value eraseValue() {
-            TURF_ASSERT(m_cell);    // Cell must have been found or inserted
+            TURF_ASSERT(m_cell); // Cell must have been found or inserted
             TURF_TRACE(ConcurrentMap_Linear, 12, "[Mutator::eraseValue] called", uptr(m_table), m_cell - m_table->getCells());
             for (;;) {
                 if (m_value == Value(ValueTraits::NullValue))
                     return Value(m_value);
-                TURF_ASSERT(m_cell);    // m_value is non-NullValue, therefore cell must have been found or inserted.
+                TURF_ASSERT(m_cell); // m_value is non-NullValue, therefore cell must have been found or inserted.
                 if (m_cell->value.compareExchangeStrong(m_value, Value(ValueTraits::NullValue), turf::Consume)) {
                     // Exchange was successful and a non-NULL value was erased and returned by reference in m_value.
-                    TURF_ASSERT(m_value != ValueTraits::NullValue);   // Implied by the test at the start of the loop.
+                    TURF_ASSERT(m_value != ValueTraits::NullValue); // Implied by the test at the start of the loop.
                     m_table->valuesRemaining.fetchAdd(1, turf::Relaxed);
                     Value result = m_value;
-                    m_value = Value(ValueTraits::NullValue);   // Leave the mutator in a valid state
+                    m_value = Value(ValueTraits::NullValue); // Leave the mutator in a valid state
                     return result;
                 }
                 // The CAS failed and m_value has been updated with the latest value.
-                TURF_TRACE(ConcurrentMap_Linear, 13, "[Mutator::eraseValue] detected race to write value", uptr(m_table), m_cell - m_table->getCells());
+                TURF_TRACE(ConcurrentMap_Linear, 13, "[Mutator::eraseValue] detected race to write value", uptr(m_table),
+                           m_cell - m_table->getCells());
                 if (m_value != Value(ValueTraits::Redirect)) {
                     // There was a racing write (or erase) to this cell.
                     // Pretend we erased nothing, and just let the racing write win.
                     return Value(ValueTraits::NullValue);
                 }
                 // We've been redirected to a new table.
-                TURF_TRACE(ConcurrentMap_Linear, 14, "[Mutator::eraseValue] was redirected", uptr(m_table), m_cell - m_table->getCells());
-                Hash hash = m_cell->hash.load(turf::Relaxed);           // Re-fetch hash
+                TURF_TRACE(ConcurrentMap_Linear, 14, "[Mutator::eraseValue] was redirected", uptr(m_table),
+                           m_cell - m_table->getCells());
+                Hash hash = m_cell->hash.load(turf::Relaxed); // Re-fetch hash
                 for (;;) {
                     // Help complete the migration.
                     m_table->jobCoordinator.participate();
@@ -250,7 +259,8 @@ public:
                     m_value = m_cell->value.load(turf::Relaxed);
                     if (m_value != Value(ValueTraits::Redirect))
                         break;
-                    TURF_TRACE(ConcurrentMap_Linear, 15, "[Mutator::eraseValue] was re-redirected", uptr(m_table), m_cell - m_table->getCells());
+                    TURF_TRACE(ConcurrentMap_Linear, 15, "[Mutator::eraseValue] was re-redirected", uptr(m_table),
+                               m_cell - m_table->getCells());
                 }
             }
         }
@@ -275,7 +285,7 @@ public:
                 return Value(ValueTraits::NullValue);
             Value value = cell->value.load(turf::Consume);
             if (value != Value(ValueTraits::Redirect))
-                return value;                        // Found an existing value
+                return value; // Found an existing value
             // We've been redirected to a new table. Help with the migration.
             TURF_TRACE(ConcurrentMap_Linear, 17, "[get] was redirected", uptr(table), uptr(cell));
             table->jobCoordinator.participate();
@@ -318,17 +328,17 @@ public:
 
         void next() {
             TURF_ASSERT(m_table);
-            TURF_ASSERT(isValid() || m_idx == -1);  // Either the Iterator is already valid, or we've just started iterating.
+            TURF_ASSERT(isValid() || m_idx == -1); // Either the Iterator is already valid, or we've just started iterating.
             while (++m_idx <= m_table->sizeMask) {
                 // Index still inside range of table.
-                typename Details::Cell *cell = m_table->getCells() + m_idx;
+                typename Details::Cellcell = m_table->getCells() + m_idx;
                 m_hash = cell->hash.load(turf::Relaxed);
                 if (m_hash != KeyTraits::NullHash) {
                     // Cell has been reserved.
                     m_value = cell->value.load(turf::Relaxed);
                     TURF_ASSERT(m_value != Value(ValueTraits::Redirect));
                     if (m_value != Value(ValueTraits::NullValue))
-                        return;     // Yield this cell.
+                        return; // Yield this cell.
                 }
             }
             // That's the end of the map.