2929import java .util .BitSet ;
3030
3131/**
32- * This utility class support locking a thread to a single core, or reserving a whole core for a thread.
32+ * This utility class support locking a thread to a single core, or reserving a whole core for a
33+ * thread.
3334 *
3435 * @author peter.lawrey
3536 */
@@ -67,8 +68,8 @@ public class AffinityLock implements Closeable {
6768 */
6869 private final boolean reservable ;
6970 /**
70- * An inventory build from the CPU layout which keeps track of the various locks
71- * belonging to each CPU.
71+ * An inventory build from the CPU layout which keeps track of the various locks belonging to
72+ * each CPU.
7273 */
7374 private final LockInventory lockInventory ;
7475 boolean bound = false ;
@@ -84,10 +85,9 @@ public class AffinityLock implements Closeable {
8485 }
8586
8687 /**
87- * Set the CPU layout for this machine. CPUs which are not mentioned will be ignored.
88- * <p>
89- * Changing the layout will have no impact on thread which have already been assigned.
90- * It only affects subsequent assignments.
88+ * Set the CPU layout for this machine. CPUs which are not mentioned will be ignored. <p>
89+ * Changing the layout will have no impact on thread which have already been assigned. It only
90+ * affects subsequent assignments.
9191 *
9292 * @param cpuLayout for this application to use for this machine.
9393 */
@@ -135,9 +135,8 @@ public static AffinityLock acquireLock() {
135135 }
136136
137137 /**
138- * Assign any free core to this thread.
139- * <p>
140- * In reality, only one cpu is assigned, the rest of the threads for that core are reservable so they are not used.
138+ * Assign any free core to this thread. <p> In reality, only one cpu is assigned, the rest of
139+ * the threads for that core are reservable so they are not used.
141140 *
142141 * @return A handle for the current AffinityLock.
143142 */
@@ -146,11 +145,11 @@ public static AffinityLock acquireCore() {
146145 }
147146
148147 /**
149- * Assign a cpu which can be bound to the current thread or another thread.
150- * <p>
151- * This can be used for defining your thread layout centrally and passing the handle via dependency injection.
148+ * Assign a cpu which can be bound to the current thread or another thread. <p> This can be used
149+ * for defining your thread layout centrally and passing the handle via dependency injection.
152150 *
153- * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
151+ * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound
152+ * later.
154153 * @return A handle for an affinity lock.
155154 */
156155 public static AffinityLock acquireLock (boolean bind ) {
@@ -159,10 +158,11 @@ public static AffinityLock acquireLock(boolean bind) {
159158
160159 /**
161160 * Assign a core(and all its cpus) which can be bound to the current thread or another thread.
162- * <p>
163- * This can be used for defining your thread layout centrally and passing the handle via dependency injection.
161+ * <p> This can be used for defining your thread layout centrally and passing the handle via
162+ * dependency injection.
164163 *
165- * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
164+ * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound
165+ * later.
166166 * @return A handle for an affinity lock.
167167 */
168168 public static AffinityLock acquireCore (boolean bind ) {
@@ -192,7 +192,8 @@ private static boolean areAssertionsEnabled() {
192192 }
193193
194194 /**
195- * Assigning the current thread has a side effect of preventing the lock being used again until it is released.
195+ * Assigning the current thread has a side effect of preventing the lock being used again until
196+ * it is released.
196197 *
197198 * @param bind whether to bind the thread as well
198199 * @param wholeCore whether to reserve all the thread in the same core.
@@ -237,6 +238,10 @@ public void bind(boolean wholeCore) {
237238 }
238239
239240 final boolean canReserve () {
241+
242+ if (!LockCheck .isCpuFree (cpuId ))
243+ return false ;
244+
240245 if (!reservable ) return false ;
241246 if (assignedThread != null ) {
242247 if (assignedThread .isAlive ()) {
@@ -249,10 +254,10 @@ final boolean canReserve() {
249254 }
250255
251256 /**
252- * Give another affinity lock relative to this one based on a list of strategies.
253- * <p>
254- * The strategies are evaluated in order to (like a search path) to find the next appropriate thread.
255- * If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving the OS to choose)
257+ * Give another affinity lock relative to this one based on a list of strategies. <p> The
258+ * strategies are evaluated in order to (like a search path) to find the next appropriate
259+ * thread. If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving
260+ * the OS to choose)
256261 *
257262 * @param strategies To determine if you want the same/different core/socket.
258263 * @return A matching AffinityLock.
0 commit comments