2222import org .slf4j .Logger ;
2323import org .slf4j .LoggerFactory ;
2424
25+ import java .io .IOException ;
2526import java .util .NavigableMap ;
2627import java .util .TreeMap ;
2728
@@ -69,9 +70,24 @@ private static boolean isAnyCpu(final int cpuId) {
6970 return cpuId == AffinityLock .ANY_CPU ;
7071 }
7172
72- private static void updateLockForCurrentThread (final boolean bind , final AffinityLock al , final boolean b ) {
73- al .assignCurrentThread (bind , b );
74- LockCheck .updateCpu (al .cpuId ());
73+ /**
74+ * Update the lock for the current thread
75+ *
76+ * @param bind Whether to also bind the thread to the core
77+ * @param al The lock to update
78+ * @param wholeCore Whether to bind the whole core
79+ * @return true if the lock was acquired, false otherwise
80+ */
81+ private static boolean updateLockForCurrentThread (final boolean bind , final AffinityLock al , final boolean wholeCore ) {
82+ try {
83+ if (LockCheck .updateCpu (al .cpuId ())) {
84+ al .assignCurrentThread (bind , wholeCore );
85+ return true ;
86+ }
87+ } catch (IOException e ) {
88+ LOGGER .warn ("Error occurred acquiring lock" , e );
89+ }
90+ return false ;
7591 }
7692
7793 public final synchronized CpuLayout getCpuLayout () {
@@ -106,8 +122,9 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
106122 final boolean specificCpuRequested = !isAnyCpu (cpuId );
107123 if (specificCpuRequested && cpuId != 0 ) {
108124 final AffinityLock required = logicalCoreLocks [cpuId ];
109- if (required .canReserve (true ) && anyStrategyMatches (cpuId , cpuId , strategies )) {
110- updateLockForCurrentThread (bind , required , false );
125+ if (required .canReserve (true )
126+ && anyStrategyMatches (cpuId , cpuId , strategies )
127+ && updateLockForCurrentThread (bind , required , false )) {
111128 return required ;
112129 }
113130 LOGGER .warn ("Unable to acquire lock on CPU {} for thread {}, trying to find another CPU" ,
@@ -119,8 +136,9 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
119136 // if you have only one core, this library is not appropriate in any case.
120137 for (int i = logicalCoreLocks .length - 1 ; i > 0 ; i --) {
121138 AffinityLock al = logicalCoreLocks [i ];
122- if (al .canReserve (false ) && (isAnyCpu (cpuId ) || strategy .matches (cpuId , al .cpuId ()))) {
123- updateLockForCurrentThread (bind , al , false );
139+ if (al .canReserve (false )
140+ && (isAnyCpu (cpuId ) || strategy .matches (cpuId , al .cpuId ()))
141+ && updateLockForCurrentThread (bind , al , false )) {
124142 return al ;
125143 }
126144 }
@@ -136,8 +154,8 @@ public final synchronized AffinityLock tryAcquireLock(boolean bind, int cpuId) {
136154 return null ;
137155
138156 final AffinityLock required = logicalCoreLocks [cpuId ];
139- if (required .canReserve (true )) {
140- updateLockForCurrentThread (bind , required , false );
157+ if (required .canReserve (true )
158+ && updateLockForCurrentThread (bind , required , false )) {
141159 return required ;
142160 }
143161
@@ -156,8 +174,9 @@ public final synchronized AffinityLock acquireCore(boolean bind, int cpuId, Affi
156174 continue LOOP ;
157175
158176 final AffinityLock al = als [0 ];
159- updateLockForCurrentThread (bind , al , true );
160- return al ;
177+ if (updateLockForCurrentThread (bind , al , true )) {
178+ return al ;
179+ }
161180 }
162181 }
163182
0 commit comments