jtvarh.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by Jonas Maebe,
  4. member of the Free Pascal development team.
  5. This file implements support routines for threadvarq with FPC/JVM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { In Java, threadvars are represented by descendnts of java.lang.ThreadLocal.
  13. This class has three important methods: set, get and initialValue.
  14. If you call the "get" method of a JLThreadLocal instance in a thread for the
  15. first time before calling "set", it will call the initialValue method and
  16. return its result. After that, it will return whatever initialValue returned,
  17. or the previous value instated by "set". A JLThreadLocal always keeps track of
  18. a JLObject.
  19. We don't want to translate accesses to threadvars into calls to get/set, since
  20. that would mean that we would
  21. a) have to generate different code in the compiler for read and write
  22. accesses
  23. b) no be able to pass threadvars to var-parameters etc
  24. Instead, we add a method called getReadWriteReference to all of our
  25. descendants classes that returns a pointer to the actual value of the
  26. threadvar for this thread. This results in several cases:
  27. a) For primitive types, we store an array of one element of that ordinal
  28. type.
  29. Their initialValue is simply an array of one element (automatically
  30. initialized to 0).
  31. The pointer returned is the "address" of element 0 (pointers to primitive
  32. types are internally arrays pointing to element 0).
  33. b) For non-dynamic arrays, we store that array itself (all arrays are
  34. internally Java arrays, which descend from JLObject).
  35. When initializing the threadvar on startup, we pass an empty copy of such
  36. an array to the constructor and store it. Their initialValue is a deep
  37. copy of this array, created by fpc_dynarray_copy (it accepts a number of
  38. dimensions, because it also has to work for making a copy of dynamic
  39. arrays whose elements are regular arrays).
  40. The pointer returned is simply the address of the array.
  41. c) For implicit pointer types other than regular arrays, we also store the
  42. implicit pointer itself and keep an initialized empty instance around
  43. that is passed to the constructor.
  44. Their initialValue is a clone of this empty instance (can't use this for
  45. arrays, since it would make a shallow copy of the array). Because of the
  46. way the JLCloneable interface works, we have to call the clone method via
  47. reflection.
  48. The pointer returned is again simply the implicit pointer itself.
  49. d) For all other types, we store an array of JLObject of one element,
  50. similar as with primitive types.
  51. Their initialValue is either nil, or optionally a value passed to the
  52. constructor when creating the JLThreadLocal instance (e.g. an empty
  53. string for unicodestring/ansistring, or the enum instance whose ordinal
  54. value is 0)
  55. The pointer returned is the address of element 0 of the array.
  56. }
  57. type
  58. FpcImplicitPtrThreadVar = class(JLThreadLocal)
  59. protected
  60. { all implicit pointer types are clonable }
  61. fInstanceToClone: JLObject;
  62. { don't look up the clone method every time }
  63. fCloneMethod: JLRMethod;
  64. function initialValue: JLObject; override;
  65. public
  66. constructor create(initInstanceToClone: JLObject);
  67. function getReadWriteReference: Pointer;
  68. end;
  69. FpcNormalArrayThreadVar = class sealed (FpcImplicitPtrThreadVar)
  70. protected
  71. fArrDim: longint;
  72. fArrTyp: widechar;
  73. function initialValue: JLObject; override;
  74. public
  75. constructor create(initInstanceToClone: JLObject; arrdim: longint; arrtyp: widechar);
  76. end;
  77. FpcBooleanThreadVar = class sealed (JLThreadLocal)
  78. protected
  79. function initialValue: JLObject; override;
  80. public
  81. function getReadWriteReference: PBoolean;
  82. end;
  83. FpcByteThreadVar = class sealed (JLThreadLocal)
  84. protected
  85. function initialValue: JLObject; override;
  86. public
  87. function getReadWriteReference: PShortint;
  88. end;
  89. FpcShortThreadVar = class sealed (JLThreadLocal)
  90. protected
  91. function initialValue: JLObject; override;
  92. public
  93. function getReadWriteReference: PSmallint;
  94. end;
  95. FpcIntThreadVar = class sealed (JLThreadLocal)
  96. protected
  97. function initialValue: JLObject; override;
  98. public
  99. function getReadWriteReference: PLongint;
  100. end;
  101. FpcLongThreadVar = class sealed (JLThreadLocal)
  102. protected
  103. function initialValue: JLObject; override;
  104. public
  105. function getReadWriteReference: PInt64;
  106. end;
  107. FpcCharThreadVar = class sealed (JLThreadLocal)
  108. protected
  109. function initialValue: JLObject; override;
  110. public
  111. function getReadWriteReference: PWideChar;
  112. end;
  113. FpcFloatThreadVar = class sealed (JLThreadLocal)
  114. protected
  115. function initialValue: JLObject; override;
  116. public
  117. function getReadWriteReference: PSingle;
  118. end;
  119. FpcDoubleThreadVar = class sealed (JLThreadLocal)
  120. protected
  121. function initialValue: JLObject; override;
  122. public
  123. function getReadWriteReference: PDouble;
  124. end;
  125. FpcPointerThreadVar = class sealed (JLThreadLocal)
  126. protected
  127. fInitVal: JLObject;
  128. function initialValue: JLObject; override;
  129. public
  130. function getReadWriteReference: PPointer;
  131. constructor create(initVal: JLObject);overload;
  132. end;