-
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.
Duplicating a duplicated context is supported but the semantic of the…
… actual locals is not defined. This update the duplicated context duplication by doing a copy of each local in the duplicated duplicate. This introduce a duplicator for each local that is responsible for copying the object when it is not null.
- Loading branch information
Showing
8 changed files
with
105 additions
and
19 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -12,18 +12,31 @@ | |
|
||
import io.vertx.core.spi.context.storage.ContextLocal; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.UnaryOperator; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class ContextLocalImpl<T> implements ContextLocal<T> { | ||
|
||
final int index; | ||
public static <T> ContextLocal<T> registerLocal(Class<T> type) { | ||
return registerLocal(type, (UnaryOperator<T>) ContextLocalImpl.IDENTITY); | ||
} | ||
|
||
public ContextLocalImpl(int index) { | ||
this.index = index; | ||
public static <T> ContextLocal<T> registerLocal(Class<T> type, Function<T, ? extends T> duplicator) { | ||
return LocalSeq.add(idx -> new ContextLocalImpl<>(idx, type, duplicator)); | ||
} | ||
|
||
public ContextLocalImpl() { | ||
this.index = LocalSeq.next(); | ||
public static final UnaryOperator<?> IDENTITY = UnaryOperator.identity(); | ||
|
||
final int index; | ||
final Class<T> type; | ||
final Function<T, ? extends T> duplicator; | ||
|
||
ContextLocalImpl(int index, Class<T> type, Function<T, ? extends T> duplicator) { | ||
this.index = index; | ||
this.type = type; | ||
this.duplicator = duplicator; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -10,28 +10,46 @@ | |
*/ | ||
package io.vertx.core.impl; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
import io.vertx.core.spi.context.storage.ContextLocal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.IntFunction; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
class LocalSeq { | ||
|
||
// 0 : reserved slot for local context map | ||
private static final AtomicInteger seq = new AtomicInteger(1); | ||
private static final List<ContextLocal<?>> locals = new ArrayList<>(); | ||
|
||
static { | ||
reset(); | ||
} | ||
|
||
/** | ||
* Hook for testing purposes | ||
*/ | ||
static void reset() { | ||
seq.set((1)); | ||
synchronized (locals) { | ||
locals.clear(); | ||
locals.add(ContextInternal.LOCAL_MAP); | ||
} | ||
} | ||
|
||
static int get() { | ||
return seq.get(); | ||
static ContextLocal<?>[] get() { | ||
synchronized (locals) { | ||
return locals.toArray(new ContextLocal[0]); | ||
} | ||
} | ||
|
||
static int next() { | ||
return seq.getAndIncrement(); | ||
static <T> ContextLocal<T> add(IntFunction<ContextLocal<T>> provider) { | ||
synchronized (locals) { | ||
int idx = locals.size(); | ||
ContextLocal<T> local = provider.apply(idx); | ||
locals.add(local); | ||
return local; | ||
} | ||
} | ||
} |
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
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
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