2323import org .slf4j .LoggerFactory ;
2424
2525import java .io .IOException ;
26+ import java .nio .channels .ClosedByInterruptException ;
2627import java .util .NavigableMap ;
2728import java .util .TreeMap ;
2829
@@ -78,12 +79,15 @@ private static boolean isAnyCpu(final int cpuId) {
7879 * @param wholeCore Whether to bind the whole core
7980 * @return true if the lock was acquired, false otherwise
8081 */
81- private static boolean updateLockForCurrentThread (final boolean bind , final AffinityLock al , final boolean wholeCore ) {
82+ private static boolean updateLockForCurrentThread (final boolean bind , final AffinityLock al , final boolean wholeCore ) throws ClosedByInterruptException {
8283 try {
8384 if (LockCheck .updateCpu (al .cpuId ())) {
8485 al .assignCurrentThread (bind , wholeCore );
8586 return true ;
8687 }
88+ } catch (ClosedByInterruptException e ) {
89+ throw e ;
90+
8791 } catch (IOException e ) {
8892 LOGGER .warn ("Error occurred acquiring lock" , e );
8993 }
@@ -120,30 +124,36 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
120124 return noLock ();
121125
122126 final boolean specificCpuRequested = !isAnyCpu (cpuId );
123- if (specificCpuRequested && cpuId != 0 ) {
124- final AffinityLock required = logicalCoreLocks [cpuId ];
125- if (required .canReserve (true )
126- && anyStrategyMatches (cpuId , cpuId , strategies )
127- && updateLockForCurrentThread (bind , required , false )) {
128- return required ;
127+ try {
128+ if (specificCpuRequested && cpuId != 0 ) {
129+ final AffinityLock required = logicalCoreLocks [cpuId ];
130+ if (required .canReserve (true )
131+ && anyStrategyMatches (cpuId , cpuId , strategies )
132+ && updateLockForCurrentThread (bind , required , false )) {
133+ return required ;
134+ }
135+ LOGGER .warn ("Unable to acquire lock on CPU {} for thread {}, trying to find another CPU" ,
136+ cpuId , Thread .currentThread ());
129137 }
130- LOGGER .warn ("Unable to acquire lock on CPU {} for thread {}, trying to find another CPU" ,
131- cpuId , Thread .currentThread ());
132- }
133138
134- for (AffinityStrategy strategy : strategies ) {
135- // consider all processors except cpu 0 which is usually used by the OS.
136- // if you have only one core, this library is not appropriate in any case.
137- for (int i = logicalCoreLocks .length - 1 ; i > 0 ; i --) {
138- AffinityLock al = logicalCoreLocks [i ];
139- if (al .canReserve (false )
140- && (isAnyCpu (cpuId ) || strategy .matches (cpuId , al .cpuId ()))
141- && updateLockForCurrentThread (bind , al , false )) {
142- return al ;
139+ for (AffinityStrategy strategy : strategies ) {
140+ // consider all processors except cpu 0 which is usually used by the OS.
141+ // if you have only one core, this library is not appropriate in any case.
142+ for (int i = logicalCoreLocks .length - 1 ; i > 0 ; i --) {
143+ AffinityLock al = logicalCoreLocks [i ];
144+ if (al .canReserve (false )
145+ && (isAnyCpu (cpuId ) || strategy .matches (cpuId , al .cpuId ()))
146+ && updateLockForCurrentThread (bind , al , false )) {
147+ return al ;
148+ }
143149 }
144150 }
151+ } catch (ClosedByInterruptException e ) {
152+ Thread .currentThread ().interrupt ();
153+ return noLock ();
145154 }
146155
156+
147157 LOGGER .warn ("No reservable CPU for {}" , Thread .currentThread ());
148158
149159 return noLock ();
@@ -154,11 +164,17 @@ public final synchronized AffinityLock tryAcquireLock(boolean bind, int cpuId) {
154164 return null ;
155165
156166 final AffinityLock required = logicalCoreLocks [cpuId ];
157- if (required .canReserve (true )
158- && updateLockForCurrentThread (bind , required , false )) {
159- return required ;
167+ try {
168+ if (required .canReserve (true )
169+ && updateLockForCurrentThread (bind , required , false )) {
170+ return required ;
171+ }
172+ } catch (ClosedByInterruptException e ) {
173+ Thread .currentThread ().interrupt ();
174+ return noLock ();
160175 }
161176
177+
162178 LOGGER .warn ("Unable to acquire lock on CPU {} for thread {}, trying to find another CPU" ,
163179 cpuId , Thread .currentThread ());
164180
@@ -174,9 +190,14 @@ public final synchronized AffinityLock acquireCore(boolean bind, int cpuId, Affi
174190 continue LOOP ;
175191
176192 final AffinityLock al = als [0 ];
193+ try {
177194 if (updateLockForCurrentThread (bind , al , true )) {
178195 return al ;
179196 }
197+ } catch (ClosedByInterruptException e ) {
198+ Thread .currentThread ().interrupt ();
199+ return noLock ();
200+ }
180201 }
181202 }
182203
0 commit comments