StampedVariableAccessor.java

  1. /*
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Eclipse Public
  6.  * License Version 1.0 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.eclipse.org/legal/epl-v10.html
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * Alternatively, the contents of this file may be used under the terms of
  16.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  17.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  18.  * in which case the provisions of the GPL or the LGPL are applicable instead
  19.  * of those above. If you wish to allow use of your version of this file only
  20.  * under the terms of either the GPL or the LGPL, and not to allow others to
  21.  * use your version of this file under the terms of the EPL, indicate your
  22.  * decision by deleting the provisions above and replace them with the notice
  23.  * and other provisions required by the GPL or the LGPL. If you do not delete
  24.  * the provisions above, a recipient may use your version of this file under
  25.  * the terms of any one of the EPL, the GPL or the LGPL.
  26.  ***** END LICENSE BLOCK *****/
  27. package org.jruby.runtime.ivars;

  28. import org.jruby.RubyBasicObject;
  29. import org.jruby.RubyClass;
  30. import org.jruby.util.unsafe.UnsafeHolder;

  31. /**
  32.  * A variable accessor that uses a stamped volatile int and Unsafe methods to
  33.  * ensure thread-friendly table updating.
  34.  */
  35. public class StampedVariableAccessor extends VariableAccessor {
  36.     /**
  37.      * Construct a new StampedVariableAccessor for the given "real" class,
  38.      * variable name, variable index, and class ID.
  39.      *
  40.      * @param realClass the "real" class
  41.      * @param name the variable's name
  42.      * @param index the variable's index
  43.      * @param classId the class's ID
  44.      */
  45.     public StampedVariableAccessor(RubyClass realClass, String name, int index, int classId) {
  46.         super(realClass, name, index, classId);
  47.     }

  48.     /**
  49.      * Set this variable into the given object using Unsafe to ensure
  50.      * safe updating of the variable table.
  51.      *
  52.      * @param object the object into which to set this variable
  53.      * @param value the variable's value
  54.      */
  55.     public void set(Object object, Object value) {
  56.         ((RubyBasicObject)object).ensureInstanceVariablesSettable();
  57.         setVariable((RubyBasicObject)object, realClass, index, value);
  58.     }
  59.    
  60.     /**
  61.      * Set the given variable index into the specified object. The "real" class
  62.      * and index are pass in to provide functional access. This version checks
  63.      * if self has been frozen before proceeding to set the variable.
  64.      *
  65.      * @param self the object into which to set the variable
  66.      * @param realClass the "real" class for the object
  67.      * @param index the index of the variable
  68.      * @param value the variable's value
  69.      */
  70.     public static void setVariableChecked(RubyBasicObject self, RubyClass realClass, int index, Object value) {
  71.         self.ensureInstanceVariablesSettable();
  72.         setVariable(self, realClass, index, value);
  73.     }
  74.    
  75.     /**
  76.      * Set the given variable index into the specified object. The "real" class
  77.      * and index are pass in to provide functional access.
  78.      *
  79.      * @param self the object into which to set the variable
  80.      * @param realClass the "real" class for the object
  81.      * @param index the index of the variable
  82.      * @param value the variable's value
  83.      */
  84.     public static void setVariable(RubyBasicObject self, RubyClass realClass, int index, Object value) {
  85.         while (true) {
  86.             int currentStamp = self.varTableStamp;
  87.             // spin-wait if odd
  88.             if((currentStamp & 0x01) != 0)
  89.                continue;
  90.            
  91.             Object[] currentTable = (Object[]) UnsafeHolder.U.getObjectVolatile(self, RubyBasicObject.VAR_TABLE_OFFSET);
  92.            
  93.             if (currentTable == null || index >= currentTable.length) {
  94.                 if (!createTableUnsafe(self, currentStamp, realClass, currentTable, index, value)) continue;
  95.             } else {
  96.                 if (!updateTableUnsafe(self, currentStamp, currentTable, index, value)) continue;
  97.             }
  98.            
  99.             break;
  100.         }
  101.     }

  102.     /**
  103.      * Create or exapand a table for the given object, using Unsafe CAS and
  104.      * ordering operations to ensure visibility.
  105.      *
  106.      * @param self the object into which to set the variable
  107.      * @param currentStamp the current variable table stamp
  108.      * @param realClass the "real" class for the object
  109.      * @param currentTable the current table
  110.      * @param index the index of the variable
  111.      * @param value the variable's value
  112.      * @return whether the update was successful, for CAS retrying
  113.      */
  114.     private static boolean createTableUnsafe(RubyBasicObject self, int currentStamp, RubyClass realClass, Object[] currentTable, int index, Object value) {
  115.         // try to acquire exclusive access to the varTable field
  116.         if (!UnsafeHolder.U.compareAndSwapInt(self, RubyBasicObject.STAMP_OFFSET, currentStamp, ++currentStamp)) {
  117.             return false;
  118.         }
  119.        
  120.         Object[] newTable = new Object[realClass.getVariableTableSizeWithExtras()];
  121.        
  122.         if(currentTable != null) {
  123.             System.arraycopy(currentTable, 0, newTable, 0, currentTable.length);
  124.         }
  125.        
  126.         newTable[index] = value;
  127.        
  128.         UnsafeHolder.U.putOrderedObject(self, RubyBasicObject.VAR_TABLE_OFFSET, newTable);
  129.        
  130.         // release exclusive access
  131.         self.varTableStamp = currentStamp + 1;
  132.        
  133.         return true;
  134.     }

  135.     /**
  136.      * Update the given table table for the given object, using Unsafe fence or
  137.      * volatile operations to ensure visibility.
  138.      *
  139.      * @param self the object into which to set the variable
  140.      * @param currentStamp the current variable table stamp
  141.      * @param currentTable the current table
  142.      * @param index the index of the variable
  143.      * @param value the variable's value
  144.      * @return whether the update was successful, for CAS retrying
  145.      */
  146.     private static boolean updateTableUnsafe(RubyBasicObject self, int currentStamp, Object[] currentTable, int index, Object value) {
  147.         // shared access to varTable field.
  148.         if(UnsafeHolder.SUPPORTS_FENCES) {
  149.             currentTable[index] = value;
  150.             UnsafeHolder.fullFence();
  151.         } else {
  152.             // TODO: maybe optimize by read and checking current value before setting
  153.             UnsafeHolder.U.putObjectVolatile(currentTable, UnsafeHolder.ARRAY_OBJECT_BASE_OFFSET + UnsafeHolder.ARRAY_OBJECT_INDEX_SCALE * index, value);
  154.         }

  155.         // validate stamp. redo on concurrent modification
  156.         return self.varTableStamp == currentStamp;
  157.     }
  158. }