-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate pool synchronization in a class
- Loading branch information
Showing
7 changed files
with
361 additions
and
47 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/io/vertx/core/net/impl/pool/LockSynchronization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.vertx.core.net.impl.pool; | ||
|
||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class LockSynchronization<S> implements Synchronization<S> { | ||
|
||
private final Lock lock = new ReentrantLock(); | ||
private final S state; | ||
|
||
public LockSynchronization(S state) { | ||
this.state = state; | ||
} | ||
|
||
@Override | ||
public void execute(Action<S> action) { | ||
lock.lock(); | ||
Runnable post = null; | ||
try { | ||
post = action.execute(state); | ||
} finally { | ||
lock.unlock(); | ||
if (post != null) { | ||
post.run(); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/io/vertx/core/net/impl/pool/NonBlockingSynchronization1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.vertx.core.net.impl.pool; | ||
|
||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class NonBlockingSynchronization1<S> implements Synchronization<S> { | ||
|
||
private final ConcurrentLinkedDeque<Action<S>> q = new ConcurrentLinkedDeque<>(); | ||
private final AtomicInteger s = new AtomicInteger(); | ||
private final S state; | ||
|
||
public NonBlockingSynchronization1(S state) { | ||
this.state = state; | ||
} | ||
|
||
@Override | ||
public void execute(Action<S> action) { | ||
q.add(action); | ||
if (s.incrementAndGet() == 1) { | ||
while (true) { | ||
int cnt = 0; | ||
Action<S> a; | ||
while ((a = q.poll()) != null) { | ||
cnt++; | ||
Runnable post = a.execute(state); | ||
if (post != null) { | ||
post.run(); | ||
} | ||
} | ||
if (s.addAndGet(-cnt) == 0) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/io/vertx/core/net/impl/pool/NonBlockingSynchronization2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.vertx.core.net.impl.pool; | ||
|
||
import io.netty.util.internal.PlatformDependent; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class NonBlockingSynchronization2<S> implements Synchronization<S> { | ||
|
||
private final Queue<Action<S>> q = PlatformDependent.newMpscQueue(Integer.MAX_VALUE); | ||
private final AtomicInteger s = new AtomicInteger(); | ||
private final S state; | ||
|
||
public NonBlockingSynchronization2(S state) { | ||
this.state = state; | ||
} | ||
|
||
@Override | ||
public void execute(Action<S> action) { | ||
q.add(action); | ||
while (true) { | ||
int v = s.get(); | ||
if (v == 0) { | ||
if (s.compareAndSet(0, 1)) { | ||
Action<S> a; | ||
while ((a = q.poll()) != null) { | ||
Runnable post = a.execute(state); | ||
if (post != null) { | ||
post.run(); | ||
} | ||
} | ||
s.set(0); | ||
} | ||
if (q.size() == 0) { | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/io/vertx/core/net/impl/pool/Synchronization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.vertx.core.net.impl.pool; | ||
|
||
public interface Synchronization<S> { | ||
|
||
interface Action<S> { | ||
Runnable execute(S state); | ||
} | ||
|
||
void execute(Action<S> action); | ||
|
||
} |
Oops, something went wrong.