cp-vmintegration.texinfo 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename cp-vmintegration.info
  4. @settitle GNU Classpath VM Integration Guide
  5. @c %**end of header
  6. @setchapternewpage off
  7. @ifinfo
  8. This file contains important information you will need to know if you
  9. are going to write an interface between GNU Classpath and a Virtual
  10. Machine.
  11. Copyright (C) 1998-2002, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  12. @ifnotplaintext
  13. @dircategory GNU Libraries
  14. @direntry
  15. * VM Integration: (cp-vmintegration). GNU Classpath VM Integration Guide
  16. @end direntry
  17. @end ifnotplaintext
  18. @end ifinfo
  19. @titlepage
  20. @title GNU Classpath VM Integration Guide
  21. @author John Keiser
  22. @author C. Brian Jones
  23. @author Mark Wielaard
  24. @page
  25. @vskip 0pt plus 1filll
  26. Copyright @copyright{} 1998-2002 Free Software Foundation, Inc.
  27. @sp 2
  28. Permission is granted to make and distribute verbatim copies of
  29. this document provided the copyright notice and this permission notice
  30. are preserved on all copies.
  31. Permission is granted to copy and distribute modified versions of this
  32. document under the conditions for verbatim copying, provided that the
  33. entire resulting derived work is distributed under the terms of a
  34. permission notice identical to this one.
  35. Permission is granted to copy and distribute translations of this manual
  36. into another language, under the above conditions for modified versions,
  37. except that this permission notice may be stated in a translation
  38. approved by the Free Software Foundation.
  39. @end titlepage
  40. @ifinfo
  41. @node Top, Introduction, (dir), (dir)
  42. @top GNU Classpath Hacker's Guide
  43. This file contains important information you will need to know if you
  44. are going to write an interface between GNU Classpath and a Virtual
  45. Machine.
  46. This document is incomplete, as we are still in alpha with the interface.
  47. @end ifinfo
  48. @menu
  49. * Introduction:: An introduction to the Classpath project
  50. * Initialization:: Initializing the classes
  51. * Classpath Hooks:: Hooks from Classpath to the VM
  52. * VM Hooks:: Hooks from the underlying VM to Classpath
  53. * JNI Implementation:: Hooking the VM to jni.h
  54. * JVMTI Implementation:: Hooking the VM to jvmti.h
  55. * Miscellaneous VM Requirements::
  56. @end menu
  57. @node Introduction, Initialization, Top, Top
  58. @comment node-name, next, previous, up
  59. @chapter Introduction
  60. The Classpath Project's ambition to be a 100% clean room implementation
  61. of the standard Java class libraries cannot be fulfilled without some
  62. level of integration with the Virtual Machine, the underlying machinery
  63. that actually runs Java.
  64. There are several VMs out there, here is a small list.
  65. @itemize @bullet
  66. @item @uref{http://www.hungry.com/old-hungry/products/japhar/,Japhar}
  67. Japhar was the first VM to use GNU Classpath. Today you can see that
  68. sort of relationship in the source tree which denotes several Japhar
  69. specific files as a reference implementation of those pieces. This VM
  70. has been primarily tested against Linux and lacks garbage collections, a
  71. JIT, and suffers recently from slow development.
  72. @item @uref{http://www.intel.com/research/mrl/orp/,Intel's Open Runtime Platform}
  73. Intel surprised us not long ago with the release of this rather advanced
  74. VM that uses GNU Classpath for a set of class libraries and works on
  75. Linux and Windows 2000. As of June, 2004, it does not appear that ORP
  76. is under active development.
  77. @item @uref{http://www.sablevm.org/,SableVM}
  78. SableVM is a robust, extremely portable, efficient, and
  79. specifications-compliant Java Virtual Machine that aims to be easy to
  80. maintain and to extend. It features a state-of-the-art, efficient
  81. interpreter engine. Its source code is very accessible and easy to
  82. understand, and has many robustness features that have been the object
  83. of careful design.
  84. @item @uref{http://www.kaffe.org,Kaffe}
  85. Kaffe is an advanced VM and together with its own class libraries
  86. provides a Java 1.1 compatible environment.
  87. @item @uref{http://www.mozilla.org/projects/ef,Electrical Fire}
  88. The Electrical File VM continues to be listed as a Mozilla project
  89. though development has been somewhat quiet. A number of concepts from
  90. EF were expected at one point to be rolled into Japhar, but that
  91. development has not occurred as of yet.
  92. @item @uref{http://latte.snu.ac.kr/,LaTTe}
  93. This VM project so far supports only Sun UltraSparc processors using the
  94. proprietary Solaris 2.5.1 or higher operating system. LaTTe was derived
  95. from Kaffe but claims a number of improvements.
  96. @item @uref{http://gcc.gnu.org/java/,GNU Compiler for Java (GCJ)}
  97. This is a portable, optimizing, ahead-of-time compiler for the Java
  98. Programming Language. It can compile Java source code directly to native
  99. machine code, Java source code to Java bytecode (class files), and Java
  100. bytecode to native machine code. Compiled applications are linked with the
  101. GCJ runtime, libgcj which is based on the GNU Classpath code, which provides
  102. the core class libraries, a garbage collector, and a bytecode interpreter.
  103. libgcj can dynamically load and interpret class files, resulting in mixed
  104. compiled/interpreted applications.
  105. GCJ is part of the GNU Compiler Collection (@uref{http://gcc.gnu.org/,GCC}).
  106. On March 6 2000 the libgcj and GNU Classpath projects were officially merged
  107. and there is active work on merging all the classes between the projects.
  108. Licensed under GPL+exception, just as GNU Classpath is.
  109. @item @uref{http://kissme.sourceforge.net/,Kissme}
  110. This is a free Java Virtual Machine that is being developed on GNU/Linux
  111. and can run console Java applications. Kissme also provides support for
  112. orthogonally persistent Java.
  113. @c I don't know what ``orthogonally persistent Java'' is, and I bet
  114. @c there are other people don't know either. -- Steve Augart, 4 June 2004
  115. @item @uref{http://jamvm.sourceforge.net/,JamVM}
  116. A simple, small bytecode interpreter that works out-of-the-box with
  117. pure GNU Classpath; it is emerging as the preferred platform for
  118. quickly testing a new build of GNU Classpath. Licensed under the GPL.
  119. @item @uref{http://jikesrvm.sourceforge.net/,Jikes RVM}
  120. A free runtime environment for Java, written in Java. Works
  121. out-of-the-box with pure GNU Classpath. Features an optimizing JIT.
  122. Runs on the x86 and PowerPC architectures, on the AIX, Linux, and Mac
  123. OS/X operating systems. Licensed under the CPL (Common Public
  124. License). Extensively documented. Actively developed as of June,
  125. 2004.
  126. @end itemize
  127. In the past integration efforts were focused mainly on Japhar with an eye
  128. towards getting Electrical Fire to work. Most information contained in
  129. this document is gleaned from these efforts. Recently more work has been
  130. done on getting gcj, orp and kissme to work out of the box with GNU Classpath
  131. but there is much to do before that becomes a reality.
  132. @node Initialization, Classpath Hooks, Introduction, Top
  133. @comment node-name, next, previous, up
  134. @chapter Initialization
  135. The order of initialization, as far as I can tell, doesn't matter just
  136. yet. However, when we move to 1.2 support, it probably will matter, so
  137. we'll have a note in here at that time.
  138. The initialization order is currently documented in the
  139. @file{Runtime.java} source file.
  140. @node Classpath Hooks, VM Hooks, Initialization, Top
  141. @comment node-name, next, previous, up
  142. @chapter Classpath Hooks
  143. The primary method of interaction between Classpath and the VM is via
  144. the helper classes, which are named after the relevant core library
  145. class, but include an additional `VM' prefix. The library classes from
  146. Classpath call out to these to get certain VM-specific dirty work done.
  147. A reference copy of each VM class exists. The majority consist of a
  148. series of static methods, some of which are simply declared
  149. @code{native}, and some which provide a default implementation. VMs may
  150. either use these as is, or create their own local variations. When
  151. using the default implementations, the VM is responsible for
  152. implementing any of the code marked as @code{native} which corresponds
  153. to functionality they wish their VM to provide. When using their own
  154. versions of the classes, VM implementors may choose to change the mix of
  155. native and non-native methods from that below, so as to best suit their
  156. implementation.
  157. @menu
  158. * java.lang::
  159. * gnu.classpath::
  160. * java.util::
  161. * java.io::
  162. * java.security::
  163. * java.net::
  164. * java.nio::
  165. * java.nio.channels::
  166. * gnu.java.nio::
  167. * java.lang.reflect::
  168. * gnu.java.lang::
  169. * gnu.java.lang.management::
  170. * java.lang.management::
  171. * Classpath Callbacks::
  172. @end menu
  173. @node java.lang, gnu.classpath, Classpath Hooks, Classpath Hooks
  174. @comment node-name, next, previous, up
  175. @section @code{java.lang}
  176. @code{java.lang} is the core Java package, being imported automatically by all
  177. classes. It includes basic classes as @code{Object} and @code{String}.
  178. A VM must implement at least some parts of this package in order to
  179. become operable.
  180. @menu
  181. * java.lang.VMClass::
  182. * java.lang.VMObject::
  183. * java.lang.VMClassLoader::
  184. * java.lang.VMSystem::
  185. * java.lang.VMThrowable::
  186. * java.lang.VMCompiler::
  187. * java.lang.VMDouble::
  188. * java.lang.VMFloat::
  189. * java.lang.VMProcess::
  190. * java.lang.VMRuntime::
  191. * java.lang.VMString::
  192. * java.lang.VMThread::
  193. * java.lang.VMMath::
  194. @end menu
  195. @node java.lang.VMClass, java.lang.VMObject ,java.lang,java.lang
  196. @subsection @code{java.lang.VMClass}
  197. The core class, @code{java.lang.Class}, and the corresponding VM class,
  198. @code{java.lang.VMClass}, provide two main functions within GNU Classpath.
  199. @enumerate
  200. @item For basic VM operation, @code{java.lang.Class} provides the link between
  201. the Java-based representation of a class it embodies and the VM's own
  202. internal structure for a class. @xref{VM Hooks}.
  203. @item As far as the user is concerned, the main function of
  204. @code{java.lang.Class} is as an entry point to the reflection
  205. facilities, and so it also provides this functionality, backed by the
  206. VM class.
  207. @end enumerate
  208. This VM class lists the following methods, organized by the version of the
  209. Java specification in which they occur. All are @code{native}, unless
  210. otherwise specified, and pertain to reflection. As a result, the VM only
  211. needs to implement these methods in order to provide reflection support,
  212. and then only to the degree required.
  213. @itemize @bullet
  214. @item 1.0
  215. @itemize @bullet
  216. @item @code{isInterface(Class)} -- This is simply a property test, and matches
  217. the presence of an appropriate flag within the class file.
  218. @item @code{getName(Class)} -- Returns the fully-qualified name of the class.
  219. @item @code{getSuperclass(Class)} -- Returns a @code{Class} instance which
  220. represents the superclass. Again, the class file contains an element directly
  221. relating to this. @code{null} is returned for primitives, interfaces and
  222. @code{Object}.
  223. @item @code{getInterfaces(Class)} -- Same as the above, but the implemented
  224. or extended interfaces rather than the superclass. An empty array should
  225. be returned, rather than @code{null}.
  226. @item @code{getDeclaredClasses(Class,boolean)} -- Returns the internal classes
  227. this instance declares directly. The flag determines whether or not the
  228. VM should filter out non-public classes.
  229. @item @code{getDeclaredFields(Class,boolean)} -- The same for fields.
  230. @item @code{getDeclaredMethods(Class,boolean)} -- And for methods.
  231. @item @code{getDeclaredConstructors(Class,boolean)} -- And constructors.
  232. @item @code{getClassLoader(Class)} -- Returns the @code{ClassLoader} instance
  233. which is responsible for the specified class.
  234. @item @code{forName(String, boolean, ClassLoader)} -- The VM should create a
  235. @code{Class} instance corresponding to the named class. As noted in
  236. @ref{VM Hooks}, the internal content of the instance is the
  237. responsibility of the VM@. The supplied class loader is recorded as that
  238. which loaded the class, and the boolean specifies whether or not to
  239. run the class initializer.
  240. @item @code{isArray(Class)} -- Another property test, corresponding to a
  241. class file flag.
  242. @item @code{initialize(Class)} -- The VM should initialize the class fully,
  243. if it has not already done so.
  244. @item @code{loadArrayClass(String,ClassLoader)} -- This is called if
  245. @code{forName} returns @code{null} and the string specifies an array class.
  246. The specified array class should be loaded with the supplied class loader.
  247. @item @code{throwException(Throwable)} -- The VM should throw the supplied
  248. checked exception, without declaring it.
  249. @end itemize
  250. @item 1.1
  251. @itemize @bullet
  252. @item @code{isInstance(Class,Object)} -- This is a reflection-based equivalent
  253. of the @code{instanceof} operator.
  254. @item @code{isAssignableFrom(Class,Class)} -- Mainly a shorthand for the above,
  255. removing the need to create an instance to test assignability.
  256. @item @code{isPrimitive(Class)} -- Returns true if this class is simply
  257. a representation of one of the primitive types: @code{boolean}, @code{byte},
  258. @code{char}, @code{short}, @code{int}, @code{long}, @code{float},
  259. @code{double} and @code{void}.
  260. @item @code{getComponentType(Class)} -- Produces a @code{Class} instance which
  261. represents the type of the members of the array the class instance represents.
  262. Classes which don't represent an array type return @code{null}.
  263. @item @code{getModifiers(Class,boolean)} -- Returns an integer which encodes
  264. the class' modifiers, such as @code{public}. Again, this relates to
  265. information stored in the class file.
  266. @item @code{getDeclaringClass(Class)} -- Returns the class that declared
  267. an inner or member class, or @code{null} if the instance refers to a top-level
  268. class.
  269. @end itemize
  270. @item 1.5
  271. @itemize @bullet
  272. @item @code{isSynthetic(Class)} -- Returns true if the flags for this class
  273. mark it as synthetic.
  274. @item @code{isAnnotation(Class)} -- Returns true if the flags for this class
  275. mark it as an annotation.
  276. @item @code{isEnum(Class)} -- Returns true if the flags for this class
  277. mark it as an enumeration.
  278. @item @code{getSimpleName(Class)} -- Returns the simple name of the class.
  279. A default implementation is provided, but a more efficient version may instead
  280. be provided by the VM.
  281. @item @code{getCanonicalName(Class)} -- Returns the canonical name of the
  282. class. A default implementation is provided, but a more efficient
  283. version may instead be provided by the VM.
  284. @item @code{getEnclosingClass(Class)} -- Returns the immediately enclosing
  285. class (null for a top-level class).
  286. @item @code{getEnclosingConstructor(Class)} -- Returns the constructor
  287. which immediately encloses the supplied class.
  288. @item @code{getEnclosingMethod(Class)} -- Returns the method
  289. which immediately encloses the supplied class.
  290. @item @code{getClassSignature(Class)} -- Returns the generic signature of
  291. the class or null if there isn't one.
  292. @item @code{isAnonymousClass(Class)} -- Returns true if the class is an
  293. anonymous class.
  294. @item @code{isLocalClass(Class)} -- Returns true if the class is an
  295. local class.
  296. @item @code{isMemberClass(Class)} -- Returns true if the class is an
  297. member class.
  298. @end itemize
  299. @end itemize
  300. @node java.lang.VMObject, java.lang.VMClassLoader, java.lang.VMClass, java.lang
  301. @subsection @code{java.lang.VMObject}
  302. @code{VMObject} is the bridge between the low level @code{Object}
  303. facilities such as making a clone, getting the class of the object and
  304. the wait/notify semantics. This is accomplished using the following
  305. @code{native} methods.
  306. @itemize @bullet
  307. @item @code{getClass(Object)} -- Returns the @code{Class} instance for the
  308. object. @code{Class} objects are produced by the VM, as described in
  309. @ref{VM Hooks}.
  310. @item @code{clone(Cloneable)} -- The VM should produce a low-level clone of the
  311. specified object, creating a field-by-field shallow copy of the original.
  312. The only difference between the two is that the new object should still be
  313. @code{finalizable}, even if the original is not.
  314. @item @code{notify(Object)} -- The VM should choose one of the threads waiting
  315. for a lock on the specified object arbitrarily, and wake it. If the current
  316. thread does not currently hold the lock on the object, then an
  317. @code{IllegalMonitorStateException} should be thrown.
  318. @item @code{notifyAll(Object)} -- Same as the above, but all threads are
  319. awakened.
  320. @item @code{wait(Object,long,int)} -- The VM should set the current thread
  321. into a waiting state, which persists until it receives a notify signal or the
  322. specified time (in milliseconds and nanoseconds) is exceeded. The nanoseconds
  323. restriction may be ignored if such granularity is not available, and a
  324. @code{IllegalMonitorStateException} should be thrown if the current thread
  325. doesn't own the object.
  326. @end itemize
  327. @node java.lang.VMClassLoader, java.lang.VMSystem, java.lang.VMObject, java.lang
  328. @subsection @code{java.lang.VMClassLoader}
  329. @code{VMClassLoader} provides methods for defining and resolving core and
  330. primitive classes, as well as handling resources, packages and assertions.
  331. The class is a mixture of @code{native} methods and Java-based
  332. implementations, with some of the latter being @emph{stubs}.
  333. @itemize @bullet
  334. @item Native Methods
  335. @itemize @bullet
  336. @item @code{defineClass(ClassLoader,String,byte[],int,int,ProtectionDomain)}
  337. -- The VM should create a @code{Class} instance from the supplied byte array.
  338. @item @code{resolveClass(Class)} -- Resolve references to other classes in the
  339. supplied class.
  340. @item @code{loadClass(name,boolean)} -- Load a class using the bootstrap
  341. loader.
  342. @item @code{getPrimitiveClass(char)} -- The VM should provide a @code{Class}
  343. implementation for one of the primitive classes. The supplied character
  344. matches the JNI code for the primitive class e.g.@: `B' for byte and
  345. `Z' for boolean.
  346. @end itemize
  347. @item Java Methods
  348. @itemize @bullet
  349. @item @code{getResource(String)} -- The default implementation calls
  350. @code{getResources} and returns the first element in the returned enumeration,
  351. or @code{null} if there are no elements.
  352. @item @code{getResources(String)} -- By default, this compiles a list of
  353. URLs via the boot class path. Any matching files within a zip file are added,
  354. and directories on the boot class path are automatically converted to file
  355. URLs that refer to join the directory with the resource name (whether or not
  356. it actually exists).
  357. @item @code{getPackage(String)} -- Always returns null, which may be suitable
  358. if the VM does not wish to return a @code{Package} implementation. Otherwise,
  359. it may be necessary to make this a @code{native} method.
  360. @item @code{getPackages()} -- As with the last, a default stub implementation
  361. exists (returning an empty array) which may be replaced if support is
  362. required.
  363. @item @code{defaultAssertionStatus()} -- A stub which can be implemented
  364. by VMs providing assertion support. At present, it always returns @code{true}.
  365. @item @code{packageAssertionStatus()} -- Much the same status as the above.
  366. The method should return a map converting package names to boolean status
  367. values. The stub implementation provides an empty map.
  368. @item @code{classAssertionStatus()} -- Same as the last, but for classes.
  369. @item @code{getSystemClassLoader()} -- The default calls @code{ClassLoader}
  370. to create a new auxiliary class loader with a system and extension class
  371. loader. The VM may wish to replace it if it wishes to supply its own custom
  372. system class loader.
  373. @end itemize
  374. @end itemize
  375. @node java.lang.VMSystem, java.lang.VMThrowable, java.lang.VMClassLoader, java.lang
  376. @subsection @code{java.lang.VMSystem}
  377. @code{VMSystem} handles the default I/O streams, provides access to the
  378. system clock and environment variables and provides methods for
  379. @code{System.arraycopy} and the @code{identityHashCode} of an
  380. @code{Object}. It consists of @code{native} methods, but the default
  381. implementation also provides some helper methods to simplify stream
  382. creation.
  383. @itemize @bullet
  384. @item Native Methods
  385. @itemize @bullet
  386. @item @code{arraycopy(Object,int,Object,int,int)} -- The VM should copy
  387. a specified number of array objects from one array to another, with
  388. appropriate checks for compatible typing, available elements and space.
  389. The VM should be able to perform this more efficiently using native code
  390. and direct memory manipulation than would have been achieved by using Java.
  391. @item @code{identityHashCode(Object)} -- This is the hashcode for
  392. @code{Object}, which relates to the actual location of the object in memory.
  393. @item @code{setIn(InputStream)} -- Set the system input stream.
  394. @item @code{setOut(PrintStream)} -- Set the system output stream.
  395. @item @code{setErr(PrintStream)} -- Set the system error stream.
  396. @item @code{currentTimeMillis()} -- Gets the system time in milliseconds.
  397. @item @code{getenv(String)} -- Returns the value of the specified environment
  398. variable.
  399. @item @code{getenv()} -- Returns a list of `name=value' pairs which correspond
  400. to the environment variables.
  401. @end itemize
  402. @item Java Methods
  403. @itemize @bullet
  404. @item @code{makeStandardInputStream()} -- Helps provide the functionality of
  405. @code{System.in} by wrapping the appropriate file descriptor in a
  406. buffered file input stream. VMs may choose to create the stream from
  407. the descriptor differently rather than using this method.
  408. @item @code{makeStandardOutputStream()} -- Helps provide the functionality of
  409. @code{System.out} by wrapping the appropriate file descriptor in a buffered
  410. file output stream. VMs may choose to create the stream from the descriptor
  411. differently rather than using this method.
  412. @item @code{makeStandardErrorStream()} -- Helps provide the functionality of
  413. @code{System.err} by wrapping the appropriate file descriptor in a buffered
  414. file output stream. VMs may choose to create the stream from the descriptor
  415. differently rather than using this method.
  416. @end itemize
  417. @end itemize
  418. Classpath also provides native implementations of
  419. @itemize @bullet
  420. @item @code{setIn(InputStream)}
  421. @item @code{setOut(PrintStream)}
  422. @item @code{setErr(PrintStream)}
  423. @item @code{currentTimeMillis()}
  424. @item @code{getenv(String)}
  425. @end itemize
  426. making a VM implementation optional.
  427. @node java.lang.VMThrowable, java.lang.VMCompiler, java.lang.VMSystem, java.lang
  428. @subsection @code{java.lang.VMThrowable}
  429. @code{VMThrowable} is used to hold the VM state of a throwable, created either
  430. when a @code{Throwable} is created or the @code{fillInStackTrace()} method is
  431. called (i.e., when the actual stack trace is needed, as a lot of exceptions are
  432. never actually used). The actual class has two @code{native} methods,
  433. one (@code{fillInStackTrace()}) being a method of the class used to obtain
  434. instances, and the other an instance method, @code{getStackTrace()}.
  435. @itemize @bullet
  436. @item @code{fillInStackTrace(Throwable)} -- The VM should return the current
  437. execution state of the @code{Throwable} in the form of a @code{VMThrowable}
  438. instance. The VM may also return @code{null} if it does not support this
  439. functionality.
  440. @item @code{getStackTrace()} -- This is used to create a real
  441. @code{StackTraceElement} array for the exception, using the state data
  442. stored during creation of the instance.
  443. @end itemize
  444. @node java.lang.VMCompiler, java.lang.VMDouble, java.lang.VMThrowable, java.lang
  445. @subsection @code{java.lang.VMCompiler}
  446. @code{VMCompiler} provides an interface for VMs which wish to provide
  447. JIT compilation support. The default implementation is simply a series
  448. of stubs. The property, @code{java.compiler}, should point to a library
  449. containing the function @code{java_lang_Compiler_start()} if such support
  450. is to be provided.
  451. @itemize @bullet
  452. @item @code{compileClass(Class)} -- Invoke the compiler to compile the specific
  453. class, returning @code{true} if successful.
  454. @item @code{compileClasses(String)} -- The compiler should compile the classes
  455. matching the specified string, again returning @code{true} on success.
  456. @item @code{command(Object)} -- The object represents a command given to the
  457. compiler, and is specific to the compiler implementation.
  458. @item @code{enable} -- Enable the operation of the compiler.
  459. @item @code{disable} -- Disable compiler operation.
  460. @end itemize
  461. @node java.lang.VMDouble, java.lang.VMFloat, java.lang.VMCompiler, java.lang
  462. @subsection @code{java.lang.VMDouble}
  463. @code{VMDouble} provides native support for the conversion and parsing
  464. of doubles.
  465. @itemize @bullet
  466. @item @code{doubleToRawLongBits(double)} -- Converts the double to the IEEE 754
  467. bit layout, preserving NaNs.
  468. @item @code{longBitsToDouble(long)} -- This is the inverse of the last method,
  469. preserving NaNs so that the output of one can be fed into the other without
  470. data loss.
  471. @item @code{toString(double,boolean)} -- Converts the double to a string,
  472. giving a shorter value if the flag @code{isFloat} is @code{true}, indicating
  473. that the conversion was requested by @code{java.lang.Float} rather than
  474. @code{java.lang.Double}.
  475. @item @code{initIDs} -- Used by JNI-based solutions to initialize the cache
  476. of the static field IDs. The default @code{VMDouble} implementation has a
  477. static initializer which loads the JNI library and calls this method.
  478. @item @code{parseDouble} -- Turn the string into a usable double value.
  479. @end itemize
  480. Classpath provides native implementations of all these, making VM
  481. implementation optional.
  482. @node java.lang.VMFloat, java.lang.VMProcess, java.lang.VMDouble, java.lang
  483. @subsection @code{java.lang.VMFloat}
  484. @code{VMFloat} provides native support for the conversion of floats.
  485. @itemize @bullet
  486. @item @code{floatToRawIntBits(float)} -- Converts the float to the IEEE 754
  487. bit layout, preserving NaNs.
  488. @item @code{intBitsToFloat(int)} -- This is the inverse of the last method,
  489. preserving NaNs so that the output of one can be fed into the other without
  490. data loss.
  491. @end itemize
  492. Classpath provides native implementations of all these, making VM
  493. implementation optional.
  494. @node java.lang.VMProcess, java.lang.VMRuntime, java.lang.VMFloat, java.lang
  495. @subsection @code{java.lang.VMProcess}
  496. @code{VMProcess} handles the execution of external processes. In the
  497. default implementation, threads are spawned and reaped by @code{ProcessThread}.
  498. A constructor creates a new @code{VMProcess}, which extends rather than
  499. complements @code{Process}, using an array of arguments, an array of
  500. environment variables and a working directory. The instance maintains
  501. system input, output and error streams linked to the external process.
  502. Three @code{native} methods are used, and implementations are provided
  503. for all three by Classpath, making VM implementation optional. These use
  504. the POSIX functions, @code{fork()}, @code{waitpid()} and @code{kill()}.
  505. @itemize @bullet
  506. @item @code{nativeSpawn(String[],String[],File,boolean)} -- The VM should
  507. create a new process which uses the specified command-line arguments,
  508. environment variables and working directory. Unlike the other two
  509. methods, this method is linked to an instance, and must call
  510. @code{setProcessInfo()} with the results before returning. The
  511. boolean argument maps to the @code{redirectErrorStream} property of
  512. @code{java.lang.ProcessBuilder}. When true, the output and error streams
  513. are merged.
  514. @item @code{nativeReap()} -- This is called to perform a reap of any
  515. zombie processes, and should not block, instead returning a boolean as to
  516. whether reaping actually took place.
  517. @item @code{nativeKill(long)} -- The VM should terminate the specified PID.
  518. @end itemize
  519. @node java.lang.VMRuntime, java.lang.VMString, java.lang.VMProcess, java.lang
  520. @subsection @code{java.lang.VMRuntime}
  521. The @code{VMRuntime} class provides a series of native methods
  522. which divulge information about the runtime or invoke certain
  523. operations. This includes retrieving the amount of available memory,
  524. and scheduling the garbage collector. There are two exceptions: the
  525. @code{enableShutdownHooks} method, which allows the VM to put in its own
  526. shutdown hooks when @code{Runtime.addShutdownHook()} is first invoked,
  527. and @code{exec(String[],String[],File)} which spawns an external process.
  528. These are Java-based static methods instead. The first is simply a stub by
  529. default, while the second simply links to the functionality of
  530. @code{VMProcess} (and should be changed if a different @code{Process}
  531. implementation is used).
  532. @itemize @bullet
  533. @item @code{availableProcessors()} -- Returns the number of processors
  534. available to the VM.
  535. @item @code{freeMemory()} -- Returns the amount of memory the VM has available
  536. on the heap for allocating.
  537. @item @code{totalMemory()} -- Returns the size of the heap.
  538. @item @code{maxMemory()} -- Returns the maximum memory block the VM will
  539. attempt to allocate. May be simply @code{Long.MAX_VALUE} (8 exabytes!)
  540. @item @code{gc()} -- Allows users to explicitly invoke the garbage collector.
  541. This is a suggestion to the VM, rather than a command, and the garbage
  542. collector should run anyway @emph{without} it being invoked.
  543. @item @code{runFinalization()} -- Like the above, but related to the
  544. finalilzation of objects rather than the garbage collector.
  545. @item @code{runFinalizationForExit()} -- Called immediately prior to VM
  546. shutdown in order to finalize all objects (including `live' ones)
  547. @item @code{traceInstructions(boolean)} -- This turns on and off the optional
  548. VM functionality of printing a trace of executed bytecode instructions.
  549. @item @code{traceMethodCalls(boolean)} -- This turns on and off the optional
  550. VM functionality of printing a trace of methods called.
  551. @item @code{runFinalizersOnExit(boolean)} -- A toggleable setting for
  552. running the finalization process at exit.
  553. @item @code{exit(int)} -- The VM should shutdown with the specified exit code.
  554. @item @code{nativeLoad(String,ClassLoader)} -- Attempts to load a file,
  555. returning an integer which is non-zero for success. Nothing happens if the
  556. file has already been loaded.
  557. @item @code{mapLibraryName(String)} -- The VM should map the system-independent
  558. library name supplied to the platform-dependent equivalent (e.g.@: a @code{.so}
  559. or @code{.dll} file)
  560. @end itemize
  561. @node java.lang.VMString, java.lang.VMThread, java.lang.VMRuntime, java.lang
  562. @subsection @code{java.lang.VMString}
  563. @code{VMString} is responsible for handling interned strings. If two strings
  564. are equal (using the @code{equals()} method), then the results of calling
  565. the @code{intern()} method on each of them makes them equal
  566. (using @code{==}). Thus, the same string object is always returned by
  567. @code{intern} if the two strings are equal. The default implementation
  568. is Java-based and implements @code{intern(String)} by maintaining a
  569. @code{WeakHashMap} which links the strings to their @code{WeakReference}.
  570. A new mapping is created for each new string being @code{intern}ed.
  571. A VM may implement this differently by implementing this method,
  572. which is @code{static} and the only one in @code{VMString}.
  573. @node java.lang.VMThread, java.lang.VMMath, java.lang.VMString, java.lang
  574. @subsection @code{java.lang.VMThread}
  575. @code{VMThread} provides the link between Java's threads and the platform
  576. threading support. A @code{VMThread} is created via a private constructor
  577. and linked to a @code{Thread} instance. This occurs when the @code{Thread}
  578. instance is started by the static @code{create(Thread,long)} method (the second
  579. argument requests a certain stack size, usually zero). The thread itself is
  580. executed via the @code{run()} method, which handles any problems with the
  581. running of the thread and its eventual death.
  582. @code{VMThread} provides the following accessors and mutators for accessing
  583. the thread state via @code{VMThread},
  584. @itemize @bullet
  585. @item @code{getName()}
  586. @item @code{setName(String)}
  587. @item @code{getPriority()}
  588. @item @code{setPriotity(int)}
  589. @item @code{isDaemon()}
  590. @end itemize
  591. all of which refer to the @code{Thread} instance. @code{setPriority(int)} also
  592. calls the appropriate native method. @code{stop(Throwable)} similarly wraps
  593. a native method, merely adding in a check for the state of the thread.
  594. The default implementation also provides Java-based implementations of
  595. @code{join(long,int)}, @code{sleep(long,int)} and
  596. @code{holdsLock(Object)}. @code{join} and @code{sleep} simply wait for
  597. the appropriate amount of time, with @code{join} additionally waiting
  598. for the thread instance to become @code{null}. @code{holdsLock} simply
  599. checks if an object is locked by the current thread by trying to invoke
  600. the @code{notify} method, and catching the failing exception if this is
  601. not the case.
  602. The remainder of the class is a series of @code{native} methods, some of
  603. which are mandatory for VM implementation and others which provide optional
  604. or deprecated functionality.
  605. @itemize @bullet
  606. @item Mandatory Instance Methods
  607. @itemize @bullet
  608. @item @code{start(long)} -- The VM should create the native thread and start
  609. it running using the @code{run} method of the @code{VMThread} instance on
  610. which this method is called.
  611. @item @code{interrupt()} -- The VM should interrupt the running thread and
  612. throw an appropriate exception.
  613. @item @code{isInterrupted()} -- Checks the interrupted state of the thread.
  614. @item @code{suspend()} -- The thread should be suspended until resumed.
  615. @item @code{resume()} -- The thread should be resumed from its suspended state.
  616. This pair of methods are deprecated, due to the possibility of a deadlock
  617. occurring when a thread with locks is suspended.
  618. @item @code{nativeSetPriority(int)} -- Called by @code{setPriority}
  619. to allow the setting to flow down to the native thread.
  620. @item @code{nativeStop(Throwable)} -- The VM should stop the thread abnormally
  621. and throw the specified exception. This is clearly deprecated, due to the
  622. ambiguous state an abruptly-stopped thread may leave.
  623. @item @code{getState()} -- Returns the VM's impression of the current state
  624. of the thread. The applicable states are supplied by the @code{State}
  625. enumeration in @code{java.lang.Thread}.
  626. @end itemize
  627. @item Mandatory Class Methods
  628. @itemize @bullet
  629. @item @code{currentThread()} -- Return a reference to the thread currently
  630. being executed.
  631. @item @code{yield()} -- The VM should allow some other thread to run.
  632. The current thread maintains its locks even though it stops executing for
  633. the time being.
  634. @item @code{interrupted()} -- A shortcut to obtaining the interrupted state
  635. of the current thread.
  636. @end itemize
  637. @item Other Methods
  638. @itemize @bullet
  639. @item @code{countStackFrames()} -- Returns a count of the number of stack
  640. frames in the thread. This depends on the deprecated method @code{suspend()}
  641. having returned true, and is thus deprecated as a result.
  642. @end itemize
  643. @end itemize
  644. @node java.lang.VMMath,, java.lang.VMThread, java.lang
  645. @subsection @code{java.lang.VMMath}
  646. The @code{VMMath} class provides a series of native methods
  647. for some of the mathematical functions present in @code{java.lang.Math}.
  648. Classpath provides a default implementation of these which maps the
  649. functions to those provided by @code{fdlibm}. VM implementors are welcome
  650. to replace this with more efficient implementations, as long as the accuracy
  651. contract of these methods, specified in @code{java.lang.Math}, is maintained.
  652. @itemize @bullet
  653. @item 1.0
  654. @itemize @bullet
  655. @item @code{sin(double)} -- Returns the sine value for the given angle.
  656. @item @code{cos(double)} -- Returns the cosine value for the given angle.
  657. @item @code{tan(double)} -- Returns the tangent value for the given angle.
  658. @item @code{asin(double)} -- Returns the arc sine value for the given angle.
  659. @item @code{acos(double)} -- Returns the arc cosine value for the given angle.
  660. @item @code{atan(double)} -- Returns the arc tangent value for the given angle.
  661. @item @code{atan2(double,double)} -- Returns the arc tangent of the ratio of
  662. the two arguments.
  663. @item @code{exp(double)} -- Returns the exponent raised to the given power.
  664. @item @code{log(double)} -- Returns the natural logarithm for the given value.
  665. @item @code{sqrt(double)} -- Returns the square root of the value.
  666. @item @code{pow(double,double)} -- Returns x to the power of y.
  667. @item @code{IEEEremainder(double,double)} -- Returns the IEEE 754 remainder
  668. for the two values.
  669. @item @code{ceil(double)} -- Returns the nearest integer >= the value.
  670. @item @code{floor(double)} -- Returns the nearest integer <= the value.
  671. @item @code{rint(double)} -- Returns the nearest integer or the even one
  672. if the distance between the two is equal.
  673. @end itemize
  674. @item 1.5
  675. @itemize @bullet
  676. @item @code{cbrt(double)} -- Returns the cube root of the value.
  677. @item @code{cosh(double)} -- Returns the hyperbolic cosine value for the given
  678. angle.
  679. @item @code{expm1(double)} -- Returns the exponent of the value minus one.
  680. @item @code{hypot(double,double)} -- Returns the hypotenuse corresponding to
  681. x and y.
  682. @item @code{log10(double)} -- Returns the base 10 logarithm of the given value.
  683. @item @code{log1p(double)} -- Returns the natural logarithm of the value plus
  684. one.
  685. @item @code{sinh(double)} -- Returns the hyperbolic sine value for the given
  686. angle.
  687. @item @code{tanh(double)} -- Returns the hyperbolic tangent value for the given angle.
  688. @end itemize
  689. @end itemize
  690. @node gnu.classpath, java.util, java.lang, Classpath Hooks
  691. @section @code{gnu.classpath}
  692. The @code{gnu.classpath} package provides Classpath-specific functionality,
  693. primarily relating to the features in @code{java.lang}. At present, this
  694. includes the context of a class (the stack) and the system properties.
  695. @menu
  696. * gnu.classpath.VMStackWalker::
  697. * gnu.classpath.VMSystemProperties::
  698. * gnu.classpath.Unsafe::
  699. @end menu
  700. @node gnu.classpath.VMStackWalker,gnu.classpath.VMSystemProperties,gnu.classpath,gnu.classpath
  701. @subsection @code{gnu.classpath.VMStackWalker}
  702. @code{VMStackWalker} provides access to the class context or stack. The
  703. default implementation consists of a @code{native} @code{static} method,
  704. @code{getClassContext()}, which obtains the class context, and two helper
  705. methods which obtain the calling class (the 3rd element in the context array)
  706. and its class loader, respectively.
  707. @itemize @bullet
  708. @item @code{getClassContext()} -- The VM should return an array of
  709. @code{Class} objects, each of which relates to the method currently being
  710. executed at that point on the stack. Thus, the first item (index 0) is the
  711. class that contains this method.
  712. @item @code{getCallingClass()} -- A Java-based helper method which returns
  713. the @code{Class} object which contains the method that called the method
  714. accessing @code{getCallingClass()}.
  715. @item @code{getCallingClassLoader()} -- Like the last, but returning the class
  716. loader of the class.
  717. @end itemize
  718. @node gnu.classpath.VMSystemProperties,gnu.classpath.Unsafe,gnu.classpath.VMStackWalker,gnu.classpath
  719. @subsection @code{gnu.classpath.VMSystemProperties}
  720. @code{VMSystemProperties} allows the VM to hook into the property creation
  721. process, both before and after the system properties are added by GNU
  722. Classpath. The default implementation assumes that the VM will add its
  723. properties first, by making the pre-initialisation method @code{native},
  724. and that the Classpath properties may then be altered by a Java-based
  725. post-initialisation method.
  726. As these methods are called as part of the bootstrap process, caution should
  727. be used as to what classes are used, and properties should only be set
  728. using @code{Properties.setProperty()}. Specifically, I/O classes should be
  729. avoided at this early stage.
  730. @itemize @bullet
  731. @item @code{preInit(Properties)} -- Allows the VM to add properties
  732. @emph{before} the Classpath properties are added. The default implementation
  733. includes a full list of properties that @emph{must} be added by the VM, but
  734. additional VM-specific ones may also be added.
  735. @item @code{postInit(Properties)} -- Same as the last, but called after the
  736. Classpath properties have been added. The main purpose of this is to allow
  737. the VM to alter the properties added by GNU Classpath to suit it.
  738. @end itemize
  739. @node gnu.classpath.Unsafe,,gnu.classpath.VMSystemProperties,gnu.classpath
  740. @subsection @code{gnu.classpath.Unsafe}
  741. The @code{Unsafe} class provides access to some low-level unsafe operations
  742. as required by the addition of the java.util.concurrent classes. These
  743. focus on direct memory access to the fields within the VM and providing
  744. atomic update methods.
  745. @itemize @bullet
  746. @item @code{objectFieldOffset(Field)} -- Provides the caller with the memory
  747. offset of a particular field.
  748. @item @code{compareAndSwap*(Object,long,*,*)} -- One of these methods is
  749. provided for each of int, long and Object (hence the *s). The value of
  750. a field pointed to by the given Object and offset is compared with the
  751. first value and replaced with the second if they are the same. The reason
  752. for this method is to make this change operation atomic.
  753. @item @code{put/get*(Object,long,*)} -- These are like the last set of
  754. methods, handling integers, longs and Objects, but the field is always
  755. changed on a put. Different methods are provided for different semantics.
  756. Ordered variants perform a lazy put, in that the change does not
  757. immediately propogate to other threads, while the others provide
  758. volatile or 'normal' semantics.
  759. @item @code{arrayBaseOffset(Class)} and @code{arrayIndexScale(Class)} --
  760. These two methods allow an array class to be traversed by pointer
  761. arithmetic, by gaining the address of the first element and then
  762. scaling appropriately for the later ones.
  763. @item @code{park(boolean,long)} and @code{unpark(Thread)} -- These methods
  764. block and unblock threads respectively, with an optional timeout being
  765. provided for the blocking. @code{unpark} is unsafe as the thread may have
  766. been destroyed by native code.
  767. @end itemize
  768. @node java.util, java.io, gnu.classpath, Classpath Hooks
  769. @section java.util
  770. The @code{java.util} VM hooks provide links between the mix of functionality
  771. present in that package, which includes collections, date and time handling
  772. and parsing. At present, there is only one hook, which connects GNU Classpath
  773. to the timezone information provided by the underlying platform.
  774. @menu
  775. * java.util.VMTimeZone::
  776. @end menu
  777. @node java.util.VMTimeZone,,java.util,java.util
  778. @subsection @code{java.util.VMTimeZone}
  779. @code{VMTimeZone} joins @code{TimeZone} to the platform timezone information
  780. via the static method, @code{getDefaultTimeZoneId()}. The VM hook is
  781. expected to return a @code{TimeZone} instance that represents the current
  782. timezone in use by the platform. The default implementation provides
  783. this functionality for POSIX or GNU-like systems, and VMs that want this
  784. functionality can keep this implementation and implement the native
  785. method, @code{getSystemTimeZoneId()}. This method is only called when
  786. obtaining the timezone name from the @code{TZ} environment variable,
  787. @code{/etc/timezone} and @code{/etc/localtime} all fail. This fallback
  788. mechanism also means that a system which doesn't provide the above three
  789. methods, but does provide a timezone in string form, can still use this
  790. implementation.
  791. @node java.io, java.security, java.util, Classpath Hooks
  792. @section java.io
  793. The @code{java.io} package is heavily reliant on access to the I/O facilities
  794. of the underlying platform. As far as its VM hooks go, they provide two
  795. areas of functionality to GNU Classpath, these being
  796. @itemize @bullet
  797. @item File and directory queries and manipulation
  798. @item Serialization of objects
  799. @end itemize
  800. The first corresponds directly to most of the @code{File} class, while
  801. the latter underlies the functionality provided by the
  802. @code{ObjectInputStream} and @code{ObjectOutputStream}. More low-level I/O
  803. is provided by @ref{java.nio}.
  804. @menu
  805. * java.io.VMFile::
  806. * java.io.VMObjectInputStream::
  807. * java.io.VMObjectStreamClass::
  808. @end menu
  809. @node java.io.VMFile,java.io.VMObjectInputStream,java.io,java.io
  810. @subsection @code{java.io.VMFile}
  811. @code{VMFile} allows GNU Classpath's @code{File} representations to
  812. probe and modify the file system using the native functions of the
  813. platform. The default implementation (which consists of both a
  814. @code{VMFile} class and the native methods) is primarily UNIX-centric,
  815. working with POSIX functions and assuming case-sensitive filenames,
  816. without the restriction of the 8.3 format. It consists mainly of
  817. @code{static} @code{native} methods, with a few Java helper methods.
  818. The native methods represent the file as a string containing its path,
  819. rather than using the object itself.
  820. @itemize @bullet
  821. @item Native Methods
  822. @itemize @bullet
  823. @item @code{lastModified(String)} -- The native method should return a
  824. @code{long} value that represents the last modified date of the file.
  825. @item @code{setReadOnly(String)} -- Sets the file's permissions to read only,
  826. in whichever way this is realised by the platform.
  827. @item @code{create(String)} -- Create the named file.
  828. @item @code{list(String)} -- The native method opens the named directory,
  829. reads the contents and returns them as a Java @code{String} array.
  830. @item @code{renameTo(String,String)} -- Renames the first file to the second.
  831. @item @code{length(String)} -- Returns a @code{long} value representing
  832. the file size.
  833. @item @code{exists(String)} -- Tests for the existence of the named file
  834. or directory.
  835. @item @code{delete(String)} -- Deletes the file or directory.
  836. @item @code{setLastModified(String,long)} -- Change the last modified time.
  837. @item @code{mkdir(String)} -- Creates the named directory.
  838. @item @code{isFile(String)} -- Tests that the named path references a file.
  839. @item @code{canWrite(String)} -- Tests that the file can be written to.
  840. This method is @code{synchronized}, so the object is locked during the check.
  841. @item @code{canRead(String)} -- Complement of the last method.
  842. @item @code{isDirectory(String)} -- Tests that the named path references
  843. a directory.
  844. @end itemize
  845. @item Java Helper Methods
  846. @itemize @bullet
  847. @item @code{canWriteDirectory(File)} -- Checks that the directory can be
  848. written to, by trying to create a temporary file in it.
  849. @item @code{listRoots()} -- Returns the root of a GNU filesystem, i.e.@: `/'
  850. in an array.
  851. @item @code{isHidden(String)} -- Checks whether the file starts with `.',
  852. which is how files are hidden on UNIX-style systems.
  853. @item @code{getName(String)} -- Pulls the actual filename from the end of
  854. the path, by breaking off the characters after the last occurrence of the
  855. platform's file separator.
  856. @item @code{getCanonicalForm(String)} -- This converts a UNIX path to
  857. its canonical form by removing the `.' and `..' sections that occur within.
  858. @end itemize
  859. @end itemize
  860. @node java.io.VMObjectInputStream,java.io.VMObjectStreamClass,java.io.VMFile,java.io
  861. @subsection @code{java.io.VMObjectInputStream}
  862. This class consists of two methods which provide functionality used in
  863. deserializing an object. @code{currentClassLoader()} provides the first
  864. user-defined class loader from the class context
  865. (@xref{gnu.classpath.VMStackWalker},) via a @code{PrivilegedAction}.
  866. @code{allocateObject(Class,Class,Constructor)} is a @code{native} method
  867. (a reference implementation is provided) which creates an object but
  868. calls the constructor of another class, which is a superclass of the
  869. object's class.
  870. @node java.io.VMObjectStreamClass,,java.io.VMObjectInputStream,java.io
  871. @subsection @code{java.io.VMObjectStreamClass}
  872. @code{VMObjectStreamClass} is a series of @code{static} @code{native}
  873. methods that provide some of the groundwork for @code{ObjectStreamClass}
  874. and @code{ObjectStreamField}. @code{hasClassInitializer(Class)} works
  875. with the former, and checks for the presence of a static initializer.
  876. The remaining methods are of the form @code{setXXXNative(Field,Object,XXX)}
  877. and support @code{ObjectStreamField}. One exists for each of the main types
  878. (boolean, float, double, long, int, short, char, byte and object) and is used
  879. to set the specified field in the supplied instance to the given value.
  880. A default implementation is provided for all of them, so a VM implementation
  881. is optional.
  882. @node java.security, java.net, java.io, Classpath Hooks
  883. @section java.security
  884. The @code{java.security} package provides support for Java's security
  885. architecture.
  886. @menu
  887. * java.security.VMAccessController::
  888. * java.security.VMSecureRandom::
  889. @end menu
  890. @node java.security.VMAccessController,java.security.VMSecureRandom,java.security,java.security
  891. @subsection @code{java.security.VMAccessController}
  892. The @code{AccessController} is used to perform privileged actions. Its
  893. hook class, @code{VMAccessController}, maintains the
  894. @code{AccessControlContext} and the default implementation is purely
  895. Java-based. The VM may choose to replace this with their own.
  896. The methods in the reference version are as follows:
  897. @itemize @bullet
  898. @item @code{pushContext(AccessControlContext)} -- Adds a new context to the
  899. stack for the current thread. This is called before a privileged action
  900. takes place.
  901. @item @code{popContext()} -- Removes the top context from the stack. This
  902. is performed after the privileged action takes place.
  903. @item @code{getContext()} -- Either derives a context based on the
  904. @code{ProtectionDomain}s of the call stack (see the next method) or returns
  905. the top of the context stack.
  906. @item @code{getStack()} -- Provides access to the call stack as a pair of
  907. arrays of classes and method names. The actual implementation returns
  908. an empty array, indicating that there are no permissions.
  909. @end itemize
  910. @node java.security.VMSecureRandom,,java.security.VMAccessController,java.security
  911. @subsection @code{java.security.VMSecureRandom}
  912. The @code{VMSecureRandom} class is used to provide access to
  913. cryptographically secure random numbers. The default implementation
  914. of the class runs eight threads that increment counters in a tight
  915. loop, and XORs each counter to produce one byte of seed data. This is
  916. not very efficient, and is not guaranteed to be random (the thread
  917. scheduler is probably deterministic, after all). VM implementors
  918. should provide a version of this class, which implements the method
  919. @code{generateSeed(byte[],int,int)}, so that it fills the buffer using
  920. a random seed from a system facility, such as a system entropy
  921. gathering device or hardware random number generator. The parameters
  922. are the usual set of buffer, offset and length and the method returns
  923. the number of bytes actually generated, which may be less than that
  924. requested.
  925. @node java.net, java.nio, java.security, Classpath Hooks
  926. @section java.net
  927. The @code{java.net} package is heavily reliant on access to the networking
  928. facilities of the underlying platform. The VM hooks provide information
  929. about the available network interfaces, and access to lookup facilities
  930. for network addresses.
  931. @menu
  932. * java.net.VMInetAddress::
  933. * java.net.VMNetworkInterface::
  934. @end menu
  935. @node java.net.VMInetAddress,java.net.VMNetworkInterface,java.net,java.net
  936. @subsection @code{java.net.VMInetAddress}
  937. @code{VMInetAddress} is a series of @code{static} @code{native} methods
  938. which provide access to the platform's lookup facilities. All the methods
  939. are implemented by GNU Classpath, making VM implementation optional, and
  940. are as follows:
  941. @itemize @bullet
  942. @item @code{getLocalHostname()} -- Wraps the @code{gethostname} function, and
  943. falls back on `localhost'.
  944. @item @code{lookupInaddrAny()} -- Returns the value of @code{INADDR_ANY}.
  945. @item @code{getHostByAddr(byte[])} -- Looks up the hostname based on an IP
  946. address.
  947. @item @code{getHostByName(String)} -- The reverse of the last method, it
  948. returns the IP addresses which the given host name resolves to.
  949. @end itemize
  950. @node java.net.VMNetworkInterface,,java.net.VMInetAddress,java.net
  951. @subsection @code{java.net.VMNetworkInterface}
  952. @code{VMNetworkInterface} currently consists of a single @code{static}
  953. @code{native} method, @code{getInterfaces()}, which retrieves the
  954. network interfaces available on the underlying platform as a @code{Vector}.
  955. The current GNU Classpath implementation is a native stub.
  956. @node java.nio, java.nio.channels, java.net, Classpath Hooks
  957. @section java.nio
  958. The @code{java.nio} package is part of the New I/O framework added in
  959. Java 1.4. This splits I/O into the concepts of @emph{buffers},
  960. @emph{charsets}, @emph{channels} and @emph{selectors}, and
  961. @code{java.nio} defines the buffer classes. As far as native and VM
  962. code is concerned, the new package needs support for low-level efficient
  963. buffer operations.
  964. @menu
  965. * java.nio.VMDirectByteBuffer::
  966. @end menu
  967. @node java.nio.VMDirectByteBuffer,,java.nio,java.nio
  968. @subsection @code{java.nio.VMDirectByteBuffer}
  969. A @code{ByteBuffer} maintains a buffer of bytes, and allows it to be
  970. manipulated using primitive operations such as @code{get}, @code{put},
  971. @code{allocate} and @code{free}. A direct buffer avoids intermediate
  972. copying, and uses native data which shouldn't be manipulated by a
  973. garbage collector. The VM class consists of @code{static} @code{native}
  974. methods, all of which are given default implementations by GNU
  975. Classpath.
  976. @itemize @bullet
  977. @item @code{init()} -- Creates an instance of an appropriate
  978. @code{gnu.classpath.RawData} class. This class is not garbage
  979. collected, is created natively and is used in the other methods to reference
  980. the buffered data.
  981. @item @code{allocate(int)} -- Allocates the memory for the buffer using
  982. @code{malloc} and returns a reference to the @code{RawData} class.
  983. @item @code{free(RawData)} -- Frees the memory used by the buffer.
  984. @item @code{get(RawData,int)} -- Returns the data at the specified index.
  985. @item @code{get(RawData,int,byte[],int,int)} -- Copies a section of the
  986. data into a byte array using @code{memcpy}.
  987. @item @code{put(RawData,int,byte)} -- Puts the given data in the buffer
  988. at the specified index.
  989. @item @code{adjustAddress(RawData,int)} -- Adjusts the pointer into the buffer.
  990. @item @code{shiftDown(RawData,int,int,int)} -- Moves the content of the buffer
  991. at an offset down to a new offset using @code{memmove}.
  992. @end itemize
  993. @node java.nio.channels, gnu.java.nio, java.nio, Classpath Hooks
  994. @section java.nio.channels
  995. Channels provide the data for the buffers with the New I/O packages.
  996. For example, a channel may wrap a file or a socket. The VM hooks,
  997. at the moment, simply allow the channels to be accessed by @code{java.io}
  998. streams.
  999. @menu
  1000. * java.nio.channels.VMChannels::
  1001. @end menu
  1002. @node java.nio.channels.VMChannels,,java.nio.channels,java.nio.channels
  1003. @subsection @code{java.nio.channels.VMChannels}
  1004. @code{VMChannels} provides the methods that create the channels or
  1005. streams. The default implementation is in pure Java and simply wraps
  1006. the channels in standard I/O classes from @code{java.io}.
  1007. @itemize @bullet
  1008. @item @code{createStream(Class,Channel)} -- Creates a @code{FileChannel}
  1009. which wraps an instance of the specified stream class, created by reflection.
  1010. This method is private, and is used by the other two.
  1011. @item @code{newInputStream(ReadableByteChannel)} -- Wraps the channel
  1012. in a @code{FileInputStream}.
  1013. @item @code{newOutputStream(WritableByteChannel)} -- Wraps the channel
  1014. in a @code{FileOutputStream}.
  1015. @end itemize
  1016. @node gnu.java.nio, java.lang.reflect, java.nio.channels, Classpath Hooks
  1017. @section gnu.java.nio
  1018. The @code{gnu.java.nio} class provides Classpath implementations of the
  1019. interfaces provided by @code{java.nio}. The VM classes provide the native
  1020. support necessary to implement @emph{pipes} and @emph{selectors}.
  1021. @menu
  1022. * gnu.java.nio.VMPipe::
  1023. * gnu.java.nio.VMSelector::
  1024. @end menu
  1025. @node gnu.java.nio.VMPipe,gnu.java.nio.VMSelector,gnu.java.nio,gnu.java.nio
  1026. @subsection @code{gnu.java.nio.VMPipe}
  1027. @code{VMPipe} provides the native functionality for a uni-directional pipe
  1028. between a source and a destination (sink) channel. It consists of one
  1029. @code{static} @code{native} method, @code{init(PipeImpl,SelectorProvider)},
  1030. the reference implementation of which is currently a native stub. Ideally,
  1031. this should initialise the pipe at the native level.
  1032. @node gnu.java.nio.VMSelector,,gnu.java.nio.VMPipe,gnu.java.nio
  1033. @subsection @code{gnu.java.nio.VMSelector}
  1034. A @code{Selector} selects between multiple @code{SelectableChannel}s based
  1035. on their readiness and a key set. The VM hook for the Classpath implementation
  1036. of this is @code{VMSelector}, and this allows the actual @code{select()}
  1037. operation to be performed. This is represented by the @code{static}
  1038. @code{native} method, @code{select(int[],int[],int[],long)}, and a default
  1039. implementation of this is provided.
  1040. @node java.lang.reflect, gnu.java.lang, gnu.java.nio, Classpath Hooks
  1041. @section @code{java.lang.reflect}
  1042. @code{java.lang.reflect} provides the interface to Java's reflection
  1043. facilities. Via reflection, programmers can obtain type information about
  1044. a particular instance at runtime or dynamically create new instances.
  1045. @menu
  1046. * java.lang.reflect.VMArray::
  1047. @end menu
  1048. @node java.lang.reflect.VMArray,,,java.lang.reflect
  1049. @subsection @code{java.lang.reflect.VMArray}
  1050. The @code{VMArray} class provides a hook, @code{createObjectArray},
  1051. which the VM uses to generate a new non-primitive array of a
  1052. particular class and size. The default implementation simply passes
  1053. the job down to the standard JNI function, @code{NewObjectArray}.
  1054. @node gnu.java.lang, gnu.java.lang.management, java.lang.reflect, Classpath Hooks
  1055. @section @code{gnu.java.lang}
  1056. @code{gnu.java.lang} provides VM interfaces for the GNU
  1057. implementations of features in java.lang. Currently, this includes the
  1058. implementation of instrumentation.
  1059. @menu
  1060. * gnu.java.lang.VMInstrumentationImpl::
  1061. @end menu
  1062. @node gnu.java.lang.VMInstrumentationImpl,,,gnu.java.lang
  1063. @subsection @code{gnu.java.lang.VMInstrumentationImpl}
  1064. The @code{gnu.java.lang.VMInstrumentationImpl} and
  1065. @code{gnu.java.lang.InstrumentationImpl} classes provide an implementation of the
  1066. @code{java.lang.instrument.Instrument} interface.
  1067. A @code{InstrumentationImpl} object should be created by the VM when agents
  1068. are given in the command line (see the @code{java.lang.instrument} package
  1069. documentation). The VM has to set the static field
  1070. @code{VMClassLoader.instrumenter} to this object. The VM should implement the
  1071. static native methods of the @code{VMInstrumentationImpl} class.
  1072. @itemize @bullet
  1073. @item @code{isRedefineClassesSupported()} -- Returns true if the JVM supports
  1074. class redefinition.
  1075. @item @code{redefineClasses()} -- Gives a set of classes with new bytecodes.
  1076. The VM must redefine the classes by reading the new bytecodes.
  1077. @item @code{getAllLoadedClass()} -- Returns an array of all loaded classes.
  1078. @item @code{getInitiatedClass()} -- Returns an array of all classes loaded
  1079. by a specific class loader.
  1080. @item @code{getObjectSize()} -- Gives the size of an object.
  1081. @end itemize
  1082. Instrumentation allows to modify the bytecode of a class before it gets read
  1083. by the VM@. In GNU Classpath, the @code{ClassLoader.defineClass} method calls
  1084. the @code{VMClassLoader.defineClassWithTransformers} method which first checks
  1085. if @code{VMClassLoader.instrumenter} is @code{null}. If it's the case, it
  1086. directly calls @code{VMClassLoader.defineClass}. If it's not the case, the
  1087. method calls at first the @code{InstrumentationImpl.callTransformers} method,
  1088. which calls each transformer registered to the @code{InstrumentationImpl}
  1089. object and returns a new bytecode array. Then, it calls the
  1090. @code{VMClassLoader.defineClass} method with this new bytecode array.
  1091. The second use of instrumentation is to redefine a class after it has been
  1092. loaded by the VM@. This is done in the Java application by calling the
  1093. @code{Instrumentation.redefineClasses} method of the standard interface on
  1094. a @code{Instrumentation} object. The @code{InstrumentationImpl.redefineClasses}
  1095. method calls the @code{VMInstrumentationImpl.redefineClasses} native method
  1096. which must be implemented by the VM@. The implementation should call the
  1097. @code{InstrumentationImpl.callTransformers} method.
  1098. @node gnu.java.lang.management, java.lang.management, gnu.java.lang, Classpath Hooks
  1099. @section @code{gnu.java.lang.management}
  1100. @code{gnu.java.lang.management} provides the VM interfaces for the GNU
  1101. implementations of the management beans.
  1102. @menu
  1103. * gnu.java.lang.management.VMRuntimeMXBeanImpl::
  1104. * gnu.java.lang.management.VMClassLoadingMXBeanImpl::
  1105. * gnu.java.lang.management.VMThreadMXBeanImpl::
  1106. * gnu.java.lang.management.VMMemoryMXBeanImpl::
  1107. * gnu.java.lang.management.VMCompilationMXBeanImpl::
  1108. * gnu.java.lang.management.VMMemoryPoolMXBeanImpl::
  1109. * gnu.java.lang.management.VMMemoryManagerMXBeanImpl::
  1110. * gnu.java.lang.management.VMGarbageCollectorMXBeanImpl::
  1111. @end menu
  1112. @node gnu.java.lang.management.VMRuntimeMXBeanImpl,gnu.java.lang.management.VMClassLoadingMXBeanImpl,,gnu.java.lang.management
  1113. @subsection @code{gnu.java.lang.management.VMRuntimeMXBeanImpl}
  1114. The @code{gnu.java.lang.management.RuntimeMXBeanImpl} provides an
  1115. implementation of the @code{java.lang.management.RuntimeMXBean} interface,
  1116. and is supported by VM functionality in the form of
  1117. @code{gnu.java.lang.management.VMRuntimeMXBeanImpl}. This provides a
  1118. series of methods, which should be implemented by the virtual machine
  1119. in order to provide the required information for the bean. The VM
  1120. methods are generally representative of information that is only
  1121. available from the virtual machine, such as the command-line arguments
  1122. it was given at startup.
  1123. The methods are as follows:
  1124. @itemize @bullet
  1125. @item @code{(getInputArguments())} -- The VM should supply
  1126. a @code{String} array containing each of the command-line
  1127. arguments, excluding those that are directed at the
  1128. @code{main()} method. The reference implementation expects
  1129. this to be a native method.
  1130. @item @code{(getName())} -- The VM developer should choose
  1131. an appropriate name for the virtual machine. This name can
  1132. be instance-specific e.g.@: it can include things like the
  1133. process identifier or host name of the machine, which only
  1134. apply to the current running instance. Thus, the intention is
  1135. that this name refers to the entity that the other information
  1136. refers to, rather than the VM in general. The reference
  1137. implementation supplies a default concatenation of the VM
  1138. name and version.
  1139. @item @code{(getStartTime())} -- This should return the number
  1140. of milliseconds at which the virtual machine was started.
  1141. The uptime property of the bean is provided relative to this
  1142. value. Again, the reference implementation also expects
  1143. this method to be native.
  1144. @end itemize
  1145. The virtual machine also needs to provide either the
  1146. @code{sun.boot.class.path} or @code{java.boot.class.path}
  1147. property in order to support the optional boot class path
  1148. retrieval functionality.
  1149. @node gnu.java.lang.management.VMClassLoadingMXBeanImpl,gnu.java.lang.management.VMThreadMXBeanImpl,gnu.java.lang.management.VMRuntimeMXBeanImpl,gnu.java.lang.management
  1150. @subsection @code{gnu.java.lang.management.VMClassLoadingMXBeanImpl}
  1151. The @code{gnu.java.lang.management.ClassLoadingMXBeanImpl} provides an
  1152. implementation of the @code{java.lang.management.ClassLoadingMXBean} interface,
  1153. and is supported by VM functionality in the form of
  1154. @code{gnu.java.lang.management.VMClassLoadingMXBeanImpl}. This provides a
  1155. series of methods, which should be implemented by the virtual machine
  1156. in order to provide the required information for the bean. Implementing
  1157. this bean requires the VM to monitor when classes are loaded and unloaded,
  1158. and provide the option of verbose class loading output.
  1159. The methods are as follows:
  1160. @itemize @bullet
  1161. @item @code{(getLoadedClassCount())} -- This should return
  1162. the number of classes that are currently loaded by the VM.
  1163. @item @code{(getUnloadedClassCount())} -- This should return
  1164. the number of classes that have been loaded by the VM, but
  1165. have since been unloaded.
  1166. @item @code{(isVerbose())} -- This should return @code{true}
  1167. or @code{false}, depending on whether verbose class loading
  1168. output is turned or not, respectively.
  1169. @item @code{(setVerbose(boolean))} -- This should allow the
  1170. verbose class loading output to be turned on and off.
  1171. @end itemize
  1172. @node gnu.java.lang.management.VMThreadMXBeanImpl,gnu.java.lang.management.VMMemoryMXBeanImpl,gnu.java.lang.management.VMClassLoadingMXBeanImpl,gnu.java.lang.management
  1173. @subsection @code{gnu.java.lang.management.VMThreadMXBeanImpl}
  1174. The @code{gnu.java.lang.management.ThreadMXBeanImpl} provides an
  1175. implementation of the @code{java.lang.management.ThreadMXBean} interface,
  1176. and is supported by VM functionality in the form of
  1177. @code{gnu.java.lang.management.VMThreadMXBeanImpl}. This provides a
  1178. series of methods, which should be implemented by the virtual machine
  1179. in order to provide the required information for the bean. Implementing
  1180. this bean requires the VM to monitor thread-related statistics such as
  1181. how often the blocked and waiting states have been entered, as well as
  1182. additional optional support for time and contention monitoring.
  1183. Optional support is determined by the following properties:
  1184. @itemize @bullet
  1185. @item @code{gnu.java.lang.management.CurrentThreadTimeSupport} --
  1186. This property should be present if the VM supports monitoring the
  1187. time used by the current thread. If time monitoring for all threads
  1188. is supported, this need not be provided.
  1189. @item @code{gnu.java.lang.management.ThreadTimeSupport} --
  1190. This property should be present if the VM supports monitoring the
  1191. time used by all threads.
  1192. @item @code{gnu.java.lang.management.ThreadContentionSupport} --
  1193. This property should be present if the VM supports thread contention
  1194. monitoring.
  1195. @item @code{gnu.java.lang.management.MonitorUsageMonitoringSupport} --
  1196. This property should be present if the VM supports the monitoring
  1197. of object monitor usage.
  1198. @item @code{gnu.java.lang.management.OwnableSynchronizerUsageMonitoringSupport} --
  1199. This property should be present if the VM supports the monitoring
  1200. of ownable synchronizer usage.
  1201. @end itemize
  1202. In addition, the property
  1203. @code{gnu.java.lang.management.ThreadTimeInitallyEnabled} may be
  1204. set to the @code{String} value, @code{"true"}, if time monitoring
  1205. is enabled at startup.
  1206. The methods are as follows:
  1207. @itemize @bullet
  1208. @item @code{(findDeadlockedThreads())} -- This should return
  1209. an array of thread identifiers which match threads involved in
  1210. deadlock cycles (where each thread is waiting to obtain a lock
  1211. held by one of the others) on object monitors or ownable
  1212. synchronizers. This is specified as a native method in the
  1213. reference implementation, and is optional. It is only called
  1214. when the VM supports ownable synchronizer monitoring.
  1215. @item @code{(findMonitorDeadlockedThreads())} -- This should return
  1216. an array of thread identifiers which match threads involved in
  1217. deadlock cycles (where each thread is waiting to obtain a lock
  1218. held by one of the others) on object monitors. This is specified
  1219. as a native method in the reference implementation.
  1220. @item @code{(getAllThreads())} -- This should return an array of
  1221. all live threads and set the @code{filled} variable to the number
  1222. found. A default implementation is provided.
  1223. @item @code{(getAllThreadIds())} -- This should return an array of
  1224. all live thread identifiers. An implementation is provided against
  1225. @code{getAllThreads()} by default.
  1226. @item @code{(getCurrentThreadCpuTime())} -- This should return the
  1227. approximate number of nanoseconds of CPU time the current thread
  1228. has used. This is an optional native method, which is used by VMs
  1229. supporting time monitoring.
  1230. @item @code{(getCurrentThreadUserTime())} -- This should return the
  1231. approximate number of nanoseconds of user time the current thread
  1232. has used. This is an optional native method, which is used by VMs
  1233. supporting time monitoring.
  1234. @item @code{(getDaemonThreadCount())} -- This should return the number
  1235. of live daemon threads. A default implementation is provided, based
  1236. on @code{getAllThreads()}.
  1237. @item @code{(getLockInfo(ThreadInfo))} -- This is an optional native
  1238. method called when the VM supports ownable synchronizer usage monitoring
  1239. and the user has requested information for a particular thread. The
  1240. supplied @code{ThreadInfo} object should be filled out with an
  1241. array of @code{LockInfo} objects, providing details on each lock.
  1242. @item @code{(getMonitorInfo(ThreadInfo))} -- This is an optional native
  1243. method called when the VM supports object monitor usage monitoring
  1244. and the user has requested information for a particular thread. The
  1245. supplied @code{ThreadInfo} object should be filled out with an
  1246. array of @code{MonitorInfo} objects, providing details on each lock.
  1247. @item @code{(getPeakThreadCount())} -- The VM should maintain a record
  1248. of the peak number of live threads, and return it when this method is
  1249. called. This is specified as a native method in the reference
  1250. implementation.
  1251. @item @code{(resetPeakThreadCount())} -- This should reset the record
  1252. of the peak number of live threads to the current number of live
  1253. threads. This is specified as a native method in the reference
  1254. implementation.
  1255. @item @code{(getThreadCount())} -- This should return the number of
  1256. live threads. A default implementation is provided, based on
  1257. @code{getAllThreads()}.
  1258. @item @code{(getThreadCpuTime(long))} -- This should return the
  1259. approximate number of nanoseconds of CPU time the specified thread
  1260. has used. This is an optional native method, which is used by VMs
  1261. supporting time monitoring.
  1262. @item @code{(getThreadUserTime(long))} -- This should return the
  1263. approximate number of nanoseconds of CPU time the specified thread
  1264. has used. This is an optional native method, which is used by VMs
  1265. supporting time monitoring.
  1266. @item @code{(getThreadInfoForId(long, int))} -- This return an instance
  1267. of @code{java.lang.management.ThreadInfo} for the specified thread.
  1268. The class includes a private constructor which VMs should use to initialise
  1269. it with the appropriate values for the thread. The second argument
  1270. given here specifies the depth of the stack trace supplied on construction
  1271. of the instance. Special values are 0 (return an empty array) and
  1272. @code{Integer.MAX_VALUE} (return the maximum depth possible). This
  1273. is specified as a native method in the reference implementation.
  1274. @item @code{(getTotalStartedThreadCount())} -- This should return the
  1275. total number of threads that have been started by the VM, including ones
  1276. that have died. This is specified as a native method in the reference
  1277. implementation.
  1278. @end itemize
  1279. @node gnu.java.lang.management.VMMemoryMXBeanImpl,gnu.java.lang.management.VMCompilationMXBeanImpl,gnu.java.lang.management.VMThreadMXBeanImpl,gnu.java.lang.management
  1280. @subsection @code{gnu.java.lang.management.VMMemoryMXBeanImpl}
  1281. The @code{gnu.java.lang.management.MemoryMXBeanImpl} provides an
  1282. implementation of the @code{java.lang.management.MemoryMXBean} interface,
  1283. and is supported by VM functionality in the form of
  1284. @code{gnu.java.lang.management.VMMemoryMXBeanImpl}. This provides a
  1285. series of methods, which should be implemented by the virtual machine
  1286. in order to provide the required information for the bean. Implementing
  1287. this bean requires the VM to monitor the levels of heap and non-heap
  1288. memory, and provide the number of objects which are eligible for garbage
  1289. collection.
  1290. The methods are as follows:
  1291. @itemize @bullet
  1292. @item @code{(getHeapMemoryUsage())} -- This should return
  1293. an instance of @code{java.lang.management.MemoryUsage} with
  1294. values pertaining to the heap. A default implementation is
  1295. provided, based on @code{java.lang.Runtime}'s methods.
  1296. @item @code{(getNonHeapMemoryUsage())} -- This should return
  1297. an instance of @code{java.lang.management.MemoryUsage} with
  1298. values pertaining to non-heap memory.
  1299. @item @code{(getObjectPendingFinalizationCount())} -- Returns
  1300. the number of objects which are no longer referenced, and which
  1301. will thus be garbage collected on the next run of the garbage
  1302. collector.
  1303. @item @code{(isVerbose())} -- This should return @code{true}
  1304. or @code{false}, depending on whether verbose memory management
  1305. output is turned or not, respectively.
  1306. @item @code{(setVerbose(boolean))} -- This should allow the
  1307. verbose memory management output to be turned on and off.
  1308. @end itemize
  1309. @node gnu.java.lang.management.VMCompilationMXBeanImpl,gnu.java.lang.management.VMMemoryPoolMXBeanImpl,gnu.java.lang.management.VMMemoryMXBeanImpl,gnu.java.lang.management
  1310. @subsection @code{gnu.java.lang.management.VMCompilationMXBeanImpl}
  1311. The @code{gnu.java.lang.management.CompilationMXBeanImpl} provides an
  1312. implementation of the optional @code{java.lang.management.CompilationMXBean}
  1313. interface, and is supported by VM functionality in the form of
  1314. @code{gnu.java.lang.management.VMCompilationMXBeanImpl}. This provides a
  1315. single method for returning the number of milliseconds the virtual
  1316. machine's Just-In-Time (JIT) compiler has spent compiling. Even if
  1317. a JIT compiler is available and an instance of the bean supplied, this
  1318. method is still optional.
  1319. Optional support is determined by the following properties:
  1320. @itemize @bullet
  1321. @item @code{gnu.java.lang.compiler.name} -- This property should
  1322. specify the name of the JIT compiler. Classpath also uses this,
  1323. within @code{java.lang.management.ManagementFactory}, to determine
  1324. whether a bean should be created. If this property is set to a
  1325. non-null value, a bean will be created and its @code{getName()}
  1326. method will return this value.
  1327. @item @code{gnu.java.lang.management.CompilationTimeSupport} --
  1328. This property should be present if the VM supports monitoring the
  1329. time spent compiling.
  1330. @end itemize
  1331. Time support is implemented by the following method:
  1332. @itemize @bullet
  1333. @item @code{(getTotalCompilationTime())} -- This should return the
  1334. number of milliseconds the JIT compiler has spent compiling.
  1335. @end itemize
  1336. @node gnu.java.lang.management.VMMemoryPoolMXBeanImpl,gnu.java.lang.management.VMMemoryManagerMXBeanImpl,gnu.java.lang.management.VMCompilationMXBeanImpl,gnu.java.lang.management
  1337. @subsection @code{gnu.java.lang.management.VMMemoryPoolMXBeanImpl}
  1338. The @code{gnu.java.lang.management.MemoryPoolMXBeanImpl} provides an
  1339. implementation of the optional @code{java.lang.management.MemoryPoolMXBean}
  1340. interface, and is supported by VM functionality in the form of
  1341. @code{gnu.java.lang.management.VMMemoryPoolMXBeanImpl}. Providing
  1342. this interface requires implementing a number of methods for each supported
  1343. pool. These return statistics on memory usage, and, optionally, allows
  1344. monitoring of when memory usage exceeds a preset threshold.
  1345. Optional support is determined by the following properties:
  1346. @itemize @bullet
  1347. @item @code{gnu.java.lang.management.CollectionUsageThresholdSupport} --
  1348. This property should be present if the VM supports setting a collection
  1349. usage threshold and monitoring when it is matched or exceeded. Collection
  1350. usage thresholds are related to the remaining memory usage following a
  1351. garbage collection cycle.
  1352. @item @code{gnu.java.lang.management.UsageThresholdSupport} --
  1353. This property should be present if the VM supports setting a
  1354. usage threshold and monitoring when it is matched or exceeded.
  1355. @end itemize
  1356. The methods are as follows (all take a pool name as their
  1357. first parameter):
  1358. @itemize @bullet
  1359. @item @code{(getCollectionUsage(String))} -- Returns a
  1360. @code{java.lang.management.MemoryUsage} object, containing the
  1361. memory usage statistics following a garbage collection cycle
  1362. for the specified pool. This may also return @code{null} if
  1363. the pool isn't an appropriate pool for this particular task.
  1364. @item @code{(getCollectionUsageThreshold(String))} -- Returns
  1365. the pool's collection usage threshold, if supported.
  1366. @item @code{(getCollectionUsageThresholdCount(String))} -- Returns
  1367. the number of times the specified pool has matched or exceeded
  1368. its collection usage threshold, if supported.
  1369. @item @code{(getMemoryManagerNames(String))} -- Returns a list
  1370. of names of memory managers which manage the specified pool.
  1371. @item @code{(getPeakUsage(String))} -- Returns a
  1372. @code{java.lang.management.MemoryUsage} object for the peak
  1373. usage level of the specified pool.
  1374. @item @code{(getType(String))} -- Returns a string containing
  1375. either @code{"HEAP"} or @code{"NON_HEAP"} which indicates the type of
  1376. memory used by the specified pool.
  1377. @item @code{(getUsage(String))} -- Returns a
  1378. @code{java.lang.management.MemoryUsage} object for the current
  1379. usage level of the specified pool.
  1380. @item @code{(getUsageThreshold(String))} -- Returns
  1381. the pool's usage threshold, if supported.
  1382. @item @code{(getUsageThresholdCount(String))} -- Returns
  1383. the number of times the specified pool has matched or exceeded
  1384. its usage threshold, if supported.
  1385. @item @code{(isValid(String))} -- Returns true if the pool
  1386. is still in use by the virtual machine.
  1387. @item @code{(resetPeakUsage(String))} -- Resets the peak usage
  1388. levels to the current usage levels for the specified pool.
  1389. @item @code{(setCollectionUsageThreshold(String, long))} -- Sets
  1390. the pool's collection usage threshold, if supported.
  1391. @item @code{(setUsageThreshold(String, long))} -- Sets
  1392. the pool's usage threshold, if supported.
  1393. @end itemize
  1394. @node gnu.java.lang.management.VMMemoryManagerMXBeanImpl,gnu.java.lang.management.VMGarbageCollectorMXBeanImpl,gnu.java.lang.management.VMMemoryPoolMXBeanImpl,gnu.java.lang.management
  1395. @subsection @code{gnu.java.lang.management.VMMemoryManagerMXBeanImpl}
  1396. The @code{gnu.java.lang.management.MemoryManagerMXBeanImpl} provides an
  1397. implementation of the optional @code{java.lang.management.MemoryManagerMXBean}
  1398. interface, and is supported by VM functionality in the form of
  1399. @code{gnu.java.lang.management.VMMemoryManagerMXBeanImpl}. Providing
  1400. this interface requires implementing two methods (each takes the name
  1401. of the manager as the first argument):
  1402. @itemize @bullet
  1403. @item @code{(getMemoryPoolNames(String))} -- Returns a list of the
  1404. memory pools that the manager maintains. A default implementation
  1405. which scans the results of @code{getMemoryManagerNames()} for each
  1406. pool is provided.
  1407. @item @code{(isValid(String))} -- Returns true if the specified
  1408. manager is still valid, i.e., it is still in use by the virtual machine.
  1409. @end itemize
  1410. @node gnu.java.lang.management.VMGarbageCollectorMXBeanImpl,,gnu.java.lang.management.VMMemoryManagerMXBeanImpl,gnu.java.lang.management
  1411. @subsection @code{gnu.java.lang.management.VMGarbageCollectorMXBeanImpl}
  1412. The @code{gnu.java.lang.management.GarbageCollectorMXBeanImpl} provides an
  1413. implementation of the optional @code{java.lang.management.GarbageCollectorMXBean}
  1414. interface, and is supported by VM functionality in the form of
  1415. @code{gnu.java.lang.management.VMGarbageCollectorMXBeanImpl}. Providing
  1416. this interface requires implementing two methods (each takes the name
  1417. of the garbage collector as the first argument):
  1418. @itemize @bullet
  1419. @item @code{(getCollectionCount(String))} -- Returns the number of
  1420. times the specified garbage collector has run.
  1421. @item @code{(getCollectionTime(String))} -- Returns the accumulated
  1422. number of milliseconds for which the garbage collector has run.
  1423. @end itemize
  1424. Note that each garbage collector is also a memory manager, and so an
  1425. implementation of the @code{gnu.java.lang.management.VMMemoryManagerMXBeanImpl}
  1426. methods for its name should also be provided.
  1427. @node java.lang.management, Classpath Callbacks, gnu.java.lang.management, Classpath Hooks
  1428. @section @code{java.lang.management}
  1429. @code{gnu.java.lang.management} provides the VM interfaces for the GNU
  1430. implementations of the management beans.
  1431. @menu
  1432. * java.lang.management.VMManagementFactory::
  1433. @end menu
  1434. @node java.lang.management.VMManagementFactory,,,java.lang.management
  1435. @subsection @code{java.lang.management.VMManagementFactory}
  1436. This VM interface provides the names of the memory pools, memory managers
  1437. and garbage collectors for use by the @code{java.lang.management.ManagementFactory}
  1438. in creating lists of appropriate beans for these types of managed object.
  1439. The methods are as follows:
  1440. @itemize @bullet
  1441. @item @code{(getMemoryPoolNames())} -- Returns a list of the names
  1442. of the current memory pools in use by the virtual machine.
  1443. @item @code{(getMemoryManagerNames())} -- Returns a list of the names
  1444. of the current memory managers in use by the virtual machine. This
  1445. should not include those that are also garbage collectors.
  1446. @item @code{(getGarbageCollectorNames())} -- Returns a list of the names
  1447. of the current garbage collectors in use by the virtual machine.
  1448. @end itemize
  1449. @node Classpath Callbacks, , java.lang.management, Classpath Hooks
  1450. Some of the classes you implement for the VM will need to call back to
  1451. package-private methods in Classpath:
  1452. @itemize @bullet
  1453. @item @code{java.lang.ThreadGroup.addThread(Thread)}
  1454. Call this method from @code{Thread} when a new @code{Thread} is created, to add it to
  1455. the group.
  1456. @item @code{java.lang.ThreadGroup.removeThread(Thread)}
  1457. Call this method from @code{Thread} when a @code{Thread} is stopped or destroyed.
  1458. @item @code{gnu.java.lang.management.MemoryMXBeanImpl.fireThresholdExceededNotification(String, long, long, long, long)}
  1459. If the monitoring of memory usage thresholds is supported, this method
  1460. should be called when the normal usage of a memory pool crosses the
  1461. threshold, in order to emit a notification. Another notification
  1462. should not be emitted until there is an intermittent period where the
  1463. usage is again below the threshold. The parameters are the memory
  1464. pool name, the usage levels (init, used, committed and max) and the
  1465. number of times the threshold has been crossed.
  1466. @item @code{gnu.java.lang.management.MemoryMXBeanImpl.fireCollectionThresholdExceededNotification(String, long, long, long, long)}
  1467. If the monitoring of memory usage thresholds is supported, this method
  1468. should be called when the usage of a memory pool after a garbage
  1469. collection cycle crosses the threshold, in order to emit a
  1470. notification. Another notification should not be emitted until there
  1471. is an intermittent period where the usage is again below the
  1472. threshold. The parameters are the memory pool name, the usage levels
  1473. (init, used, committed and max) and the number of times the threshold
  1474. has been crossed.
  1475. @end itemize
  1476. @node VM Hooks, JNI Implementation, Classpath Hooks, Top
  1477. @comment node-name, next, previous, up
  1478. @chapter VM Hooks
  1479. VMs need to do some dirty work; there are some things in the VM that
  1480. unfortunately are dependent on the internal structure of various
  1481. classes. This is a guide to all of the things the VM itself needs to
  1482. know about classes.
  1483. Some of the core classes, while being implemented by GNU Classpath,
  1484. provide space for state (in the form of a @code{vmdata} object) to be
  1485. stored by the VM, and can not be constructed normally.
  1486. @itemize @bullet
  1487. @item java.lang.Class
  1488. @item java.lang.ClassLoader
  1489. @end itemize
  1490. The default implementations of some VM classes also follow this methodology,
  1491. when it is intended that most VMs will keep the default.
  1492. @itemize @bullet
  1493. @item java.lang.VMThread
  1494. @item java.lang.VMThrowable
  1495. @end itemize
  1496. Several core classes must be completely implemented by the VM for Classpath to
  1497. work, although reference implementations are provided. These classes are:
  1498. @itemize @bullet
  1499. @item java.lang.reflect.Constructor
  1500. @item java.lang.reflect.Method
  1501. @item java.lang.reflect.Field
  1502. @end itemize
  1503. The following issues are of note;
  1504. @itemize @bullet
  1505. @item @code{java.lang.Class} @*
  1506. The GNU Classpath implementation of @code{java.lang.Class} provides an
  1507. object for storing the internal state of the class maintained by the VM.
  1508. This is the only known place where this matters. The class is
  1509. constructed with this data by the VM@. Some VMs do not create the
  1510. @code{Class} object at the point where the class is defined; instead,
  1511. they wait until a @code{Class} object is actually used.
  1512. @item Array Classes @*
  1513. When you are creating an array class, you should set the
  1514. @code{ClassLoader} of the array class to the @code{ClassLoader} of its
  1515. component type. Whenever you add a class to a @code{ClassLoader}, you
  1516. need to notify the @code{ClassLoader} and add the new @code{Class} to
  1517. its internal cache of classes. To do this, call
  1518. @code{ClassLoader.addVMCreatedClass(Class)}. @emph{Note: this is
  1519. written in anticipation of 1.2 support and does not apply just yet.}
  1520. @item Primordial Class Loader @*
  1521. When the primordial class loader loads a class, it needs to tell
  1522. Classpath what it has done in order for security stuff to work right.
  1523. To do this, call the static method
  1524. @code{ClassLoader.newPrimordialClass(Class)}.
  1525. Even the first few core classes need to do this; in order to do it,
  1526. simply call this method @emph{after} the initial class loading has been
  1527. done. No harm will come, as long as you follow the guidelines in the
  1528. @pxref{Initialization} section.
  1529. @emph{Note: this is written in anticipation of 1.2 support and does not
  1530. apply just yet.}
  1531. @item Top-level Exception Handler @*
  1532. Exceptions take care of themselves in Classpath; all you need to do in
  1533. the top-level exception handler is call @code{Throwable.printStackTrace()}.
  1534. @item Security and Traces @*
  1535. There will eventually be a feature in the 1.2 security that keeps the
  1536. @code{AccessController} from having to evaluate @emph{all} of the
  1537. @code{ProtectionDomain}s every time a security check is made. I think a common
  1538. case is a single method doing a lot of things that require security
  1539. checks. However, I don't want to bog down the method stack too much, so
  1540. this feature of the VM will have the @code{AccessController} for a thread
  1541. calling out to the VM to tell it how high it was on the stack when it
  1542. made the last security request. Every time the stack goes lower than
  1543. that number, the VM will decrement the number. The @code{AccessController}
  1544. will remember what the accumulated protection status was at every stack
  1545. level (an @code{AccessControlContext}) and use that aggregated information to
  1546. do the check. I am not sure, however, whether the savings are
  1547. substantial enough to outweigh the integer check and set after every
  1548. method call. I will investigate.
  1549. @item Threading @*
  1550. I figured I'd put this here because a VM guy might be wondering about it.
  1551. We implement @code{ThreadGroup}, but that class is almost entirely
  1552. VM-independent. The root @code{ThreadGroup}, a static field called
  1553. @code{ThreadGroup.root}, should be initialized by Classpath, but if you wish to
  1554. reinitialize it yourself, there should be no harm.
  1555. @end itemize
  1556. @node JNI Implementation, JVMTI Implementation, VM Hooks, Top
  1557. @comment node-name, next, previous, up
  1558. @chapter JNI Implementation
  1559. Classpath comes with its own implementation of @file{jni.h}. This
  1560. file can be customized by the VM in a few ways, by defining macros
  1561. that affect the interpretation of the file. These macros are all
  1562. intended for use by a VM which uses GNU Classpath and which wants to
  1563. use a single copy of @file{jni.h} for both internal and external use.
  1564. @itemize @bullet
  1565. @item _CLASSPATH_VM_JNI_TYPES_DEFINED
  1566. Some VMs like to define JNI ``object'' types in a special way. If
  1567. this macro is defined, the Classpath @file{jni.h} will avoid defining
  1568. these types. By default, these types are defined in @file{jni.h}.
  1569. The full list of types and macros treated this way is: @samp{jobject},
  1570. @samp{jclass}, @samp{jstring}, @samp{jthrowable}, @samp{jweak},
  1571. @samp{jarray}, @samp{jobjectArray}, @samp{jbyteArray},
  1572. @samp{jshortArray}, @samp{jintArray}, @samp{jlongArray},
  1573. @samp{jbooleanArray}, @samp{jcharArray}, @samp{jfloatArray},
  1574. @samp{jdoubleArray}, @samp{JNIEnv}, @samp{JavaVM}, @samp{JNI_TRUE}
  1575. (macro), @samp{JNI_FALSE} (macro).
  1576. @item _CLASSPATH_VM_INTERNAL_TYPES_DEFINED
  1577. If the VM has its own definitions for @samp{jfieldID} and
  1578. @samp{jmethodID}, then it should define this macro. Otherwise,
  1579. @file{jni.h} will provide definitions for these types.
  1580. @item _CLASSPATH_JNIIMPEXP
  1581. Three functions -- @samp{JNI_GetDefaultJavaVMInitArgs},
  1582. @samp{JNI_CreateJavaVM}, and @samp{JNI_GetCreatedJavaVMs} -- must be
  1583. marked as @samp{JNIIMPORT} when seen by user code, but most likely
  1584. should be marked as @samp{JNIEXPORT} when defined in the VM
  1585. implementation. This macro can be defined to one or the other by the
  1586. VM as appropriate. If this macro is not defined, it defaults to
  1587. @samp{JNIIMPORT}.
  1588. @item _CLASSPATH_JNIENV_CONTENTS
  1589. A VM can add fields to the @samp{JNIEnv} structure by defining this to
  1590. be a sequence of field declarations.
  1591. @end itemize
  1592. @node JVMTI Implementation, Miscellaneous VM Requirements, JNI Implementation, Top
  1593. @comment node-name, next, previous, up
  1594. @chapter JVMTI Implementation
  1595. Classpath comes with its own implementation of @file{jvmti.h}. This
  1596. file can be customized by the VM in a few ways by defining macros that
  1597. affect the interpretation of the file. These macros are all intended
  1598. for use for use by a VM which uses GNU Classpath and which wants to
  1599. use a single copy of @file{jvmti.h} for both internal and external use.
  1600. @itemize @bullet
  1601. @item _CLASSPATH_VM_JVMTI_TYPES_DEFINED
  1602. Some VMs like to define JVMTI ``object'' types in a special way. If
  1603. this macro is defined, the Classpath @file{jvmti.h} will avoid defining
  1604. these types. By default these types are defined in @file{jvmti.h}.
  1605. The full list of types and macros treated this way is: @samp{jthread},
  1606. @samp{jthreadGroup}, @samp{jlocation}, and @samp{jrawMonitorID}. By
  1607. default @samp{jrawMonitorID} is defined as an opaque pointer which
  1608. must be defined by the VM.
  1609. @item _CLASSPATH_JVMTIENV_CONTENTS
  1610. A VM can add fields to the @samp{jvmtiEnv} structure by defining this
  1611. to be a sequence of field declarations.
  1612. @end itemize
  1613. @node Miscellaneous VM Requirements, , JVMTI Implementation, Top
  1614. @comment node-name, next, previous, up
  1615. @chapter Miscellaneous VM Requirements
  1616. Classpath places a few requirements on the VM that uses it.
  1617. @menu
  1618. * JNI Version::
  1619. * VM Threading Model::
  1620. * Boot Library Path Property::
  1621. @end menu
  1622. @node JNI Version, VM Threading Model, Miscellaneous VM Requirements, Miscellaneous VM Requirements
  1623. @comment node-name, next, previous, up
  1624. @section JNI Version
  1625. Classpath currently uses only JNI 1.1, except for one JNI 1.2 function
  1626. in the JNI Invocation API: GetEnv(). And GetEnv() is only used in the
  1627. now deprecated ``portable native sync'' code.
  1628. A future direction will probably be to require that all VMs provide
  1629. JNI 1.2. If this poses problems, please raise them on the classpath
  1630. mailing list.
  1631. @node VM Threading Model, Boot Library Path Property, JNI Version, Miscellaneous VM Requirements
  1632. @comment node-name, next, previous, up
  1633. @section VM Threading Model
  1634. VM authors can implement a number of different threading models. When
  1635. native code is also threaded there is the potential for one threading
  1636. model to deadlock the other. The
  1637. @uref{http://java.sun.com/docs/books/jni/html/other.html#29406,Java
  1638. Native Interface Programmer's Guide and Specification} suggests
  1639. consulting VM documentation in such situations. Classpath uses
  1640. existing libraries, for example the AWT peers can use the GTK+
  1641. graphics library. As these libraries assume a different threading
  1642. model, there is the potential for the native code to deadlock a VM.
  1643. The different threading models available to a VM author are:
  1644. @enumerate
  1645. @item
  1646. @i{Native threads}: Map a Java thread to an underlying operating system
  1647. thread (normally a POSIX compatible pthread). This approach reduces
  1648. the potential for deadlock as there is only one thread scheduling
  1649. mechanism.
  1650. @item
  1651. @i{Green threads 1}: Green threads are threads scheduled by the VM,
  1652. typically by switching swapping registers. In early VMs green threads
  1653. were seen as advantageous as they didn't require the operating system
  1654. to reschedule, save and swap all of a threads registers. The green
  1655. thread 1 model switches thread on an externally created event, such as
  1656. a timer interrupt. An example of a VM using this approach is Kaffe
  1657. configured with its jthreads model.
  1658. @item
  1659. @i{Green threads 2}: The essential difference with this model is to
  1660. not switch threads on an event, but at fixed points in the code being
  1661. executed by the VM@. Points chosen could be backward branches (loops)
  1662. or method calls. This approach can be advantageous to nonconservative
  1663. garbage collectors, as non-running threads would be at known points
  1664. and can have fixed register maps. It can also reduce the number of
  1665. registers it is necessary to swap when switching threads.
  1666. @item
  1667. @i{M:N threading}: a flaw to green threading is that it is unable to
  1668. use multiple processors. @i{M}:@i{N} threading fixes this problem by
  1669. running groups of green threads on multiple underlying native
  1670. threads. An example of a VM using this approach is the Jikes RVM,
  1671. which uses @i{M}:@i{N} threading combined with the green thread 2
  1672. model.
  1673. @end enumerate
  1674. An example of the problem of mixing threading models is:
  1675. @itemize @bullet
  1676. @item
  1677. A Java thread calls a native method. The native method acquires a lock.
  1678. @item
  1679. The native method calls back into the VM.
  1680. @item
  1681. An event triggers the VM to reschedule the currently running thread.
  1682. @item
  1683. A new VM thread, executing on the same underlying native thread, calls
  1684. a native method.
  1685. @item
  1686. The native method tries to acquire the lock already acquired earlier. As
  1687. the lock is busy the thread waits and allows the operating system to
  1688. reschedule native threads.
  1689. @item
  1690. The operating system reschedules the VM thread again, but the lock is
  1691. still busy and in some threading models will remain busy forever
  1692. (the VM is deadlocked).
  1693. @end itemize
  1694. VMs that don't use the underlying operating system thread scheduling
  1695. mechanism need to avoid deadlock. One now deprecated approach was to
  1696. build Classpath and VMs on top of a wrapper thread library (aka
  1697. portable native sync). The wrapper thread library used was GLIB's
  1698. @dfn{gthreads}. This approach has been deprecated because:
  1699. @enumerate
  1700. @item
  1701. The wrapper library is only in use by some native libraries. For
  1702. example, GTK+ uses the gthread library but QT does not.
  1703. @item
  1704. The wrapper library can't be in use prior to the VM starting as the VM
  1705. must replace the wrapper libraries functions with its own. This
  1706. prevents the VM from running as a plugin in an application that
  1707. already uses the wrapper library.
  1708. @end enumerate
  1709. An alternative approach is for the VM to detect deadlocked native code
  1710. and swap Java threads off of that native thread. The VM can't,
  1711. however, swap two blocked native threads that are potentially
  1712. deadlocking each other on a lock. The lock will be associated with the
  1713. native thread. To prevent this from happening the VM must hijack
  1714. functions that operate on locks. This is done by redefining the lock
  1715. functions inside the VM and configuring the linker so that it uses the
  1716. VMs symbol in preference to that of the external thread support
  1717. library. The VM's lock function can then reschedule Java threads if it
  1718. must wait for the lock.
  1719. @node Boot Library Path Property, , VM Threading Model, Miscellaneous VM Requirements
  1720. @comment node-name, next, previous, up
  1721. @section Boot Library Path Property
  1722. As of GNU Classpath 0.15 a system property named @code{gnu.classpath.boot.library.path}
  1723. can be set by the VM to specify the directories which contain GNU Classpath's native
  1724. libraries. Usually this value is given at configuration time and is then hardcoded
  1725. in the VM@. However for development purposes it is handy to switch to another installation
  1726. by overriding the properties' value on the command line.
  1727. A VM that does not support this feature can simply ignore the property.
  1728. For compatibility reasons we suggest to set the default value of @code{java.library.path}
  1729. to the value of the @code{LD_LIBRARY_PATH} environment if it exists on your platform.
  1730. @bye