org.qbicc.plugin.threadlocal.ThreadLocals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qbicc-plugin-thread-local Show documentation
Show all versions of qbicc-plugin-thread-local Show documentation
Support for true thread-local fields
package org.qbicc.plugin.threadlocal;
import java.util.concurrent.ConcurrentHashMap;
import org.qbicc.context.AttachmentKey;
import org.qbicc.context.CompilationContext;
import org.qbicc.type.definition.element.InstanceFieldElement;
import org.qbicc.type.definition.element.StaticFieldElement;
/**
*
*/
public final class ThreadLocals {
private static final AttachmentKey KEY = new AttachmentKey<>();
private final CompilationContext ctxt;
private final ConcurrentHashMap threadLocalFields = new ConcurrentHashMap<>();
private ThreadLocals(final CompilationContext ctxt) {
this.ctxt = ctxt;
}
public static ThreadLocals get(final CompilationContext ctxt) {
ThreadLocals nativeInfo = ctxt.getAttachment(KEY);
if (nativeInfo == null) {
ThreadLocals appearing = ctxt.putAttachmentIfAbsent(KEY, nativeInfo = new ThreadLocals(ctxt));
if (appearing != null) {
nativeInfo = appearing;
}
}
return nativeInfo;
}
public InstanceFieldElement getThreadLocalField(StaticFieldElement staticField) {
return threadLocalFields.get(staticField);
}
void registerThreadLocalField(StaticFieldElement staticField, InstanceFieldElement injectedField) {
threadLocalFields.put(staticField, injectedField);
}
public boolean isThreadLocalField(InstanceFieldElement instanceField) {
return threadLocalFields.contains(instanceField);
}
}