Skip to content

Commit 623cfcd

Browse files
author
Alan Bateman
committed
8310892: ScopedValue throwing StructureViolationException should be clearer
Reviewed-by: dfuchs, jpai
1 parent 06a1a15 commit 623cfcd

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

src/java.base/share/classes/java/lang/ScopedValue.java

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -394,19 +394,20 @@ public <T> T get(ScopedValue<T> key) {
394394
* to its value in the current thread.
395395
* When the operation completes (normally or with an exception), each scoped value
396396
* in the mapping will revert to being unbound, or revert to its previous value
397-
* when previously bound, in the current thread.
397+
* when previously bound, in the current thread. If {@code op} completes with an
398+
* exception then it propagated by this method.
398399
*
399-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
400-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
401-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
402-
* underlying construct of each {@code StructuredTaskScope} created in the
403-
* dynamic scope to be closed. This may require blocking until all child threads
404-
* have completed their sub-tasks. The closing is done in the reverse order that
405-
* they were created. Once closed, {@link StructureViolationException} is thrown.
400+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
401+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
402+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
403+
* as a <em>structure violation</em> when the operation completes (normally or with an
404+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
405+
* is closed and {@link StructureViolationException} is thrown.
406406
*
407407
* @param op the operation to run
408408
* @param <R> the type of the result of the operation
409409
* @return the result
410+
* @throws StructureViolationException if a structure violation is detected
410411
* @throws Exception if {@code op} completes with an exception
411412
* @see ScopedValue#callWhere(ScopedValue, Object, Callable)
412413
*/
@@ -423,19 +424,20 @@ public <R> R call(Callable<? extends R> op) throws Exception {
423424
* to its value in the current thread.
424425
* When the operation completes (normally or with an exception), each scoped value
425426
* in the mapping will revert to being unbound, or revert to its previous value
426-
* when previously bound, in the current thread.
427+
* when previously bound, in the current thread. If {@code op} completes with an
428+
* exception then it propagated by this method.
427429
*
428-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
429-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
430-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
431-
* underlying construct of each {@code StructuredTaskScope} created in the
432-
* dynamic scope to be closed. This may require blocking until all child threads
433-
* have completed their sub-tasks. The closing is done in the reverse order that
434-
* they were created. Once closed, {@link StructureViolationException} is thrown.
430+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
431+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
432+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
433+
* as a <em>structure violation</em> when the operation completes (normally or with an
434+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
435+
* is closed and {@link StructureViolationException} is thrown.
435436
*
436437
* @param op the operation to run
437438
* @param <R> the type of the result of the operation
438439
* @return the result
440+
* @throws StructureViolationException if a structure violation is detected
439441
* @see ScopedValue#getWhere(ScopedValue, Object, Supplier)
440442
*/
441443
public <R> R get(Supplier<? extends R> op) {
@@ -486,17 +488,18 @@ private <R> R runWith(Snapshot newSnapshot, Callable<R> op) {
486488
* in the current thread.
487489
* When the operation completes (normally or with an exception), each scoped value
488490
* in the mapping will revert to being unbound, or revert to its previous value
489-
* when previously bound, in the current thread.
491+
* when previously bound, in the current thread. If {@code op} completes with an
492+
* exception then it propagated by this method.
490493
*
491-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
492-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
493-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
494-
* underlying construct of each {@code StructuredTaskScope} created in the
495-
* dynamic scope to be closed. This may require blocking until all child threads
496-
* have completed their sub-tasks. The closing is done in the reverse order that
497-
* they were created. Once closed, {@link StructureViolationException} is thrown.
494+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
495+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
496+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
497+
* as a <em>structure violation</em> when the operation completes (normally or with an
498+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
499+
* is closed and {@link StructureViolationException} is thrown.
498500
*
499501
* @param op the operation to run
502+
* @throws StructureViolationException if a structure violation is detected
500503
* @see ScopedValue#runWhere(ScopedValue, Object, Runnable)
501504
*/
502505
public void run(Runnable op) {
@@ -553,15 +556,15 @@ public static <T> Carrier where(ScopedValue<T> key, T value) {
553556
* Calls a value-returning operation with a {@code ScopedValue} bound to a value
554557
* in the current thread. When the operation completes (normally or with an
555558
* exception), the {@code ScopedValue} will revert to being unbound, or revert to
556-
* its previous value when previously bound, in the current thread.
559+
* its previous value when previously bound, in the current thread. If {@code op}
560+
* completes with an exception then it propagated by this method.
557561
*
558-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
559-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
560-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
561-
* underlying construct of each {@code StructuredTaskScope} created in the
562-
* dynamic scope to be closed. This may require blocking until all child threads
563-
* have completed their sub-tasks. The closing is done in the reverse order that
564-
* they were created. Once closed, {@link StructureViolationException} is thrown.
562+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
563+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
564+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
565+
* as a <em>structure violation</em> when the operation completes (normally or with an
566+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
567+
* is closed and {@link StructureViolationException} is thrown.
565568
*
566569
* @implNote
567570
* This method is implemented to be equivalent to:
@@ -576,6 +579,7 @@ public static <T> Carrier where(ScopedValue<T> key, T value) {
576579
* @param <R> the result type
577580
* @param op the operation to call
578581
* @return the result
582+
* @throws StructureViolationException if a structure violation is detected
579583
* @throws Exception if the operation completes with an exception
580584
*/
581585
public static <T, R> R callWhere(ScopedValue<T> key,
@@ -588,15 +592,15 @@ public static <T, R> R callWhere(ScopedValue<T> key,
588592
* Invokes a supplier of results with a {@code ScopedValue} bound to a value
589593
* in the current thread. When the operation completes (normally or with an
590594
* exception), the {@code ScopedValue} will revert to being unbound, or revert to
591-
* its previous value when previously bound, in the current thread.
595+
* its previous value when previously bound, in the current thread. If {@code op}
596+
* completes with an exception then it propagated by this method.
592597
*
593-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
594-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
595-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
596-
* underlying construct of each {@code StructuredTaskScope} created in the
597-
* dynamic scope to be closed. This may require blocking until all child threads
598-
* have completed their sub-tasks. The closing is done in the reverse order that
599-
* they were created. Once closed, {@link StructureViolationException} is thrown.
598+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
599+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
600+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
601+
* as a <em>structure violation</em> when the operation completes (normally or with an
602+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
603+
* is closed and {@link StructureViolationException} is thrown.
600604
*
601605
* @implNote
602606
* This method is implemented to be equivalent to:
@@ -611,6 +615,7 @@ public static <T, R> R callWhere(ScopedValue<T> key,
611615
* @param <R> the result type
612616
* @param op the operation to call
613617
* @return the result
618+
* @throws StructureViolationException if a structure violation is detected
614619
*/
615620
public static <T, R> R getWhere(ScopedValue<T> key,
616621
T value,
@@ -622,15 +627,15 @@ public static <T, R> R getWhere(ScopedValue<T> key,
622627
* Run an operation with a {@code ScopedValue} bound to a value in the current
623628
* thread. When the operation completes (normally or with an exception), the
624629
* {@code ScopedValue} will revert to being unbound, or revert to its previous value
625-
* when previously bound, in the current thread.
630+
* when previously bound, in the current thread. If {@code op} completes with an
631+
* exception then it propagated by this method.
626632
*
627-
* <p> Scoped values are intended to be used in a <em>structured manner</em>.
628-
* If {@code op} creates a {@link StructuredTaskScope} but does not {@linkplain
629-
* StructuredTaskScope#close() close} it, then exiting {@code op} causes the
630-
* underlying construct of each {@code StructuredTaskScope} created in the
631-
* dynamic scope to be closed. This may require blocking until all child threads
632-
* have completed their sub-tasks. The closing is done in the reverse order that
633-
* they were created. Once closed, {@link StructureViolationException} is thrown.
633+
* <p> Scoped values are intended to be used in a <em>structured manner</em>. If code
634+
* invoked directly or indirectly by the operation creates a {@link StructuredTaskScope}
635+
* but does not {@linkplain StructuredTaskScope#close() close} it, then it is detected
636+
* as a <em>structure violation</em> when the operation completes (normally or with an
637+
* exception). In that case, the underlying construct of the {@code StructuredTaskScope}
638+
* is closed and {@link StructureViolationException} is thrown.
634639
*
635640
* @implNote
636641
* This method is implemented to be equivalent to:
@@ -643,6 +648,7 @@ public static <T, R> R getWhere(ScopedValue<T> key,
643648
* @param value the value, can be {@code null}
644649
* @param <T> the type of the value
645650
* @param op the operation to call
651+
* @throws StructureViolationException if a structure violation is detected
646652
*/
647653
public static <T> void runWhere(ScopedValue<T> key, T value, Runnable op) {
648654
where(key, value).run(op);

0 commit comments

Comments
 (0)