Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.devtools.build.skyframe.GraphTester.ValueComputer; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.skyframe.SkyFunctionException.Transience; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import java.util.concurrent.CountDownLatch; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import javax.annotation.Nullable; |
| 22 | |
| 23 | /** |
| 24 | * {@link ValueComputer} that can be chained together with others of its type to synchronize the |
| 25 | * order in which builders finish. |
| 26 | */ |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 27 | public final class ChainedFunction implements SkyFunction { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | @Nullable private final SkyValue value; |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 29 | private final Runnable notifyStart; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | @Nullable private final CountDownLatch waitToFinish; |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 31 | private final Runnable notifyFinish; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | private final boolean waitForException; |
| 33 | private final Iterable<SkyKey> deps; |
| 34 | |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 35 | /** Do not use! Use {@link Builder} instead. */ |
| 36 | ChainedFunction( |
| 37 | @Nullable CountDownLatch notifyStart, |
| 38 | @Nullable CountDownLatch waitToFinish, |
| 39 | @Nullable CountDownLatch notifyFinish, |
| 40 | boolean waitForException, |
| 41 | @Nullable SkyValue value, |
| 42 | Iterable<SkyKey> deps) { |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 43 | this( |
| 44 | makeRunnable(notifyStart), |
| 45 | waitToFinish, |
| 46 | makeRunnable(notifyFinish), |
| 47 | waitForException, |
| 48 | value, |
| 49 | deps); |
| 50 | } |
| 51 | |
| 52 | private ChainedFunction( |
| 53 | Runnable notifyStart, |
| 54 | @Nullable CountDownLatch waitToFinish, |
| 55 | Runnable notifyFinish, |
| 56 | boolean waitForException, |
| 57 | @Nullable SkyValue value, |
| 58 | Iterable<SkyKey> deps) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | this.notifyStart = notifyStart; |
| 60 | this.waitToFinish = waitToFinish; |
| 61 | this.notifyFinish = notifyFinish; |
| 62 | this.waitForException = waitForException; |
| 63 | Preconditions.checkState(this.waitToFinish != null || !this.waitForException, value); |
| 64 | this.value = value; |
| 65 | this.deps = deps; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public SkyValue compute(SkyKey key, SkyFunction.Environment env) throws GenericFunctionException, |
| 70 | InterruptedException { |
| 71 | try { |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 72 | notifyStart.run(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | if (waitToFinish != null) { |
Janak Ramakrishnan | a67bb8b | 2015-09-02 17:56:08 +0000 | [diff] [blame] | 74 | TrackingAwaiter.INSTANCE.awaitLatchAndTrackExceptions( |
| 75 | waitToFinish, key + " timed out waiting to finish"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | if (waitForException) { |
| 77 | SkyFunctionEnvironment skyEnv = (SkyFunctionEnvironment) env; |
Janak Ramakrishnan | a67bb8b | 2015-09-02 17:56:08 +0000 | [diff] [blame] | 78 | TrackingAwaiter.INSTANCE.awaitLatchAndTrackExceptions( |
| 79 | skyEnv.getExceptionLatchForTesting(), key + " timed out waiting for exception"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | for (SkyKey dep : deps) { |
| 83 | env.getValue(dep); |
| 84 | } |
| 85 | if (value == null) { |
| 86 | throw new GenericFunctionException(new SomeErrorException("oops"), |
| 87 | Transience.PERSISTENT); |
| 88 | } |
| 89 | if (env.valuesMissing()) { |
| 90 | return null; |
| 91 | } |
| 92 | return value; |
| 93 | } finally { |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 94 | notifyFinish.run(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 98 | private static Runnable makeRunnable(@Nullable CountDownLatch latch) { |
| 99 | return latch != null ? latch::countDown : () -> {}; |
| 100 | } |
| 101 | |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 102 | /** Builder for {@link ChainedFunction} objects. */ |
| 103 | public static class Builder { |
| 104 | @Nullable private SkyValue value; |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 105 | Runnable notifyStart = makeRunnable(null); |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 106 | @Nullable private CountDownLatch waitToFinish; |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 107 | private Runnable notifyFinish = makeRunnable(null); |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 108 | private boolean waitForException; |
| 109 | private Iterable<SkyKey> deps = ImmutableList.of(); |
| 110 | |
| 111 | public Builder setValue(SkyValue value) { |
| 112 | this.value = value; |
| 113 | return this; |
| 114 | } |
| 115 | |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 116 | public Builder setNotifyStart(Runnable notifyStart) { |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 117 | this.notifyStart = notifyStart; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | public Builder setWaitToFinish(CountDownLatch waitToFinish) { |
| 122 | this.waitToFinish = waitToFinish; |
| 123 | return this; |
| 124 | } |
| 125 | |
janakr | b4ff31b | 2019-01-14 08:29:37 -0800 | [diff] [blame] | 126 | public Builder setNotifyFinish(Runnable notifyFinish) { |
Janak Ramakrishnan | 1acd466 | 2016-02-10 20:30:41 +0000 | [diff] [blame] | 127 | this.notifyFinish = notifyFinish; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public Builder setWaitForException(boolean waitForException) { |
| 132 | this.waitForException = waitForException; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | public Builder setDeps(Iterable<SkyKey> deps) { |
| 137 | this.deps = Preconditions.checkNotNull(deps); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | public SkyFunction build() { |
| 142 | return new ChainedFunction( |
| 143 | notifyStart, waitToFinish, notifyFinish, waitForException, value, deps); |
| 144 | } |
| 145 | } |
| 146 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | @Override |
| 148 | public String extractTag(SkyKey skyKey) { |
| 149 | throw new UnsupportedOperationException(); |
| 150 | } |
| 151 | } |