rpuch commented on a change in pull request #414:
URL: https://github.com/apache/ignite-3/pull/414#discussion_r738119413



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteSpinReadWriteLock.java
##########
@@ -172,101 +244,107 @@ public void readUnlock() {
     }
 
     /**
-     * Acquires write lock.
+     * Acquires write lock waiting, if needed. The thread will block until all 
other threads release both read
+     * and write locks.
      */
     @SuppressWarnings("BusyWait")
     public void writeLock() {
-        long threadId = Thread.currentThread().getId();
-
-        if (threadId == writeLockOwner) {
-            assert state == -1;
-
-            writeLockEntryCnt++;
+        if (writeLockedByCurrentThread()) {
+            incrementWriteLockCount();
 
             return;
         }
 
-        // Increment pending write locks.
-        while (true) {
-            int pendingWLocks0 = pendingWLocks;
-
-            if (compareAndSet(PENDING_WLOCKS_OFFS, pendingWLocks0, 
pendingWLocks0 + 1))
-                break;
-        }
-
         boolean interrupted = false;
 
-        while (!compareAndSet(STATE_OFFS, 0, -1)) {
-            try {
-                Thread.sleep(10);
-            }
-            catch (InterruptedException ignored) {
-                interrupted = true;
+        incrementPendingWriteLocks();
+        try {
+            while (!trySwitchStateToWriteLocked()) {
+                try {
+                    Thread.sleep(10);
+                }
+                catch (InterruptedException ignored) {
+                    interrupted = true;
+                }
             }
         }
-
-        // Decrement pending write locks.
-        while (true) {
-            int pendingWLocks0 = pendingWLocks;
-
-            assert pendingWLocks0 > 0;
-
-            if (compareAndSet(PENDING_WLOCKS_OFFS, pendingWLocks0, 
pendingWLocks0 - 1))
-                break;
+        finally {
+            decrementPendingWriteLocks();
         }
 
         if (interrupted)
             Thread.currentThread().interrupt();
 
-        assert writeLockOwner == -1;
-
-        writeLockOwner = threadId;
-        writeLockEntryCnt = 1;
+        finishWriteLockAckquire();
     }
 
-    /**
-     * Acquires write lock without sleeping between unsuccessful attempts.
-     */
-    public void writeLock0() {
-        long threadId = Thread.currentThread().getId();
-
-        if (threadId == writeLockOwner) {
-            assert state == -1;
-
-            writeLockEntryCnt++;
+    /***/
+    private void incrementWriteLockCount() {
+        assert state == WRITE_LOCKED;
 
-            return;
-        }
+        writeLockEntryCnt++;
+    }
 
-        // Increment pending write locks.
+    /***/
+    private void incrementPendingWriteLocks() {
         while (true) {
-            int pendingWLocks0 = pendingWLocks;
+            int curPendingWLocks = pendingWLocks;
 
-            if (compareAndSet(PENDING_WLOCKS_OFFS, pendingWLocks0, 
pendingWLocks0 + 1))
+            if (compareAndSet(PENDING_WLOCKS_VH, curPendingWLocks, 
curPendingWLocks + 1))
                 break;
         }
+    }
 
-        for (;;) {
-            if (compareAndSet(STATE_OFFS, 0, -1))
-                break;
-        }
+    /***/
+    private boolean trySwitchStateToWriteLocked() {
+        return compareAndSet(STATE_VH, AVAILABLE, WRITE_LOCKED);
+    }
 
-        // Decrement pending write locks.
+    /***/
+    private void decrementPendingWriteLocks() {
         while (true) {
-            int pendingWLocks0 = pendingWLocks;
+            int curPendingWLocks = pendingWLocks;
 
-            assert pendingWLocks0 > 0;
+            assert curPendingWLocks > 0;
 
-            if (compareAndSet(PENDING_WLOCKS_OFFS, pendingWLocks0, 
pendingWLocks0 - 1))
+            if (compareAndSet(PENDING_WLOCKS_VH, curPendingWLocks, 
curPendingWLocks - 1))
                 break;
         }
+    }
 
-        assert writeLockOwner == -1;
+    /***/
+    private void finishWriteLockAckquire() {

Review comment:
       Argh, this typo bugs me again :) Thanks for letting me know, fixed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to