Unsafe.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Unsafe.java - Unsafe operations needed for concurrency
  2. Copyright (C) 2006 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package sun.misc;
  32. import java.lang.reflect.Field;
  33. /**
  34. * This class should provide access to low-level operations and its
  35. * use should be limited to trusted code. Fields can be accessed using
  36. * memory addresses, with undefined behaviour occurring if invalid memory
  37. * addresses are given.
  38. *
  39. * @author Tom Tromey (tromey@redhat.com)
  40. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  41. */
  42. public class Unsafe
  43. {
  44. // Singleton class.
  45. private static Unsafe unsafe = new Unsafe();
  46. /**
  47. * Private default constructor to prevent creation of an arbitrary
  48. * number of instances.
  49. */
  50. private Unsafe()
  51. {
  52. }
  53. /**
  54. * Retrieve the singleton instance of <code>Unsafe</code>. The calling
  55. * method should guard this instance from untrusted code, as it provides
  56. * access to low-level operations such as direct memory access.
  57. *
  58. * @throws SecurityException if a security manager exists and prevents
  59. * access to the system properties.
  60. */
  61. public static Unsafe getUnsafe()
  62. {
  63. SecurityManager sm = System.getSecurityManager();
  64. if (sm != null)
  65. sm.checkPropertiesAccess();
  66. return unsafe;
  67. }
  68. /**
  69. * Returns the memory address offset of the given static field.
  70. * The offset is merely used as a means to access a particular field
  71. * in the other methods of this class. The value is unique to the given
  72. * field and the same value should be returned on each subsequent call.
  73. *
  74. * @param field the field whose offset should be returned.
  75. * @return the offset of the given field.
  76. */
  77. public native long objectFieldOffset(Field field);
  78. /**
  79. * Compares the value of the integer field at the specified offset
  80. * in the supplied object with the given expected value, and updates
  81. * it if they match. The operation of this method should be atomic,
  82. * thus providing an uninterruptible way of updating an integer field.
  83. *
  84. * @param obj the object containing the field to modify.
  85. * @param offset the offset of the integer field within <code>obj</code>.
  86. * @param expect the expected value of the field.
  87. * @param update the new value of the field if it equals <code>expect</code>.
  88. * @return true if the field was changed.
  89. */
  90. public native boolean compareAndSwapInt(Object obj, long offset,
  91. int expect, int update);
  92. /**
  93. * Compares the value of the long field at the specified offset
  94. * in the supplied object with the given expected value, and updates
  95. * it if they match. The operation of this method should be atomic,
  96. * thus providing an uninterruptible way of updating a long field.
  97. *
  98. * @param obj the object containing the field to modify.
  99. * @param offset the offset of the long field within <code>obj</code>.
  100. * @param expect the expected value of the field.
  101. * @param update the new value of the field if it equals <code>expect</code>.
  102. * @return true if the field was changed.
  103. */
  104. public native boolean compareAndSwapLong(Object obj, long offset,
  105. long expect, long update);
  106. /**
  107. * Compares the value of the object field at the specified offset
  108. * in the supplied object with the given expected value, and updates
  109. * it if they match. The operation of this method should be atomic,
  110. * thus providing an uninterruptible way of updating an object field.
  111. *
  112. * @param obj the object containing the field to modify.
  113. * @param offset the offset of the object field within <code>obj</code>.
  114. * @param expect the expected value of the field.
  115. * @param update the new value of the field if it equals <code>expect</code>.
  116. * @return true if the field was changed.
  117. */
  118. public native boolean compareAndSwapObject(Object obj, long offset,
  119. Object expect, Object update);
  120. /**
  121. * Sets the value of the integer field at the specified offset in the
  122. * supplied object to the given value. This is an ordered or lazy
  123. * version of <code>putIntVolatile(Object,long,int)</code>, which
  124. * doesn't guarantee the immediate visibility of the change to other
  125. * threads. It is only really useful where the integer field is
  126. * <code>volatile</code>, and is thus expected to change unexpectedly.
  127. *
  128. * @param obj the object containing the field to modify.
  129. * @param offset the offset of the integer field within <code>obj</code>.
  130. * @param value the new value of the field.
  131. * @see #putIntVolatile(Object,long,int)
  132. */
  133. public native void putOrderedInt(Object obj, long offset, int value);
  134. /**
  135. * Sets the value of the long field at the specified offset in the
  136. * supplied object to the given value. This is an ordered or lazy
  137. * version of <code>putLongVolatile(Object,long,long)</code>, which
  138. * doesn't guarantee the immediate visibility of the change to other
  139. * threads. It is only really useful where the long field is
  140. * <code>volatile</code>, and is thus expected to change unexpectedly.
  141. *
  142. * @param obj the object containing the field to modify.
  143. * @param offset the offset of the long field within <code>obj</code>.
  144. * @param value the new value of the field.
  145. * @see #putLongVolatile(Object,long,long)
  146. */
  147. public native void putOrderedLong(Object obj, long offset, long value);
  148. /**
  149. * Sets the value of the object field at the specified offset in the
  150. * supplied object to the given value. This is an ordered or lazy
  151. * version of <code>putObjectVolatile(Object,long,Object)</code>, which
  152. * doesn't guarantee the immediate visibility of the change to other
  153. * threads. It is only really useful where the object field is
  154. * <code>volatile</code>, and is thus expected to change unexpectedly.
  155. *
  156. * @param obj the object containing the field to modify.
  157. * @param offset the offset of the object field within <code>obj</code>.
  158. * @param value the new value of the field.
  159. */
  160. public native void putOrderedObject(Object obj, long offset, Object value);
  161. /**
  162. * Sets the value of the integer field at the specified offset in the
  163. * supplied object to the given value, with volatile store semantics.
  164. *
  165. * @param obj the object containing the field to modify.
  166. * @param offset the offset of the integer field within <code>obj</code>.
  167. * @param value the new value of the field.
  168. */
  169. public native void putIntVolatile(Object obj, long offset, int value);
  170. /**
  171. * Retrieves the value of the integer field at the specified offset in the
  172. * supplied object with volatile load semantics.
  173. *
  174. * @param obj the object containing the field to read.
  175. * @param offset the offset of the integer field within <code>obj</code>.
  176. */
  177. public native int getIntVolatile(Object obj, long offset);
  178. /**
  179. * Sets the value of the long field at the specified offset in the
  180. * supplied object to the given value, with volatile store semantics.
  181. *
  182. * @param obj the object containing the field to modify.
  183. * @param offset the offset of the long field within <code>obj</code>.
  184. * @param value the new value of the field.
  185. * @see #putLong(Object,long,long)
  186. */
  187. public native void putLongVolatile(Object obj, long offset, long value);
  188. /**
  189. * Sets the value of the long field at the specified offset in the
  190. * supplied object to the given value.
  191. *
  192. * @param obj the object containing the field to modify.
  193. * @param offset the offset of the long field within <code>obj</code>.
  194. * @param value the new value of the field.
  195. * @see #putLongVolatile(Object,long,long)
  196. */
  197. public native void putLong(Object obj, long offset, long value);
  198. /**
  199. * Retrieves the value of the long field at the specified offset in the
  200. * supplied object with volatile load semantics.
  201. *
  202. * @param obj the object containing the field to read.
  203. * @param offset the offset of the long field within <code>obj</code>.
  204. * @see #getLong(Object,long)
  205. */
  206. public native long getLongVolatile(Object obj, long offset);
  207. /**
  208. * Retrieves the value of the long field at the specified offset in the
  209. * supplied object.
  210. *
  211. * @param obj the object containing the field to read.
  212. * @param offset the offset of the long field within <code>obj</code>.
  213. * @see #getLongVolatile(Object,long)
  214. */
  215. public native long getLong(Object obj, long offset);
  216. /**
  217. * Sets the value of the object field at the specified offset in the
  218. * supplied object to the given value, with volatile store semantics.
  219. *
  220. * @param obj the object containing the field to modify.
  221. * @param offset the offset of the object field within <code>obj</code>.
  222. * @param value the new value of the field.
  223. * @see #putObject(Object,long,Object)
  224. */
  225. public native void putObjectVolatile(Object obj, long offset, Object value);
  226. /**
  227. * Sets the value of the object field at the specified offset in the
  228. * supplied object to the given value.
  229. *
  230. * @param obj the object containing the field to modify.
  231. * @param offset the offset of the object field within <code>obj</code>.
  232. * @param value the new value of the field.
  233. * @see #putObjectVolatile(Object,long,Object)
  234. */
  235. public native void putObject(Object obj, long offset, Object value);
  236. /**
  237. * Retrieves the value of the object field at the specified offset in the
  238. * supplied object with volatile load semantics.
  239. *
  240. * @param obj the object containing the field to read.
  241. * @param offset the offset of the object field within <code>obj</code>.
  242. */
  243. public native Object getObjectVolatile(Object obj, long offset);
  244. /**
  245. * Returns the offset of the first element for a given array class.
  246. * To access elements of the array class, this value may be used along
  247. * with that returned by
  248. * <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
  249. * if non-zero.
  250. *
  251. * @param arrayClass the class for which the first element's address should
  252. * be obtained.
  253. * @return the offset of the first element of the array class.
  254. * @see arrayIndexScale(Class)
  255. */
  256. public native int arrayBaseOffset(Class arrayClass);
  257. /**
  258. * Returns the scale factor used for addressing elements of the supplied
  259. * array class. Where a suitable scale factor can not be returned (e.g.
  260. * for primitive types), zero should be returned. The returned value
  261. * can be used with
  262. * <a href="#arrayBaseOffset"><code>arrayBaseOffset</code></a>
  263. * to access elements of the class.
  264. *
  265. * @param arrayClass the class whose scale factor should be returned.
  266. * @return the scale factor, or zero if not supported for this array class.
  267. */
  268. public native int arrayIndexScale(Class arrayClass);
  269. /**
  270. * Releases the block on a thread created by
  271. * <a href="#park"><code>park</code></a>. This method can also be used
  272. * to terminate a blockage caused by a prior call to <code>park</code>.
  273. * This operation is unsafe, as the thread must be guaranteed to be
  274. * live. This is true of Java, but not native code.
  275. *
  276. * @param thread the thread to unblock.
  277. */
  278. public native void unpark(Thread thread);
  279. /**
  280. * Blocks the thread until a matching
  281. * <a href="#unpark"><code>unpark</code></a> occurs, the thread is
  282. * interrupted or the optional timeout expires. If an <code>unpark</code>
  283. * call has already occurred, this also counts. A timeout value of zero
  284. * is defined as no timeout. When <code>isAbsolute</code> is
  285. * <code>true</code>, the timeout is in milliseconds relative to the
  286. * epoch. Otherwise, the value is the number of nanoseconds which must
  287. * occur before timeout. This call may also return spuriously (i.e.
  288. * for no apparent reason).
  289. *
  290. * @param isAbsolute true if the timeout is specified in milliseconds from
  291. * the epoch.
  292. * @param time either the number of nanoseconds to wait, or a time in
  293. * milliseconds from the epoch to wait for.
  294. */
  295. public native void park(boolean isAbsolute, long time);
  296. }