3434 * @author peter.lawrey
3535 */
3636public class AffinityLock {
37- private static final Logger LOGGER = LoggerFactory .getLogger (AffinityLock .class );
38-
39- // TODO It seems like on virtualized platforms .availableProcessors() value can change at
40- // TODO runtime. We should think about how to adopt to such change
41-
4237 // Static fields and methods.
4338 public static final String AFFINITY_RESERVED = "affinity.reserved" ;
4439
40+ // TODO It seems like on virtualized platforms .availableProcessors() value can change at
41+ // TODO runtime. We should think about how to adopt to such change
4542 public static final int PROCESSORS = Runtime .getRuntime ().availableProcessors ();
4643 public static final long BASE_AFFINITY = AffinitySupport .getAffinity ();
4744 public static final long RESERVED_AFFINITY = getReservedAffinity0 ();
48-
45+ private static final Logger LOGGER = LoggerFactory . getLogger ( AffinityLock . class );
4946 private static final LockInventory LOCK_INVENTORY = new LockInventory (new NoCpuLayout (PROCESSORS ));
5047
5148 static {
@@ -57,10 +54,37 @@ public class AffinityLock {
5754 LOGGER .warn ("Unable to load /proc/cpuinfo" , e );
5855 }
5956 }
57+ /**
58+ * Logical ID of the CPU to which this lock belongs to.
59+ */
60+ private final int cpuId ;
61+ /**
62+ * CPU to which this lock belongs to is of general use.
63+ */
64+ private final boolean base ;
65+ /**
66+ * CPU to which this lock belongs to is reservable.
67+ */
68+ private final boolean reservable ;
69+ /**
70+ * An inventory build from the CPU layout which keeps track of the various locks
71+ * belonging to each CPU.
72+ */
73+ private final LockInventory lockInventory ;
74+ boolean bound = false ;
75+ @ Nullable
76+ Thread assignedThread ;
77+
78+ AffinityLock (int cpuId , boolean base , boolean reservable , LockInventory lockInventory ) {
79+ this .lockInventory = lockInventory ;
80+ this .cpuId = cpuId ;
81+ this .base = base ;
82+ this .reservable = reservable ;
83+ }
6084
6185 /**
6286 * Set the CPU layout for this machine. CPUs which are not mentioned will be ignored.
63- * <p></p>
87+ * <p>
6488 * Changing the layout will have no impact on thread which have already been assigned.
6589 * It only affects subsequent assignments.
6690 *
@@ -102,7 +126,7 @@ public static AffinityLock acquireLock() {
102126
103127 /**
104128 * Assign any free core to this thread.
105- * <p></p>
129+ * <p>
106130 * In reality, only one cpu is assigned, the rest of the threads for that core are reservable so they are not used.
107131 *
108132 * @return A handle for the current AffinityLock.
@@ -113,7 +137,7 @@ public static AffinityLock acquireCore() {
113137
114138 /**
115139 * Assign a cpu which can be bound to the current thread or another thread.
116- * <p></p>
140+ * <p>
117141 * This can be used for defining your thread layout centrally and passing the handle via dependency injection.
118142 *
119143 * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
@@ -125,7 +149,7 @@ public static AffinityLock acquireLock(boolean bind) {
125149
126150 /**
127151 * Assign a core(and all its cpus) which can be bound to the current thread or another thread.
128- * <p></p>
152+ * <p>
129153 * This can be used for defining your thread layout centrally and passing the handle via dependency injection.
130154 *
131155 * @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
@@ -143,7 +167,6 @@ private static AffinityLock acquireCore(boolean bind, int cpuId, @NotNull Affini
143167 return LOCK_INVENTORY .acquireCore (bind , cpuId , strategies );
144168 }
145169
146-
147170 /**
148171 * @return All the current locks as a String.
149172 */
@@ -152,39 +175,6 @@ public static String dumpLocks() {
152175 return LOCK_INVENTORY .dumpLocks ();
153176 }
154177
155- /**
156- * Logical ID of the CPU to which this lock belongs to.
157- */
158- private final int cpuId ;
159-
160- /**
161- * CPU to which this lock belongs to is of general use.
162- */
163- private final boolean base ;
164-
165- /**
166- * CPU to which this lock belongs to is reservable.
167- */
168- private final boolean reservable ;
169-
170- /**
171- * An inventory build from the CPU layout which keeps track of the various locks
172- * belonging to each CPU.
173- */
174- private final LockInventory lockInventory ;
175-
176- boolean bound = false ;
177-
178- @ Nullable
179- Thread assignedThread ;
180-
181- AffinityLock (int cpuId , boolean base , boolean reservable , LockInventory lockInventory ) {
182- this .lockInventory = lockInventory ;
183- this .cpuId = cpuId ;
184- this .base = base ;
185- this .reservable = reservable ;
186- }
187-
188178 /**
189179 * Assigning the current thread has a side effect of preventing the lock being used again until it is released.
190180 *
@@ -238,7 +228,7 @@ final boolean canReserve() {
238228
239229 /**
240230 * Give another affinity lock relative to this one based on a list of strategies.
241- * <p></p>
231+ * <p>
242232 * The strategies are evaluated in order to (like a search path) to find the next appropriate thread.
243233 * If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving the OS to choose)
244234 *
0 commit comments