natUnsafe.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // natUnsafe.cc - Implementation of sun.misc.Unsafe native methods.
  2. /* Copyright (C) 2006, 2007
  3. Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #include <gcj/cni.h>
  9. #include <gcj/field.h>
  10. #include <gcj/javaprims.h>
  11. #include <jvm.h>
  12. #include <sun/misc/Unsafe.h>
  13. #include <java/lang/System.h>
  14. #include <java/lang/InterruptedException.h>
  15. #include <java/lang/Thread.h>
  16. #include <java/lang/Long.h>
  17. #include "sysdep/locks.h"
  18. // Use a spinlock for multi-word accesses
  19. class spinlock
  20. {
  21. static volatile obj_addr_t lock;
  22. public:
  23. spinlock ()
  24. {
  25. while (! compare_and_swap (&lock, 0, 1))
  26. _Jv_ThreadYield ();
  27. }
  28. ~spinlock ()
  29. {
  30. release_set (&lock, 0);
  31. }
  32. };
  33. // This is a single lock that is used for all synchronized accesses if
  34. // the compiler can't generate inline compare-and-swap operations. In
  35. // most cases it'll never be used, but the i386 needs it for 64-bit
  36. // locked accesses and so does PPC32. It's worth building libgcj with
  37. // target=i486 (or above) to get the inlines.
  38. volatile obj_addr_t spinlock::lock;
  39. static inline bool
  40. compareAndSwap (volatile jint *addr, jint old, jint new_val)
  41. {
  42. jboolean result = false;
  43. spinlock lock;
  44. if ((result = (*addr == old)))
  45. *addr = new_val;
  46. return result;
  47. }
  48. static inline bool
  49. compareAndSwap (volatile jlong *addr, jlong old, jlong new_val)
  50. {
  51. jboolean result = false;
  52. spinlock lock;
  53. if ((result = (*addr == old)))
  54. *addr = new_val;
  55. return result;
  56. }
  57. static inline bool
  58. compareAndSwap (volatile jobject *addr, jobject old, jobject new_val)
  59. {
  60. jboolean result = false;
  61. spinlock lock;
  62. if ((result = (*addr == old)))
  63. *addr = new_val;
  64. return result;
  65. }
  66. jlong
  67. sun::misc::Unsafe::objectFieldOffset (::java::lang::reflect::Field *field)
  68. {
  69. _Jv_Field *fld = _Jv_FromReflectedField (field);
  70. // FIXME: what if it is not an instance field?
  71. return fld->getOffset();
  72. }
  73. jint
  74. sun::misc::Unsafe::arrayBaseOffset (jclass arrayClass)
  75. {
  76. // FIXME: assert that arrayClass is array.
  77. jclass eltClass = arrayClass->getComponentType();
  78. return (jint)(jlong) _Jv_GetArrayElementFromElementType (NULL, eltClass);
  79. }
  80. jint
  81. sun::misc::Unsafe::arrayIndexScale (jclass arrayClass)
  82. {
  83. // FIXME: assert that arrayClass is array.
  84. jclass eltClass = arrayClass->getComponentType();
  85. if (eltClass->isPrimitive())
  86. return eltClass->size();
  87. return sizeof (void *);
  88. }
  89. // These methods are used when the compiler fails to generate inline
  90. // versions of the compare-and-swap primitives.
  91. jboolean
  92. sun::misc::Unsafe::compareAndSwapInt (jobject obj, jlong offset,
  93. jint expect, jint update)
  94. {
  95. jint *addr = (jint *)((char *)obj + offset);
  96. return compareAndSwap (addr, expect, update);
  97. }
  98. jboolean
  99. sun::misc::Unsafe::compareAndSwapLong (jobject obj, jlong offset,
  100. jlong expect, jlong update)
  101. {
  102. volatile jlong *addr = (jlong*)((char *) obj + offset);
  103. return compareAndSwap (addr, expect, update);
  104. }
  105. jboolean
  106. sun::misc::Unsafe::compareAndSwapObject (jobject obj, jlong offset,
  107. jobject expect, jobject update)
  108. {
  109. jobject *addr = (jobject*)((char *) obj + offset);
  110. return compareAndSwap (addr, expect, update);
  111. }
  112. void
  113. sun::misc::Unsafe::putOrderedInt (jobject obj, jlong offset, jint value)
  114. {
  115. volatile jint *addr = (jint *) ((char *) obj + offset);
  116. *addr = value;
  117. }
  118. void
  119. sun::misc::Unsafe::putOrderedLong (jobject obj, jlong offset, jlong value)
  120. {
  121. volatile jlong *addr = (jlong *) ((char *) obj + offset);
  122. spinlock lock;
  123. *addr = value;
  124. }
  125. void
  126. sun::misc::Unsafe::putOrderedObject (jobject obj, jlong offset, jobject value)
  127. {
  128. volatile jobject *addr = (jobject *) ((char *) obj + offset);
  129. *addr = value;
  130. }
  131. void
  132. sun::misc::Unsafe::putIntVolatile (jobject obj, jlong offset, jint value)
  133. {
  134. write_barrier ();
  135. volatile jint *addr = (jint *) ((char *) obj + offset);
  136. *addr = value;
  137. }
  138. void
  139. sun::misc::Unsafe::putLongVolatile (jobject obj, jlong offset, jlong value)
  140. {
  141. volatile jlong *addr = (jlong *) ((char *) obj + offset);
  142. spinlock lock;
  143. *addr = value;
  144. }
  145. void
  146. sun::misc::Unsafe::putObjectVolatile (jobject obj, jlong offset, jobject value)
  147. {
  148. write_barrier ();
  149. volatile jobject *addr = (jobject *) ((char *) obj + offset);
  150. *addr = value;
  151. }
  152. #if 0 // FIXME
  153. void
  154. sun::misc::Unsafe::putInt (jobject obj, jlong offset, jint value)
  155. {
  156. jint *addr = (jint *) ((char *) obj + offset);
  157. *addr = value;
  158. }
  159. #endif
  160. void
  161. sun::misc::Unsafe::putLong (jobject obj, jlong offset, jlong value)
  162. {
  163. jlong *addr = (jlong *) ((char *) obj + offset);
  164. spinlock lock;
  165. *addr = value;
  166. }
  167. void
  168. sun::misc::Unsafe::putObject (jobject obj, jlong offset, jobject value)
  169. {
  170. jobject *addr = (jobject *) ((char *) obj + offset);
  171. *addr = value;
  172. }
  173. jint
  174. sun::misc::Unsafe::getIntVolatile (jobject obj, jlong offset)
  175. {
  176. volatile jint *addr = (jint *) ((char *) obj + offset);
  177. jint result = *addr;
  178. read_barrier ();
  179. return result;
  180. }
  181. jobject
  182. sun::misc::Unsafe::getObjectVolatile (jobject obj, jlong offset)
  183. {
  184. volatile jobject *addr = (jobject *) ((char *) obj + offset);
  185. jobject result = *addr;
  186. read_barrier ();
  187. return result;
  188. }
  189. jlong
  190. sun::misc::Unsafe::getLong (jobject obj, jlong offset)
  191. {
  192. jlong *addr = (jlong *) ((char *) obj + offset);
  193. spinlock lock;
  194. return *addr;
  195. }
  196. jlong
  197. sun::misc::Unsafe::getLongVolatile (jobject obj, jlong offset)
  198. {
  199. volatile jlong *addr = (jlong *) ((char *) obj + offset);
  200. spinlock lock;
  201. return *addr;
  202. }
  203. void
  204. sun::misc::Unsafe::unpark (::java::lang::Thread *thread)
  205. {
  206. natThread *nt = (natThread *) thread->data;
  207. nt->park_helper.unpark ();
  208. }
  209. void
  210. sun::misc::Unsafe::park (jboolean isAbsolute, jlong time)
  211. {
  212. using namespace ::java::lang;
  213. Thread *thread = Thread::currentThread();
  214. natThread *nt = (natThread *) thread->data;
  215. nt->park_helper.park (isAbsolute, time);
  216. }