hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [3/3] httpcomponents-core git commit: I/O interest flag in IOSessionImpl requires synchronization instead of atomic operation in order to avoid race condition
Date Thu, 22 Jun 2017 10:03:00 GMT
I/O interest flag in IOSessionImpl requires synchronization instead of atomic operation in
order to avoid race condition


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/79018db8
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/79018db8
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/79018db8

Branch: refs/heads/master
Commit: 79018db83b12d3ae8b6b962f2dfff581fb6488e2
Parents: 274ad6e
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Thu Jun 22 10:29:18 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Thu Jun 22 11:59:16 2017 +0200

----------------------------------------------------------------------
 .../apache/hc/core5/reactor/IOSessionImpl.java  | 30 +++++---------------
 1 file changed, 7 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/79018db8/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
index 08abfb2..6b4b58c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
@@ -48,7 +48,6 @@ class IOSessionImpl implements IOSession {
     private final SocketChannel channel;
     private final String id;
     private final AtomicInteger status;
-    private final AtomicInteger eventMask;
     private final Deque<Command> commandQueue;
 
     private volatile IOEventHandler eventHandler;
@@ -68,7 +67,6 @@ class IOSessionImpl implements IOSession {
         this.channel = Args.notNull(socketChannel, "Socket channel");
         this.commandQueue = new ConcurrentLinkedDeque<>();
         this.socketTimeout = 0;
-        this.eventMask = new AtomicInteger(key.interestOps());
         this.id = String.format("i/o-%08X", COUNT.getAndIncrement());
         this.status = new AtomicInteger(ACTIVE);
     }
@@ -130,11 +128,7 @@ class IOSessionImpl implements IOSession {
         if (this.status.get() == CLOSED) {
             return;
         }
-        final int currentValue = this.eventMask.get();
-        if (newValue == currentValue) {
-            return;
-        }
-        if (this.eventMask.compareAndSet(currentValue, newValue)) {
+        synchronized (this.key) {
             this.key.interestOps(newValue);
             this.key.selector().wakeup();
         }
@@ -145,14 +139,9 @@ class IOSessionImpl implements IOSession {
         if (this.status.get() == CLOSED) {
             return;
         }
-        for (;;) {
-            final int currentValue = this.eventMask.get();
-            final int newValue = currentValue | op;
-            if (this.eventMask.compareAndSet(currentValue, newValue)) {
-                this.key.interestOps(newValue);
-                this.key.selector().wakeup();
-                return;
-            }
+        synchronized (this.key) {
+            this.key.interestOps(this.key.interestOps() | op);
+            this.key.selector().wakeup();
         }
     }
 
@@ -161,14 +150,9 @@ class IOSessionImpl implements IOSession {
         if (this.status.get() == CLOSED) {
             return;
         }
-        for (;;) {
-            final int currentValue = this.eventMask.get();
-            final int newValue = currentValue & ~op;
-            if (this.eventMask.compareAndSet(currentValue, newValue)) {
-                this.key.interestOps(newValue);
-                this.key.selector().wakeup();
-                return;
-            }
+        synchronized (this.key) {
+            this.key.interestOps(this.key.interestOps() & ~op);
+            this.key.selector().wakeup();
         }
     }
 


Mime
View raw message