org.sheinbergon.needle.AffinityResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of needle-core Show documentation
Show all versions of needle-core Show documentation
Feature-rich CPU affinity for the JVM - Core library
The newest version!
package org.sheinbergon.needle;
import com.google.common.annotations.VisibleForTesting;
import com.sun.jna.Platform;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.sheinbergon.needle.jna.linux.LinuxAffinityResolver;
import org.sheinbergon.needle.jna.win32.Win32AffinityResolver;
import javax.annotation.Nonnull;
@SuppressWarnings({"rawtypes"})
public abstract class AffinityResolver {
/**
* The concrete {@code AffinityResolver} instance to be used for various affinity tasks.
* Set during initial class loading.
*
* Available implementations are:
*
*
* {@link LinuxAffinityResolver} - Linux Libpthread/Libc based implementation
* {@link Win32AffinityResolver} - Windows Kernel32 based implementation
* {@link AffinityResolver.NoOp} - Stub/No-op fallback implementation
*
*/
static final AffinityResolver INSTANCE;
static {
if (Platform.isWindows()) {
INSTANCE = Win32AffinityResolver.INSTANCE;
} else if (Platform.isLinux()) {
INSTANCE = LinuxAffinityResolver.INSTANCE;
} else {
INSTANCE = AffinityResolver.NoOp.INSTANCE;
}
}
@Nonnull
protected abstract I self();
final void thread(final @Nonnull AffinityDescriptor affinity) {
thread(self(), affinity);
}
protected abstract void thread(@Nonnull I identifier, @Nonnull AffinityDescriptor affinity);
@Nonnull
final AffinityDescriptor thread() {
return thread(self());
}
@Nonnull
protected abstract AffinityDescriptor process();
@Nonnull
protected abstract AffinityDescriptor thread(@Nonnull I identifier);
@NoArgsConstructor(access = AccessLevel.PRIVATE)
static final class NoOp extends AffinityResolver