All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.qbicc.plugin.threadlocal.ThreadLocals Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy