| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.PackageData; |
| 11 | import net.sf.saxon.om.GroundedValue; |
| 12 | import net.sf.saxon.om.Sequence; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.value.ObjectValue; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * The Bindery class holds information about variables and their values. It is |
| 19 | * used only for global variables: local variables are held in the XPathContext object. |
| 20 | * |
| 21 | * <p>Variables are identified by a {@link GlobalVariable} object. Values will always be of class {@link Sequence}.</p> |
| 22 | * |
| 23 | * <p>The Bindery is a run-time object, but slot numbers within the bindery are allocated to global |
| 24 | * variables at compile time. Because XSLT packages can be separately compiled, each package needs |
| 25 | * to have its own bindery.</p> |
| 26 | * |
| 27 | * <p>From Saxon 9.7 there is one Bindery for the global variables in each separately-compiled package. |
| 28 | * The Bindery is no longer used to hold supplied values of global parameters, but it does hold their |
| 29 | * values after initialization, treating them as normal global variables. Management of dependencies |
| 30 | * among global variables, and checking for dynamic circularities, has been moved to the {@link net.sf.saxon.Controller}.</p> |
| 31 | */ |
| 32 | |
| 33 | public final class Bindery { |
| 34 | |
| 35 | private GroundedValue<?>[] globals; // values of global variables and parameters |
| 36 | private long[] busy; // set to current thread id while variable is being evaluated |
| 37 | |
| 38 | |
| 39 | public Bindery(PackageData pack) { |
| 40 | allocateGlobals(pack.getGlobalSlotManager()); |
| 41 | } |
| 42 | /** |
| 43 | * Define how many slots are needed for global variables |
| 44 | * |
| 45 | * @param map the SlotManager that keeps track of slot allocation for global variables. |
| 46 | */ |
| 47 | |
| 48 | private void allocateGlobals(SlotManager map) { |
| 49 | int n = map.getNumberOfVariables() + 1; |
| 50 | globals = (GroundedValue<?>[])new GroundedValue[n]; |
| 51 | busy = new long[n]; |
| 52 | for (int i = 0; i < n; i++) { |
| 53 | globals[i] = null; |
| 54 | busy[i] = -1L; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Provide a value for a global variable |
| 60 | * |
| 61 | * @param binding identifies the variable |
| 62 | * @param value the value of the variable |
| 63 | */ |
| 64 | |
| 65 | public void setGlobalVariable(GlobalVariable binding, GroundedValue<?> value) { |
| 66 | globals[binding.getBinderySlotNumber()] = value; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set/Unset a flag to indicate that a particular global variable is currently being |
| 71 | * evaluated. Note that this code is not synchronized, so there is no absolute guarantee that |
| 72 | * two threads will not both evaluate the same global variable; however, apart from wasted time, |
| 73 | * it is harmless if they do. |
| 74 | * |
| 75 | * @param binding the global variable in question |
| 76 | * @return true if evaluation of the variable should proceed; false if it is found that the variable has now been |
| 77 | * evaluated in another thread. |
| 78 | * @throws net.sf.saxon.trans.XPathException |
| 79 | * If an attempt is made to set the flag when it is already set, this means |
| 80 | * the definition of the variable is circular. |
| 81 | */ |
| 82 | |
| 83 | public boolean setExecuting(GlobalVariable binding) |
| 84 | throws XPathException { |
| 85 | long thisThread = Thread.currentThread().getId(); |
| 86 | int slot = binding.getBinderySlotNumber(); |
| 87 | |
| 88 | long busyThread = busy[slot]; |
| 89 | if (busyThread != -1L) { |
| 90 | if (busyThread == thisThread) { |
| 91 | // The global variable is being evaluated in this thread. This shouldn't happen, because |
| 92 | // we have already tested for circularities. If it does happen, however, we fail cleanly. |
| 93 | throw new XPathException.Circularity("Circular definition of variable " |
| 94 | + binding.getVariableQName().getDisplayName()); |
| 95 | } else { |
| 96 | // The global variable is being evaluated in another thread. Give it a chance to finish. |
| 97 | // It could be a circularity, or just an accident of timing. Note that in the latter case, |
| 98 | // we will actually re-evaluate the variable; this normally does no harm, though there is a small |
| 99 | // risk it could lead to problems with the identity of a node changing. |
| 100 | for (int i = 0; i < 10; i++) { |
| 101 | try { |
| 102 | Thread.sleep(20 * i); |
| 103 | } catch (InterruptedException e) { |
| 104 | // no action |
| 105 | } |
| 106 | if (busy[slot] == -1L) { |
| 107 | // evaluation has finished in another thread |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | // We've waited long enough; there could be a deadlock if we wait any longer. |
| 112 | // Continue with the evaluation; whichever thread completes the evaluation first will |
| 113 | // save the value. |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | busy[slot] = thisThread; |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Indicate that a global variable is not currently being evaluated |
| 123 | * |
| 124 | * @param binding the global variable |
| 125 | */ |
| 126 | |
| 127 | public void setNotExecuting(GlobalVariable binding) { |
| 128 | int slot = binding.getBinderySlotNumber(); |
| 129 | busy[slot] = -1L; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Save the value of a global variable, and mark evaluation as complete. |
| 135 | * |
| 136 | * @param binding the global variable in question |
| 137 | * @param value the value that this thread has obtained by evaluating the variable |
| 138 | * @return the value actually given to the variable. Exceptionally this will be different from the supplied |
| 139 | * value if another thread has evaluated the same global variable concurrently. The returned value should be |
| 140 | * used in preference, to ensure that all threads agree on the value. They could be different if for example |
| 141 | * the variable is initialized using the collection() function. |
| 142 | */ |
| 143 | |
| 144 | public synchronized GroundedValue<?> saveGlobalVariableValue( |
| 145 | GlobalVariable binding, GroundedValue<?> value) { |
| 146 | int slot = binding.getBinderySlotNumber(); |
| 147 | if (globals[slot] != null) { |
| 148 | // another thread has already evaluated the value |
| 149 | return globals[slot]; |
| 150 | } else { |
| 151 | busy[slot] = -1L; |
| 152 | globals[slot] = value; |
| 153 | return value; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Get the value of a global variable |
| 160 | * |
| 161 | * @param binding the Binding that establishes the unique instance of the variable |
| 162 | * @return the Value of the variable if defined, null otherwise. |
| 163 | */ |
| 164 | |
| 165 | public GroundedValue<?> getGlobalVariableValue(GlobalVariable binding) { |
| 166 | return globals[binding.getBinderySlotNumber()]; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the value of a global variable whose slot number is known |
| 171 | * |
| 172 | * @param slot the slot number of the required variable |
| 173 | * @return the Value of the variable if defined, null otherwise. |
| 174 | */ |
| 175 | |
| 176 | public GroundedValue<?> getGlobalVariable(int slot) { |
| 177 | return globals[slot]; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get all the global variables, as an array. This is provided for use by debuggers. |
| 182 | * The meaning of the result can be determined by use of data held in the corresponding PackageData. |
| 183 | * |
| 184 | * @return the array of global variables. |
| 185 | */ |
| 186 | |
| 187 | public GroundedValue[] getGlobalVariables() { |
| 188 | return globals; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * A value that can be saved in a global variable to indicate that evaluation failed, and that |
| 194 | * subsequent attempts at evaluation should also fail |
| 195 | */ |
| 196 | |
| 197 | public static class FailureValue extends ObjectValue<XPathException> { |
| 198 | |
| 199 | public FailureValue(XPathException err) { |
| 200 | super(err); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | } |
| 206 |