23 */
24
25 /*
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
29 * file:
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
34 */
35
36 package java.util.concurrent.locks;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.ForkJoinPool;
43 import jdk.internal.misc.Unsafe;
44
45 /**
46 * A version of {@link AbstractQueuedSynchronizer} in
47 * which synchronization state is maintained as a {@code long}.
48 * This class has exactly the same structure, properties, and methods
49 * as {@code AbstractQueuedSynchronizer} with the exception
50 * that all state-related parameters and results are defined
51 * as {@code long} rather than {@code int}. This class
52 * may be useful when creating synchronizers such as
53 * multilevel locks and barriers that require
54 * 64 bits of state.
55 *
56 * <p>See {@link AbstractQueuedSynchronizer} for usage
57 * notes and examples.
58 *
59 * @since 1.6
60 * @author Doug Lea
61 */
62 public abstract class AbstractQueuedLongSynchronizer
1180 w = next;
1181 }
1182 }
1183 }
1184
1185 /**
1186 * Implements uninterruptible condition wait.
1187 * <ol>
1188 * <li>Save lock state returned by {@link #getState}.
1189 * <li>Invoke {@link #release} with saved state as argument,
1190 * throwing IllegalMonitorStateException if it fails.
1191 * <li>Block until signalled.
1192 * <li>Reacquire by invoking specialized version of
1193 * {@link #acquire} with saved state as argument.
1194 * </ol>
1195 */
1196 public final void awaitUninterruptibly() {
1197 ConditionNode node = new ConditionNode();
1198 long savedState = enableWait(node);
1199 LockSupport.setCurrentBlocker(this); // for back-compatibility
1200 boolean interrupted = false;
1201 while (!canReacquire(node)) {
1202 if (Thread.interrupted())
1203 interrupted = true;
1204 else if ((node.status & COND) != 0) {
1205 try {
1206 ForkJoinPool.managedBlock(node);
1207 } catch (InterruptedException ie) {
1208 interrupted = true;
1209 }
1210 } else
1211 Thread.onSpinWait(); // awoke while enqueuing
1212 }
1213 LockSupport.setCurrentBlocker(null);
1214 node.clearStatus();
1215 acquire(node, savedState, false, false, false, 0L);
1216 if (interrupted)
1217 Thread.currentThread().interrupt();
1218 }
1219
1220 /**
1221 * Implements interruptible condition wait.
1222 * <ol>
1223 * <li>If current thread is interrupted, throw InterruptedException.
1224 * <li>Save lock state returned by {@link #getState}.
1225 * <li>Invoke {@link #release} with saved state as argument,
1226 * throwing IllegalMonitorStateException if it fails.
1227 * <li>Block until signalled or interrupted.
1228 * <li>Reacquire by invoking specialized version of
1229 * {@link #acquire} with saved state as argument.
1230 * <li>If interrupted while blocked in step 4, throw InterruptedException.
1231 * </ol>
1232 */
1233 public final void await() throws InterruptedException {
1234 if (Thread.interrupted())
1235 throw new InterruptedException();
1236 ConditionNode node = new ConditionNode();
1237 long savedState = enableWait(node);
1238 LockSupport.setCurrentBlocker(this); // for back-compatibility
1239 boolean interrupted = false, cancelled = false;
1240 while (!canReacquire(node)) {
1241 if (interrupted |= Thread.interrupted()) {
1242 if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
1243 break; // else interrupted after signal
1244 } else if ((node.status & COND) != 0) {
1245 try {
1246 ForkJoinPool.managedBlock(node);
1247 } catch (InterruptedException ie) {
1248 interrupted = true;
1249 }
1250 } else
1251 Thread.onSpinWait(); // awoke while enqueuing
1252 }
1253 LockSupport.setCurrentBlocker(null);
1254 node.clearStatus();
1255 acquire(node, savedState, false, false, false, 0L);
1256 if (interrupted) {
1257 if (cancelled) {
1258 unlinkCancelledWaiters(node);
1259 throw new InterruptedException();
1260 }
1261 Thread.currentThread().interrupt();
1262 }
1263 }
1264
1265 /**
1266 * Implements timed condition wait.
|
23 */
24
25 /*
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
29 * file:
30 *
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
34 */
35
36 package java.util.concurrent.locks;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.ForkJoinPool;
43 import java.util.concurrent.RejectedExecutionException;
44 import jdk.internal.misc.Unsafe;
45
46 /**
47 * A version of {@link AbstractQueuedSynchronizer} in
48 * which synchronization state is maintained as a {@code long}.
49 * This class has exactly the same structure, properties, and methods
50 * as {@code AbstractQueuedSynchronizer} with the exception
51 * that all state-related parameters and results are defined
52 * as {@code long} rather than {@code int}. This class
53 * may be useful when creating synchronizers such as
54 * multilevel locks and barriers that require
55 * 64 bits of state.
56 *
57 * <p>See {@link AbstractQueuedSynchronizer} for usage
58 * notes and examples.
59 *
60 * @since 1.6
61 * @author Doug Lea
62 */
63 public abstract class AbstractQueuedLongSynchronizer
1181 w = next;
1182 }
1183 }
1184 }
1185
1186 /**
1187 * Implements uninterruptible condition wait.
1188 * <ol>
1189 * <li>Save lock state returned by {@link #getState}.
1190 * <li>Invoke {@link #release} with saved state as argument,
1191 * throwing IllegalMonitorStateException if it fails.
1192 * <li>Block until signalled.
1193 * <li>Reacquire by invoking specialized version of
1194 * {@link #acquire} with saved state as argument.
1195 * </ol>
1196 */
1197 public final void awaitUninterruptibly() {
1198 ConditionNode node = new ConditionNode();
1199 long savedState = enableWait(node);
1200 LockSupport.setCurrentBlocker(this); // for back-compatibility
1201 boolean interrupted = false, rejected = false;
1202 while (!canReacquire(node)) {
1203 if (Thread.interrupted())
1204 interrupted = true;
1205 else if ((node.status & COND) != 0) {
1206 try {
1207 if (rejected)
1208 node.block();
1209 else
1210 ForkJoinPool.managedBlock(node);
1211 } catch (RejectedExecutionException ex) {
1212 rejected = true;
1213 } catch (InterruptedException ie) {
1214 interrupted = true;
1215 }
1216 } else
1217 Thread.onSpinWait(); // awoke while enqueuing
1218 }
1219 LockSupport.setCurrentBlocker(null);
1220 node.clearStatus();
1221 acquire(node, savedState, false, false, false, 0L);
1222 if (interrupted)
1223 Thread.currentThread().interrupt();
1224 }
1225
1226 /**
1227 * Implements interruptible condition wait.
1228 * <ol>
1229 * <li>If current thread is interrupted, throw InterruptedException.
1230 * <li>Save lock state returned by {@link #getState}.
1231 * <li>Invoke {@link #release} with saved state as argument,
1232 * throwing IllegalMonitorStateException if it fails.
1233 * <li>Block until signalled or interrupted.
1234 * <li>Reacquire by invoking specialized version of
1235 * {@link #acquire} with saved state as argument.
1236 * <li>If interrupted while blocked in step 4, throw InterruptedException.
1237 * </ol>
1238 */
1239 public final void await() throws InterruptedException {
1240 if (Thread.interrupted())
1241 throw new InterruptedException();
1242 ConditionNode node = new ConditionNode();
1243 long savedState = enableWait(node);
1244 LockSupport.setCurrentBlocker(this); // for back-compatibility
1245 boolean interrupted = false, cancelled = false, rejected = false;
1246 while (!canReacquire(node)) {
1247 if (interrupted |= Thread.interrupted()) {
1248 if (cancelled = (node.getAndUnsetStatus(COND) & COND) != 0)
1249 break; // else interrupted after signal
1250 } else if ((node.status & COND) != 0) {
1251 try {
1252 if (rejected)
1253 node.block();
1254 else
1255 ForkJoinPool.managedBlock(node);
1256 } catch (RejectedExecutionException ex) {
1257 rejected = true;
1258 } catch (InterruptedException ie) {
1259 interrupted = true;
1260 }
1261 } else
1262 Thread.onSpinWait(); // awoke while enqueuing
1263 }
1264 LockSupport.setCurrentBlocker(null);
1265 node.clearStatus();
1266 acquire(node, savedState, false, false, false, 0L);
1267 if (interrupted) {
1268 if (cancelled) {
1269 unlinkCancelledWaiters(node);
1270 throw new InterruptedException();
1271 }
1272 Thread.currentThread().interrupt();
1273 }
1274 }
1275
1276 /**
1277 * Implements timed condition wait.
|