Skip to content

Commit

Permalink
clean up TL a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
DaMatrix committed Feb 27, 2021
1 parent c9f385e commit c836109
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand All @@ -21,6 +21,7 @@
package net.daporkchop.lib.common.misc.threadfactory;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

Expand All @@ -35,15 +36,16 @@
@Getter
@Accessors(fluent = true)
public abstract class AbstractThreadFactory implements ThreadFactory {
protected final ThreadFactory delegate;
protected final ClassLoader contextClassLoader;
protected final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
protected final int priority;
protected final boolean daemon;

@Override
public Thread newThread(Runnable task) {
public Thread newThread(@NonNull Runnable task) {
Runnable wrappedTask = this.wrapTask(task);
Thread thread = PThreadFactories.DEFAULT_THREAD_FACTORY.newThread(wrappedTask);
Thread thread = this.delegate.newThread(wrappedTask);

String name = this.getName(task, wrappedTask, thread);
if (name != null) {
Expand All @@ -62,9 +64,9 @@ public Thread newThread(Runnable task) {
return thread;
}

protected Runnable wrapTask(Runnable task) {
protected Runnable wrapTask(@NonNull Runnable task) {
return task;
}

protected abstract String getName(Runnable task, Runnable wrappedTask, Thread thread);
protected abstract String getName(@NonNull Runnable task, @NonNull Runnable wrappedTask, @NonNull Thread thread);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand Down Expand Up @@ -36,28 +36,29 @@
* @author DaPorkchop_
*/
@Accessors(fluent = true)
public final class CollapsingIncrementingNamedThreadFactory extends AbstractThreadFactory {
public class CollapsingIncrementingNamedThreadFactory extends AbstractThreadFactory {
@Getter
protected final String format;
protected final BitSet usedIds = new BitSet();

public CollapsingIncrementingNamedThreadFactory(@NonNull String format, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(contextClassLoader, uncaughtExceptionHandler, priority, daemon);
public CollapsingIncrementingNamedThreadFactory(@NonNull String format, ThreadFactory delegate, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(delegate, contextClassLoader, uncaughtExceptionHandler, priority, daemon);

this.format = format;
}

@Override
protected Runnable wrapTask(Runnable task) {
protected Runnable wrapTask(@NonNull Runnable task) {
return new TaskWrapper(task);
}

@Override
protected String getName(Runnable task, Runnable wrappedTask, Thread thread) {
protected String getName(@NonNull Runnable task, @NonNull Runnable wrappedTask, @NonNull Thread thread) {
return String.format(this.format, ((TaskWrapper) wrappedTask).id);
}

private final class TaskWrapper implements Runnable {
@NonNull
protected final Runnable delegate;
protected final int id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand Down Expand Up @@ -33,17 +33,17 @@
*/
@Getter
@Accessors(fluent = true)
public final class FixedNamedThreadFactory extends AbstractThreadFactory {
public class FixedNamedThreadFactory extends AbstractThreadFactory {
protected final String name;

public FixedNamedThreadFactory(@NonNull String name, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(contextClassLoader, uncaughtExceptionHandler, priority, daemon);
public FixedNamedThreadFactory(@NonNull String name, ThreadFactory delegate, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(delegate, contextClassLoader, uncaughtExceptionHandler, priority, daemon);

this.name = name;
}

@Override
protected String getName(Runnable task, Runnable wrappedTask, Thread thread) {
protected String getName(@NonNull Runnable task, @NonNull Runnable wrappedTask, @NonNull Thread thread) {
return this.name;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand Down Expand Up @@ -33,19 +33,19 @@
* @author DaPorkchop_
*/
@Accessors(fluent = true)
public final class IncrementingNamedThreadFactory extends AbstractThreadFactory {
public class IncrementingNamedThreadFactory extends AbstractThreadFactory {
@Getter
protected final String format;
protected final AtomicInteger counter = new AtomicInteger();

public IncrementingNamedThreadFactory(@NonNull String format, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(contextClassLoader, uncaughtExceptionHandler, priority, daemon);
public IncrementingNamedThreadFactory(@NonNull String format, ThreadFactory delegate, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(delegate, contextClassLoader, uncaughtExceptionHandler, priority, daemon);

this.format = format;
}

@Override
protected String getName(Runnable task, Runnable wrappedTask, Thread thread) {
protected String getName(@NonNull Runnable task, @NonNull Runnable wrappedTask, @NonNull Thread thread) {
return String.format(this.format, this.counter.getAndIncrement());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand All @@ -21,6 +21,7 @@
package net.daporkchop.lib.common.misc.threadfactory;

import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.experimental.Accessors;

Expand All @@ -34,12 +35,14 @@
* @author DaPorkchop_
* @see PThreadFactories#builder()
*/
@NoArgsConstructor(onConstructor_ = {@Deprecated})
@NoArgsConstructor(onConstructor_ = { @Deprecated })
@Setter
@Accessors(fluent = true, chain = true)
public class ThreadFactoryBuilder {
protected String name;

@NonNull
protected ThreadFactory delegate = PThreadFactories.DEFAULT_THREAD_FACTORY;
protected ClassLoader contextClassLoader;
protected Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
protected int priority = Thread.NORM_PRIORITY;
Expand Down Expand Up @@ -105,19 +108,19 @@ public ThreadFactory build() {
if (!this.formatId) {
if (this.name == null) {
if (this.contextClassLoader == null && this.uncaughtExceptionHandler == null && this.priority == Thread.NORM_PRIORITY && !this.daemon) {
return PThreadFactories.DEFAULT_THREAD_FACTORY;
return this.delegate;
} else {
return new UnnamedThreadFactory(this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
return new UnnamedThreadFactory(this.delegate, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
}
} else {
return new FixedNamedThreadFactory(this.name, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
return new FixedNamedThreadFactory(this.name, this.delegate, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
}
} else if (this.name == null) {
throw new IllegalStateException("formatId is set, but no name is given!");
} else if (this.collapsingId) {
return new CollapsingIncrementingNamedThreadFactory(this.name, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
return new CollapsingIncrementingNamedThreadFactory(this.name, this.delegate, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
} else {
return new IncrementingNamedThreadFactory(this.name, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
return new IncrementingNamedThreadFactory(this.name, this.delegate, this.contextClassLoader, this.uncaughtExceptionHandler, this.priority, this.daemon);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand All @@ -20,20 +20,22 @@

package net.daporkchop.lib.common.misc.threadfactory;

import lombok.NonNull;

import java.util.concurrent.ThreadFactory;

/**
* A {@link ThreadFactory} that does not apply any names to created threads.
*
* @author DaPorkchop_
*/
public final class UnnamedThreadFactory extends AbstractThreadFactory {
public UnnamedThreadFactory(ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(contextClassLoader, uncaughtExceptionHandler, priority, daemon);
public class UnnamedThreadFactory extends AbstractThreadFactory {
public UnnamedThreadFactory(ThreadFactory delegate, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler, int priority, boolean daemon) {
super(delegate, contextClassLoader, uncaughtExceptionHandler, priority, daemon);
}

@Override
protected String getName(Runnable task, Runnable wrappedTask, Thread thread) {
protected String getName(@NonNull Runnable task, @NonNull Runnable wrappedTask, @NonNull Thread thread) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand All @@ -20,7 +20,10 @@

package net.daporkchop.lib.common.misc.threadlocal;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.util.function.Supplier;

Expand All @@ -29,12 +32,32 @@
*
* @author DaPorkchop_
*/
@RequiredArgsConstructor
final class JavaTL<T> extends ThreadLocal<T> implements TL<T> {
protected final Supplier<T> initialSupplier;
public class JavaTL<T> extends ThreadLocal<T> implements TL<T> {
/**
* Extension of {@link JavaTL} with a fixed default initialization value.
*
* @author DaPorkchop_
*/
@RequiredArgsConstructor
@Getter
@Accessors(fluent = true)
public static class WithConstant<T> extends JavaTL<T> {
protected final T initialValue;
}

/**
* Extension of {@link JavaTL} which computes the default initialization value on-demand.
*
* @author DaPorkchop_
*/
@RequiredArgsConstructor
public static class WithInitializer<T> extends JavaTL<T> {
@NonNull
protected final Supplier<T> initialSupplier;

@Override
protected T initialValue() {
return this.initialSupplier != null ? this.initialSupplier.get() : null;
@Override
protected T initialValue() {
return this.initialSupplier.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
* Copyright (c) 2018-2021 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
Expand All @@ -21,7 +21,10 @@
package net.daporkchop.lib.common.misc.threadlocal;

import io.netty.util.concurrent.FastThreadLocal;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.util.function.Supplier;

Expand All @@ -31,11 +34,32 @@
* @author DaPorkchop_
*/
@RequiredArgsConstructor
final class FastTL<T> extends FastThreadLocal<T> implements TL<T> {
protected final Supplier<T> initialSupplier;
public class NettyFastTL<T> extends FastThreadLocal<T> implements TL<T> {
/**
* Extension of {@link NettyFastTL} with a fixed default initialization value.
*
* @author DaPorkchop_
*/
@RequiredArgsConstructor
@Getter
@Accessors(fluent = true)
public static class WithConstant<T> extends NettyFastTL<T> {
protected final T initialValue;
}

/**
* Extension of {@link NettyFastTL} which computes the default initialization value on-demand.
*
* @author DaPorkchop_
*/
@RequiredArgsConstructor
public static class WithInitializer<T> extends NettyFastTL<T> {
@NonNull
protected final Supplier<T> initialSupplier;

@Override
protected T initialValue() {
return this.initialSupplier != null ? this.initialSupplier.get() : null;
@Override
protected T initialValue() {
return this.initialSupplier.get();
}
}
}
Loading

0 comments on commit c836109

Please sign in to comment.