private fun scanAndLock(key: Any, hash: Int) { var first: HashEntry<K?, V?> = entryForHash(this, hash) var e: HashEntry<K?, V?> = first var retries = -1 while (!tryLock()) { var f: HashEntry<K?, V?> if (retries < 0) { if (e == null || key == e.key) retries = 0 else e = e.next } else if (++retries > MAX_SCAN_RETRIES) { lock() break } else if (retries and 1 == 0 && entryForHash(this, hash).also { f = it } !== first) { first = f e = first retries = -1 } } }
Scans for a node containing the given key while trying to acquire lock for a remove or replace operation . Upon return , guarantees that lock is held . Note that we must lock even if the key is not found , to ensure sequential consistency of updates .