|
| 1 | +package net.openhft.affinity; |
| 2 | + |
| 3 | +import net.openhft.affinity.impl.NoCpuLayout; |
| 4 | +import org.jetbrains.annotations.NotNull; |
| 5 | + |
| 6 | +import java.lang.reflect.Field; |
| 7 | + |
| 8 | +public class NonForkingAffinityLock extends AffinityLock implements ThreadLifecycleListener { |
| 9 | + |
| 10 | + private static final Field GROUP_FIELD = makeThreadFieldModifiable("group"); |
| 11 | + |
| 12 | + private static final Field TARGET_FIELD = makeThreadFieldModifiable("target"); |
| 13 | + |
| 14 | + private static final LockInventory LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS)) { |
| 15 | + @Override |
| 16 | + protected AffinityLock newLock(int cpuId, boolean base, boolean reservable) { |
| 17 | + return new NonForkingAffinityLock(cpuId, base, reservable, this); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + /** |
| 22 | + * Assign any free cpu to this thread. |
| 23 | + * |
| 24 | + * @return A handle for the current AffinityLock. |
| 25 | + */ |
| 26 | + public static AffinityLock acquireLock() { |
| 27 | + return acquireLock(true); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Assign any free core to this thread. |
| 32 | + * <p></p> |
| 33 | + * In reality, only one cpu is assigned, the rest of the threads for that core are reservable so they are not used. |
| 34 | + * |
| 35 | + * @return A handle for the current AffinityLock. |
| 36 | + */ |
| 37 | + public static AffinityLock acquireCore() { |
| 38 | + return acquireCore(true); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Assign a cpu which can be bound to the current thread or another thread. |
| 43 | + * <p></p> |
| 44 | + * This can be used for defining your thread layout centrally and passing the handle via dependency injection. |
| 45 | + * |
| 46 | + * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later. |
| 47 | + * @return A handle for an affinity lock. |
| 48 | + */ |
| 49 | + public static AffinityLock acquireLock(boolean bind) { |
| 50 | + return acquireLock(bind, -1, AffinityStrategies.ANY); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Assign a core(and all its cpus) which can be bound to the current thread or another thread. |
| 55 | + * <p></p> |
| 56 | + * This can be used for defining your thread layout centrally and passing the handle via dependency injection. |
| 57 | + * |
| 58 | + * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later. |
| 59 | + * @return A handle for an affinity lock. |
| 60 | + */ |
| 61 | + public static AffinityLock acquireCore(boolean bind) { |
| 62 | + return acquireCore(bind, -1, AffinityStrategies.ANY); |
| 63 | + } |
| 64 | + |
| 65 | + private static AffinityLock acquireLock(boolean bind, int cpuId, @NotNull AffinityStrategy... strategies) { |
| 66 | + return LOCK_INVENTORY.acquireLock(bind, cpuId, strategies); |
| 67 | + } |
| 68 | + |
| 69 | + private static AffinityLock acquireCore(boolean bind, int cpuId, @NotNull AffinityStrategy... strategies) { |
| 70 | + return LOCK_INVENTORY.acquireCore(bind, cpuId, strategies); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Set the CPU layout for this machine. CPUs which are not mentioned will be ignored. |
| 75 | + * <p></p> |
| 76 | + * Changing the layout will have no impact on thread which have already been assigned. |
| 77 | + * It only affects subsequent assignments. |
| 78 | + * |
| 79 | + * @param cpuLayout for this application to use for this machine. |
| 80 | + */ |
| 81 | + public static void cpuLayout(@NotNull CpuLayout cpuLayout) { |
| 82 | + LOCK_INVENTORY.set(cpuLayout); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return The current CpuLayout for the application. |
| 87 | + */ |
| 88 | + @NotNull |
| 89 | + public static CpuLayout cpuLayout() { |
| 90 | + return LOCK_INVENTORY.getCpuLayout(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return All the current locks as a String. |
| 95 | + */ |
| 96 | + @NotNull |
| 97 | + public static String dumpLocks() { |
| 98 | + return LOCK_INVENTORY.dumpLocks(); |
| 99 | + } |
| 100 | + |
| 101 | + NonForkingAffinityLock(int cpuId, boolean base, boolean reservable, LockInventory lockInventory) { |
| 102 | + super(cpuId, base, reservable, lockInventory); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void bind(boolean wholeCore) { |
| 107 | + super.bind(wholeCore); |
| 108 | + Thread thread = Thread.currentThread(); |
| 109 | + changeGroupOfThread(thread, new ThreadTrackingGroup(thread.getThreadGroup(), this)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void release() { |
| 114 | + Thread thread = Thread.currentThread(); |
| 115 | + changeGroupOfThread(thread, thread.getThreadGroup().getParent()); |
| 116 | + super.release(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void started(Thread t) { |
| 121 | + wrapRunnableOfThread(t, this); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void startFailed(Thread t) { |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void terminated(Thread t) { |
| 130 | + } |
| 131 | + |
| 132 | + private static Field makeThreadFieldModifiable(String fieldName) { |
| 133 | + try { |
| 134 | + Field field = Thread.class.getDeclaredField(fieldName); |
| 135 | + field.setAccessible(true); |
| 136 | + return field; |
| 137 | + } catch (NoSuchFieldException e) { |
| 138 | + throw new RuntimeException(Thread.class.getName() + " class doesn't have a " + fieldName + " field! Quite unexpected!"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static void changeGroupOfThread(Thread thread, ThreadGroup group) { |
| 143 | + try { |
| 144 | + GROUP_FIELD.set(thread, group); |
| 145 | + } catch (IllegalAccessException e) { |
| 146 | + throw new RuntimeException("Failed changing " + Thread.class.getName() + "'s the '" + GROUP_FIELD.getName() + "' field! Reason: " + e.getMessage()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static void wrapRunnableOfThread(Thread thread, final AffinityLock lock) { |
| 151 | + try { |
| 152 | + final Runnable originalRunnable = (Runnable) TARGET_FIELD.get(thread); |
| 153 | + TARGET_FIELD.set( |
| 154 | + thread, |
| 155 | + new Runnable() { |
| 156 | + @Override |
| 157 | + public void run() { |
| 158 | + lock.release(); |
| 159 | + originalRunnable.run(); |
| 160 | + } |
| 161 | + } |
| 162 | + ); |
| 163 | + } catch (IllegalAccessException e) { |
| 164 | + throw new RuntimeException("Failed wrapping " + Thread.class.getName() + "'s '" + TARGET_FIELD.getName() + "' field! Reason: " + e.getMessage()); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments