ffi 1.9.24 → 1.16.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (457) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +348 -0
  4. data/Gemfile +4 -5
  5. data/README.md +44 -17
  6. data/Rakefile +74 -148
  7. data/ext/ffi_c/AbstractMemory.c +89 -70
  8. data/ext/ffi_c/AbstractMemory.h +3 -2
  9. data/ext/ffi_c/ArrayType.c +49 -13
  10. data/ext/ffi_c/ArrayType.h +1 -0
  11. data/ext/ffi_c/Buffer.c +90 -38
  12. data/ext/ffi_c/Call.c +55 -56
  13. data/ext/ffi_c/Call.h +12 -6
  14. data/ext/ffi_c/ClosurePool.c +333 -0
  15. data/ext/ffi_c/{Closure.h → ClosurePool.h} +25 -13
  16. data/ext/ffi_c/DynamicLibrary.c +91 -33
  17. data/ext/ffi_c/Function.c +306 -235
  18. data/ext/ffi_c/Function.h +3 -5
  19. data/ext/ffi_c/FunctionInfo.c +87 -32
  20. data/ext/ffi_c/LastError.c +74 -15
  21. data/ext/ffi_c/LongDouble.c +12 -10
  22. data/ext/ffi_c/LongDouble.h +0 -4
  23. data/ext/ffi_c/MappedType.c +66 -23
  24. data/ext/ffi_c/MappedType.h +0 -2
  25. data/ext/ffi_c/MemoryPointer.c +37 -15
  26. data/ext/ffi_c/MemoryPointer.h +0 -4
  27. data/ext/ffi_c/MethodHandle.c +265 -38
  28. data/ext/ffi_c/MethodHandle.h +3 -2
  29. data/ext/ffi_c/Platform.c +5 -56
  30. data/ext/ffi_c/Pointer.c +93 -55
  31. data/ext/ffi_c/Pointer.h +1 -4
  32. data/ext/ffi_c/Struct.c +202 -134
  33. data/ext/ffi_c/Struct.h +18 -9
  34. data/ext/ffi_c/StructByValue.c +50 -23
  35. data/ext/ffi_c/StructLayout.c +135 -64
  36. data/ext/ffi_c/Thread.c +4 -228
  37. data/ext/ffi_c/Thread.h +1 -20
  38. data/ext/ffi_c/Type.c +105 -55
  39. data/ext/ffi_c/Type.h +3 -2
  40. data/ext/ffi_c/Types.c +8 -9
  41. data/ext/ffi_c/Types.h +3 -4
  42. data/ext/ffi_c/Variadic.c +88 -47
  43. data/ext/ffi_c/compat.h +26 -22
  44. data/ext/ffi_c/extconf.rb +75 -32
  45. data/ext/ffi_c/ffi.c +9 -10
  46. data/ext/ffi_c/libffi/.appveyor/site.exp +16 -0
  47. data/ext/ffi_c/libffi/.appveyor/unix-noexec.exp +7 -0
  48. data/ext/ffi_c/libffi/.appveyor.yml +53 -19
  49. data/ext/ffi_c/libffi/.ci/bfin-sim.exp +58 -0
  50. data/ext/ffi_c/libffi/.ci/build-cross-in-container.sh +18 -0
  51. data/ext/ffi_c/libffi/.ci/build-in-container.sh +10 -0
  52. data/ext/ffi_c/libffi/.ci/build.sh +124 -0
  53. data/ext/ffi_c/libffi/.ci/install.sh +78 -0
  54. data/ext/ffi_c/libffi/.ci/m32r-sim.exp +58 -0
  55. data/ext/ffi_c/libffi/{.travis → .ci}/moxie-sim.exp +1 -1
  56. data/ext/ffi_c/libffi/.ci/msvs-detect +1103 -0
  57. data/ext/ffi_c/libffi/.ci/or1k-sim.exp +58 -0
  58. data/ext/ffi_c/libffi/.ci/powerpc-eabisim.exp +58 -0
  59. data/ext/ffi_c/libffi/.ci/site.exp +29 -0
  60. data/ext/ffi_c/libffi/.ci/wine-sim.exp +55 -0
  61. data/ext/ffi_c/libffi/.circleci/config.yml +156 -0
  62. data/ext/ffi_c/libffi/.gitattributes +4 -0
  63. data/ext/ffi_c/libffi/.github/workflows/build.yml +460 -0
  64. data/ext/ffi_c/libffi/.github/workflows/emscripten.yml +171 -0
  65. data/ext/ffi_c/libffi/.gitignore +10 -2
  66. data/ext/ffi_c/libffi/{ChangeLog.libffi-3.1 → ChangeLog.old} +1407 -0
  67. data/ext/ffi_c/libffi/LICENSE +1 -1
  68. data/ext/ffi_c/libffi/LICENSE-BUILDTOOLS +5 -4
  69. data/ext/ffi_c/libffi/Makefile.am +64 -73
  70. data/ext/ffi_c/libffi/Makefile.in +553 -235
  71. data/ext/ffi_c/libffi/README.md +165 -100
  72. data/ext/ffi_c/libffi/acinclude.m4 +10 -112
  73. data/ext/ffi_c/libffi/compile +348 -0
  74. data/ext/ffi_c/libffi/config.guess +950 -662
  75. data/ext/ffi_c/libffi/config.sub +1362 -1306
  76. data/ext/ffi_c/libffi/configure +4909 -4096
  77. data/ext/ffi_c/libffi/configure.ac +93 -32
  78. data/ext/ffi_c/libffi/configure.host +76 -28
  79. data/ext/ffi_c/libffi/doc/Makefile.in +15 -8
  80. data/ext/ffi_c/libffi/doc/libffi.texi +107 -46
  81. data/ext/ffi_c/libffi/doc/version.texi +4 -4
  82. data/ext/ffi_c/libffi/fficonfig.h.in +22 -44
  83. data/ext/ffi_c/libffi/generate-darwin-source-and-headers.py +165 -56
  84. data/ext/ffi_c/libffi/include/Makefile.am +1 -1
  85. data/ext/ffi_c/libffi/include/Makefile.in +17 -12
  86. data/ext/ffi_c/libffi/include/ffi.h.in +64 -48
  87. data/ext/ffi_c/libffi/include/ffi_cfi.h +21 -0
  88. data/ext/ffi_c/libffi/include/ffi_common.h +34 -1
  89. data/ext/ffi_c/libffi/include/tramp.h +45 -0
  90. data/ext/ffi_c/libffi/install-sh +107 -74
  91. data/ext/ffi_c/libffi/libffi.map.in +8 -12
  92. data/ext/ffi_c/libffi/libffi.xcodeproj/project.pbxproj +2 -48
  93. data/ext/ffi_c/libffi/libtool-version +2 -2
  94. data/ext/ffi_c/libffi/ltmain.sh +576 -284
  95. data/ext/ffi_c/libffi/m4/asmcfi.m4 +1 -1
  96. data/ext/ffi_c/libffi/m4/ax_append_flag.m4 +5 -26
  97. data/ext/ffi_c/libffi/m4/ax_cc_maxopt.m4 +18 -14
  98. data/ext/ffi_c/libffi/m4/ax_cflags_warn_all.m4 +108 -72
  99. data/ext/ffi_c/libffi/m4/ax_check_compile_flag.m4 +5 -26
  100. data/ext/ffi_c/libffi/m4/ax_compiler_vendor.m4 +76 -44
  101. data/ext/ffi_c/libffi/m4/ax_configure_args.m4 +5 -26
  102. data/ext/ffi_c/libffi/m4/ax_gcc_archflag.m4 +7 -3
  103. data/ext/ffi_c/libffi/m4/ax_prepend_flag.m4 +51 -0
  104. data/ext/ffi_c/libffi/make_sunver.pl +333 -0
  105. data/ext/ffi_c/libffi/man/Makefile.in +15 -8
  106. data/ext/ffi_c/libffi/missing +8 -8
  107. data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.sln +33 -0
  108. data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj +130 -0
  109. data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj.filters +57 -0
  110. data/ext/ffi_c/libffi/msvc_build/aarch64/Ffi_staticLib.vcxproj.user +4 -0
  111. data/ext/ffi_c/libffi/msvc_build/aarch64/aarch64_include/ffi.h +511 -0
  112. data/ext/ffi_c/libffi/msvc_build/aarch64/aarch64_include/fficonfig.h +219 -0
  113. data/ext/ffi_c/libffi/msvcc.sh +39 -14
  114. data/ext/ffi_c/libffi/src/aarch64/ffi.c +286 -98
  115. data/ext/ffi_c/libffi/src/aarch64/ffitarget.h +19 -3
  116. data/ext/ffi_c/libffi/src/aarch64/internal.h +33 -0
  117. data/ext/ffi_c/libffi/src/aarch64/sysv.S +142 -37
  118. data/ext/ffi_c/libffi/src/aarch64/win64_armasm.S +506 -0
  119. data/ext/ffi_c/libffi/src/arc/arcompact.S +169 -94
  120. data/ext/ffi_c/libffi/src/arc/ffi.c +325 -148
  121. data/ext/ffi_c/libffi/src/arc/ffitarget.h +14 -0
  122. data/ext/ffi_c/libffi/src/arm/ffi.c +112 -10
  123. data/ext/ffi_c/libffi/src/arm/ffitarget.h +8 -1
  124. data/ext/ffi_c/libffi/src/arm/internal.h +10 -0
  125. data/ext/ffi_c/libffi/src/arm/sysv.S +117 -44
  126. data/ext/ffi_c/libffi/src/arm/sysv_msvc_arm32.S +311 -0
  127. data/ext/ffi_c/libffi/src/closures.c +189 -48
  128. data/ext/ffi_c/libffi/src/csky/ffi.c +395 -0
  129. data/ext/ffi_c/libffi/src/csky/ffitarget.h +63 -0
  130. data/ext/ffi_c/libffi/src/csky/sysv.S +371 -0
  131. data/ext/ffi_c/libffi/src/dlmalloc.c +6 -1
  132. data/ext/ffi_c/libffi/src/frv/ffi.c +1 -1
  133. data/ext/ffi_c/libffi/src/ia64/ffi.c +12 -0
  134. data/ext/ffi_c/libffi/src/ia64/unix.S +20 -2
  135. data/ext/ffi_c/libffi/src/kvx/asm.h +5 -0
  136. data/ext/ffi_c/libffi/src/kvx/ffi.c +273 -0
  137. data/ext/ffi_c/libffi/src/kvx/ffitarget.h +75 -0
  138. data/ext/ffi_c/libffi/src/kvx/sysv.S +127 -0
  139. data/ext/ffi_c/libffi/src/loongarch64/ffi.c +621 -0
  140. data/ext/ffi_c/libffi/src/loongarch64/ffitarget.h +82 -0
  141. data/ext/ffi_c/libffi/src/loongarch64/sysv.S +327 -0
  142. data/ext/ffi_c/libffi/src/m32r/ffi.c +31 -14
  143. data/ext/ffi_c/libffi/src/metag/ffi.c +1 -1
  144. data/ext/ffi_c/libffi/src/mips/ffi.c +245 -66
  145. data/ext/ffi_c/libffi/src/mips/ffitarget.h +8 -1
  146. data/ext/ffi_c/libffi/src/mips/n32.S +137 -28
  147. data/ext/ffi_c/libffi/src/mips/o32.S +63 -4
  148. data/ext/ffi_c/libffi/src/moxie/ffi.c +48 -23
  149. data/ext/ffi_c/libffi/src/or1k/ffi.c +25 -12
  150. data/ext/ffi_c/libffi/src/pa/ffi.c +62 -108
  151. data/ext/ffi_c/libffi/src/pa/ffi64.c +614 -0
  152. data/ext/ffi_c/libffi/src/pa/ffitarget.h +20 -11
  153. data/ext/ffi_c/libffi/src/pa/hpux32.S +87 -38
  154. data/ext/ffi_c/libffi/src/pa/hpux64.S +681 -0
  155. data/ext/ffi_c/libffi/src/pa/linux.S +109 -39
  156. data/ext/ffi_c/libffi/src/powerpc/ffi.c +6 -4
  157. data/ext/ffi_c/libffi/src/powerpc/ffi_darwin.c +13 -1
  158. data/ext/ffi_c/libffi/src/powerpc/ffi_linux64.c +211 -32
  159. data/ext/ffi_c/libffi/src/powerpc/ffi_powerpc.h +18 -7
  160. data/ext/ffi_c/libffi/src/powerpc/ffitarget.h +10 -4
  161. data/ext/ffi_c/libffi/src/powerpc/linux64.S +93 -28
  162. data/ext/ffi_c/libffi/src/powerpc/linux64_closure.S +83 -5
  163. data/ext/ffi_c/libffi/src/powerpc/sysv.S +5 -7
  164. data/ext/ffi_c/libffi/src/powerpc/t-aix +5 -0
  165. data/ext/ffi_c/libffi/src/prep_cif.c +26 -4
  166. data/ext/ffi_c/libffi/src/riscv/ffi.c +79 -10
  167. data/ext/ffi_c/libffi/src/riscv/ffitarget.h +1 -0
  168. data/ext/ffi_c/libffi/src/riscv/sysv.S +86 -7
  169. data/ext/ffi_c/libffi/src/sparc/ffi64.c +16 -0
  170. data/ext/ffi_c/libffi/src/tramp.c +716 -0
  171. data/ext/ffi_c/libffi/src/wasm32/ffi.c +934 -0
  172. data/ext/ffi_c/libffi/src/wasm32/ffitarget.h +62 -0
  173. data/ext/ffi_c/libffi/src/x86/ffi.c +128 -49
  174. data/ext/ffi_c/libffi/src/x86/ffi64.c +89 -23
  175. data/ext/ffi_c/libffi/src/x86/ffitarget.h +21 -4
  176. data/ext/ffi_c/libffi/src/x86/ffiw64.c +63 -10
  177. data/ext/ffi_c/libffi/src/x86/internal.h +14 -0
  178. data/ext/ffi_c/libffi/src/x86/internal64.h +14 -0
  179. data/ext/ffi_c/libffi/src/x86/sysv.S +274 -45
  180. data/ext/ffi_c/libffi/src/x86/sysv_intel.S +998 -0
  181. data/ext/ffi_c/libffi/src/x86/unix64.S +190 -4
  182. data/ext/ffi_c/libffi/src/x86/win64.S +32 -10
  183. data/ext/ffi_c/libffi/src/x86/win64_intel.S +3 -2
  184. data/ext/ffi_c/libffi/src/xtensa/ffi.c +16 -8
  185. data/ext/ffi_c/libffi/src/xtensa/ffitarget.h +4 -0
  186. data/ext/ffi_c/libffi/src/xtensa/sysv.S +26 -16
  187. data/ext/ffi_c/libffi/testsuite/Makefile.am +127 -109
  188. data/ext/ffi_c/libffi/testsuite/Makefile.in +144 -88
  189. data/ext/ffi_c/libffi/testsuite/emscripten/build-tests.sh +54 -0
  190. data/ext/ffi_c/libffi/testsuite/emscripten/build.sh +63 -0
  191. data/ext/ffi_c/libffi/testsuite/emscripten/conftest.py +80 -0
  192. data/ext/ffi_c/libffi/testsuite/emscripten/node-tests.sh +48 -0
  193. data/ext/ffi_c/libffi/testsuite/emscripten/test.html +7 -0
  194. data/ext/ffi_c/libffi/testsuite/emscripten/test_libffi.py +51 -0
  195. data/ext/ffi_c/libffi/testsuite/lib/libffi.exp +71 -25
  196. data/ext/ffi_c/libffi/testsuite/lib/target-libpath.exp +2 -2
  197. data/ext/ffi_c/libffi/testsuite/libffi.bhaible/bhaible.exp +7 -2
  198. data/ext/ffi_c/libffi/testsuite/libffi.bhaible/test-call.c +5 -4
  199. data/ext/ffi_c/libffi/testsuite/libffi.bhaible/test-callback.c +2 -2
  200. data/ext/ffi_c/libffi/testsuite/libffi.bhaible/testcases.c +5 -1
  201. data/ext/ffi_c/libffi/testsuite/libffi.call/bpo_38748.c +41 -0
  202. data/ext/ffi_c/libffi/testsuite/libffi.call/call.exp +12 -1
  203. data/ext/ffi_c/libffi/testsuite/libffi.call/ffitest.h +28 -3
  204. data/ext/ffi_c/libffi/testsuite/libffi.call/float2.c +9 -8
  205. data/ext/ffi_c/libffi/testsuite/libffi.call/float_va.c +3 -0
  206. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ll1.c +5 -0
  207. data/ext/ffi_c/libffi/testsuite/libffi.call/return_sl.c +4 -0
  208. data/ext/ffi_c/libffi/testsuite/libffi.call/return_ul.c +4 -0
  209. data/ext/ffi_c/libffi/testsuite/libffi.call/s55.c +60 -0
  210. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen.c +8 -9
  211. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen2.c +2 -2
  212. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen3.c +2 -2
  213. data/ext/ffi_c/libffi/testsuite/libffi.call/strlen4.c +2 -2
  214. data/ext/ffi_c/libffi/testsuite/libffi.call/struct10.c +1 -1
  215. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_2.c +63 -0
  216. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_3.c +65 -0
  217. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_3f.c +65 -0
  218. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_4.c +67 -0
  219. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_4f.c +67 -0
  220. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_big.c +93 -0
  221. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_by_value_small.c +61 -0
  222. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_return_2H.c +63 -0
  223. data/ext/ffi_c/libffi/testsuite/libffi.call/struct_return_8H.c +90 -0
  224. data/ext/ffi_c/libffi/testsuite/libffi.call/va_1.c +1 -119
  225. data/ext/ffi_c/libffi/testsuite/libffi.call/va_2.c +220 -0
  226. data/ext/ffi_c/libffi/testsuite/libffi.call/va_3.c +154 -0
  227. data/ext/ffi_c/libffi/testsuite/libffi.call/va_struct1.c +13 -0
  228. data/ext/ffi_c/libffi/testsuite/libffi.call/va_struct2.c +11 -0
  229. data/ext/ffi_c/libffi/testsuite/libffi.call/va_struct3.c +15 -0
  230. data/ext/ffi_c/libffi/testsuite/libffi.closures/closure.exp +67 -0
  231. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn0.c +3 -2
  232. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn1.c +2 -0
  233. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn2.c +2 -0
  234. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn3.c +21 -1
  235. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn4.c +2 -0
  236. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn5.c +2 -0
  237. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_fn6.c +2 -0
  238. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_loc_fn0.c +7 -6
  239. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/closure_simple.c +6 -0
  240. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_12byte.c +18 -0
  241. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_16byte.c +22 -0
  242. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_18byte.c +24 -0
  243. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_19byte.c +29 -0
  244. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_1_1byte.c +4 -0
  245. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_20byte.c +19 -1
  246. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_20byte1.c +21 -1
  247. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_24byte.c +35 -3
  248. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_2byte.c +13 -0
  249. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3_1byte.c +19 -0
  250. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3byte1.c +13 -0
  251. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3byte2.c +13 -0
  252. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_3float.c +18 -0
  253. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_4_1byte.c +22 -0
  254. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_4byte.c +13 -0
  255. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_5_1_byte.c +29 -1
  256. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_5byte.c +19 -1
  257. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_64byte.c +24 -0
  258. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_6_1_byte.c +28 -1
  259. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_6byte.c +24 -2
  260. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_7_1_byte.c +39 -1
  261. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_7byte.c +25 -1
  262. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_8byte.c +14 -0
  263. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_9byte1.c +14 -1
  264. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_9byte2.c +14 -2
  265. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_double.c +19 -1
  266. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_float.c +19 -1
  267. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble.c +20 -1
  268. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble_split.c +40 -25
  269. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_longdouble_split2.c +40 -3
  270. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_pointer.c +19 -1
  271. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint16.c +18 -1
  272. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint32.c +18 -3
  273. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_sint64.c +18 -1
  274. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint16.c +18 -1
  275. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint32.c +19 -1
  276. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_align_uint64.c +19 -1
  277. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_dbls_struct.c +3 -1
  278. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_double_va.c +9 -1
  279. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_float.c +4 -0
  280. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_longdouble.c +11 -1
  281. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_longdouble_va.c +22 -3
  282. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_schar.c +5 -1
  283. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_sshort.c +6 -1
  284. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_sshortchar.c +9 -1
  285. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_uchar.c +9 -1
  286. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_ushort.c +6 -2
  287. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_multi_ushortchar.c +9 -1
  288. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_pointer.c +5 -0
  289. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_pointer_stack.c +10 -0
  290. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_schar.c +3 -0
  291. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_sint.c +2 -0
  292. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_sshort.c +3 -0
  293. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_struct_va1.c +11 -0
  294. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uchar.c +3 -0
  295. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uint.c +4 -0
  296. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_uint_va.c +4 -0
  297. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ulong_va.c +4 -0
  298. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ulonglong.c +2 -0
  299. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_ushort.c +3 -0
  300. data/ext/ffi_c/libffi/testsuite/libffi.closures/ffitest.h +1 -0
  301. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/huge_struct.c +24 -22
  302. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct.c +32 -9
  303. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct1.c +1 -1
  304. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct10.c +12 -0
  305. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct11.c +21 -5
  306. data/ext/ffi_c/libffi/testsuite/libffi.closures/nested_struct12.c +86 -0
  307. data/ext/ffi_c/libffi/testsuite/libffi.closures/nested_struct13.c +115 -0
  308. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct2.c +10 -1
  309. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct3.c +10 -1
  310. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct4.c +9 -0
  311. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct5.c +9 -0
  312. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct6.c +11 -0
  313. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct7.c +9 -0
  314. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct8.c +11 -0
  315. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/nested_struct9.c +11 -0
  316. data/ext/ffi_c/libffi/testsuite/libffi.closures/single_entry_structs1.c +86 -0
  317. data/ext/ffi_c/libffi/testsuite/libffi.closures/single_entry_structs2.c +102 -0
  318. data/ext/ffi_c/libffi/testsuite/libffi.closures/single_entry_structs3.c +101 -0
  319. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_medium.c +1 -1
  320. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_medium2.c +1 -1
  321. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/testclosure.c +6 -1
  322. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/unwindtest.cc +2 -1
  323. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/unwindtest_ffi_call.cc +1 -0
  324. data/ext/ffi_c/libffi.bsd.mk +2 -2
  325. data/ext/ffi_c/libffi.darwin.mk +1 -1
  326. data/ext/ffi_c/libffi.gnu.mk +2 -2
  327. data/ext/ffi_c/rbffi.h +1 -3
  328. data/ffi.gemspec +15 -9
  329. data/lib/ffi/abstract_memory.rb +44 -0
  330. data/lib/ffi/autopointer.rb +8 -23
  331. data/lib/ffi/compat.rb +43 -0
  332. data/lib/ffi/data_converter.rb +67 -0
  333. data/lib/ffi/dynamic_library.rb +89 -0
  334. data/lib/ffi/enum.rb +18 -11
  335. data/lib/ffi/ffi.rb +6 -0
  336. data/lib/ffi/function.rb +71 -0
  337. data/lib/ffi/io.rb +3 -3
  338. data/lib/ffi/library.rb +65 -77
  339. data/lib/ffi/library_path.rb +109 -0
  340. data/lib/ffi/managedstruct.rb +3 -3
  341. data/lib/ffi/platform/aarch64-darwin/types.conf +130 -0
  342. data/lib/ffi/platform/aarch64-freebsd/types.conf +128 -0
  343. data/lib/ffi/platform/aarch64-freebsd12/types.conf +181 -0
  344. data/lib/ffi/platform/aarch64-linux/types.conf +81 -81
  345. data/lib/ffi/platform/aarch64-openbsd/types.conf +134 -0
  346. data/lib/ffi/platform/aarch64-windows/types.conf +52 -0
  347. data/lib/ffi/platform/arm-freebsd/types.conf +152 -0
  348. data/lib/ffi/platform/arm-freebsd12/types.conf +152 -0
  349. data/lib/ffi/platform/arm-linux/types.conf +110 -82
  350. data/lib/ffi/platform/hppa1.1-linux/types.conf +178 -0
  351. data/lib/ffi/platform/hppa2.0-linux/types.conf +178 -0
  352. data/lib/ffi/platform/i386-cygwin/types.conf +1 -1
  353. data/lib/ffi/platform/i386-darwin/types.conf +63 -63
  354. data/lib/ffi/platform/i386-freebsd/types.conf +89 -89
  355. data/lib/ffi/platform/i386-freebsd12/types.conf +152 -0
  356. data/lib/ffi/platform/i386-gnu/types.conf +84 -84
  357. data/lib/ffi/platform/i386-linux/types.conf +77 -77
  358. data/lib/ffi/platform/i386-netbsd/types.conf +87 -87
  359. data/lib/ffi/platform/i386-openbsd/types.conf +89 -89
  360. data/lib/ffi/platform/i386-solaris/types.conf +96 -96
  361. data/lib/ffi/platform/i386-windows/types.conf +43 -96
  362. data/lib/ffi/platform/ia64-linux/types.conf +79 -79
  363. data/lib/ffi/platform/loongarch64-linux/types.conf +141 -0
  364. data/lib/ffi/platform/mips-linux/types.conf +79 -79
  365. data/lib/ffi/platform/mips64-linux/types.conf +81 -81
  366. data/lib/ffi/platform/mips64el-linux/types.conf +81 -81
  367. data/lib/ffi/platform/mipsel-linux/types.conf +79 -79
  368. data/lib/ffi/platform/mipsisa32r6-linux/types.conf +79 -79
  369. data/lib/ffi/platform/mipsisa32r6el-linux/types.conf +79 -79
  370. data/lib/ffi/platform/mipsisa64r6-linux/types.conf +81 -81
  371. data/lib/ffi/platform/mipsisa64r6el-linux/types.conf +81 -81
  372. data/lib/ffi/platform/powerpc-aix/types.conf +155 -155
  373. data/lib/ffi/platform/powerpc-darwin/types.conf +63 -63
  374. data/lib/ffi/platform/powerpc-linux/types.conf +108 -78
  375. data/lib/ffi/platform/powerpc-openbsd/types.conf +156 -0
  376. data/lib/ffi/platform/powerpc64-linux/types.conf +81 -81
  377. data/lib/ffi/platform/powerpc64le-linux/types.conf +100 -0
  378. data/lib/ffi/platform/riscv64-linux/types.conf +104 -0
  379. data/lib/ffi/platform/s390-linux/types.conf +79 -79
  380. data/lib/ffi/platform/s390x-linux/types.conf +79 -79
  381. data/lib/ffi/platform/sparc-linux/types.conf +79 -79
  382. data/lib/ffi/platform/sparc-solaris/types.conf +103 -103
  383. data/lib/ffi/platform/sparcv9-openbsd/types.conf +156 -0
  384. data/lib/ffi/platform/sparcv9-solaris/types.conf +103 -103
  385. data/lib/ffi/platform/sw_64-linux/types.conf +141 -0
  386. data/lib/ffi/platform/x86_64-cygwin/types.conf +1 -1
  387. data/lib/ffi/platform/x86_64-darwin/types.conf +88 -84
  388. data/lib/ffi/platform/x86_64-dragonflybsd/types.conf +130 -0
  389. data/lib/ffi/platform/x86_64-freebsd/types.conf +90 -90
  390. data/lib/ffi/platform/x86_64-freebsd12/types.conf +158 -0
  391. data/lib/ffi/platform/x86_64-haiku/types.conf +117 -0
  392. data/lib/ffi/platform/x86_64-linux/types.conf +107 -77
  393. data/lib/ffi/platform/x86_64-msys/types.conf +119 -0
  394. data/lib/ffi/platform/x86_64-netbsd/types.conf +89 -89
  395. data/lib/ffi/platform/x86_64-openbsd/types.conf +86 -86
  396. data/lib/ffi/platform/x86_64-solaris/types.conf +96 -96
  397. data/lib/ffi/platform/x86_64-windows/types.conf +42 -110
  398. data/lib/ffi/platform.rb +43 -20
  399. data/lib/ffi/pointer.rb +26 -20
  400. data/lib/ffi/struct.rb +14 -68
  401. data/lib/ffi/struct_by_reference.rb +72 -0
  402. data/lib/ffi/struct_layout.rb +96 -0
  403. data/lib/ffi/struct_layout_builder.rb +1 -1
  404. data/lib/ffi/tools/const_generator.rb +11 -8
  405. data/lib/ffi/tools/generator.rb +47 -2
  406. data/lib/ffi/tools/generator_task.rb +13 -17
  407. data/lib/ffi/tools/struct_generator.rb +6 -5
  408. data/lib/ffi/tools/types_generator.rb +7 -4
  409. data/lib/ffi/types.rb +33 -7
  410. data/lib/ffi/variadic.rb +20 -18
  411. data/lib/ffi/version.rb +1 -1
  412. data/lib/ffi.rb +10 -3
  413. data/rakelib/ffi_gem_helper.rb +65 -0
  414. data/samples/getlogin.rb +1 -1
  415. data/samples/getpid.rb +1 -1
  416. data/samples/gettimeofday.rb +8 -8
  417. data/samples/hello.rb +2 -1
  418. data/samples/hello_ractor.rb +11 -0
  419. data/samples/inotify.rb +1 -1
  420. data/samples/pty.rb +1 -2
  421. data/samples/qsort.rb +0 -1
  422. data/samples/qsort_ractor.rb +28 -0
  423. data.tar.gz.sig +0 -0
  424. metadata +252 -153
  425. metadata.gz.sig +0 -0
  426. data/.gitignore +0 -22
  427. data/.gitmodules +0 -3
  428. data/.travis.yml +0 -52
  429. data/.yardopts +0 -5
  430. data/appveyor.yml +0 -22
  431. data/ext/ffi_c/Closure.c +0 -54
  432. data/ext/ffi_c/DataConverter.c +0 -91
  433. data/ext/ffi_c/StructByReference.c +0 -190
  434. data/ext/ffi_c/StructByReference.h +0 -50
  435. data/ext/ffi_c/libffi/.travis/build.sh +0 -34
  436. data/ext/ffi_c/libffi/.travis/install.sh +0 -22
  437. data/ext/ffi_c/libffi/.travis/site.exp +0 -18
  438. data/ext/ffi_c/libffi/.travis.yml +0 -34
  439. data/ext/ffi_c/libffi/ChangeLog.libffi +0 -584
  440. data/ext/ffi_c/libffi/ChangeLog.libgcj +0 -40
  441. data/ext/ffi_c/libffi/ChangeLog.v1 +0 -764
  442. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_uchar_va.c +0 -44
  443. data/ext/ffi_c/libffi/testsuite/libffi.call/cls_ushort_va.c +0 -44
  444. data/ext/ffi_c/win32/stdbool.h +0 -8
  445. data/ext/ffi_c/win32/stdint.h +0 -201
  446. data/samples/sample_helper.rb +0 -6
  447. data/ext/ffi_c/libffi/{.travis → .ci}/ar-lib +0 -0
  448. data/ext/ffi_c/libffi/{.travis → .ci}/compile +0 -0
  449. data/ext/ffi_c/libffi/testsuite/libffi.call/{pyobjc-tc.c → pyobjc_tc.c} +0 -0
  450. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_double.c +0 -0
  451. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_many_mixed_args.c +0 -0
  452. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/cls_many_mixed_float_double.c +0 -0
  453. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/err_bad_abi.c +0 -0
  454. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/problem1.c +0 -0
  455. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_large.c +0 -0
  456. data/ext/ffi_c/libffi/testsuite/{libffi.call → libffi.closures}/stret_large2.c +0 -0
  457. data/lib/ffi/platform/{sparc64-linux → sparcv9-linux}/types.conf +79 -79
data/ext/ffi_c/Function.c CHANGED
@@ -37,18 +37,13 @@
37
37
  #endif
38
38
 
39
39
  #include <stdio.h>
40
- #ifndef _MSC_VER
41
- # include <stdint.h>
42
- # include <stdbool.h>
43
- #else
44
- # include "win32/stdbool.h"
45
- # if !defined(INT8_MIN)
46
- # include "win32/stdint.h"
47
- # endif
48
- #endif
40
+ #include <stdint.h>
41
+ #include <stdbool.h>
49
42
  #include <ruby.h>
50
- #if defined(HAVE_RUBY_THREAD_H)
51
43
  #include <ruby/thread.h>
44
+
45
+ #if HAVE_RB_EXT_RACTOR_SAFE
46
+ #include <ruby/ractor.h>
52
47
  #endif
53
48
 
54
49
  #include <ffi.h>
@@ -67,13 +62,16 @@
67
62
  #include "Type.h"
68
63
  #include "LastError.h"
69
64
  #include "Call.h"
70
- #include "Closure.h"
65
+ #include "ClosurePool.h"
71
66
  #include "MappedType.h"
72
67
  #include "Thread.h"
73
68
  #include "LongDouble.h"
74
69
  #include "MethodHandle.h"
75
70
  #include "Function.h"
76
71
 
72
+ #define DEFER_ASYNC_CALLBACK 1
73
+
74
+ struct async_cb_dispatcher;
77
75
  typedef struct Function_ {
78
76
  Pointer base;
79
77
  FunctionType* info;
@@ -82,35 +80,46 @@ typedef struct Function_ {
82
80
  Closure* closure;
83
81
  VALUE rbProc;
84
82
  VALUE rbFunctionInfo;
83
+ #if defined(DEFER_ASYNC_CALLBACK)
84
+ struct async_cb_dispatcher *dispatcher;
85
+ #endif
85
86
  } Function;
86
87
 
87
- static void function_mark(Function *);
88
- static void function_free(Function *);
88
+ static void function_mark(void *data);
89
+ static void function_compact(void *data);
90
+ static void function_free(void *data);
91
+ static size_t function_memsize(const void *data);
89
92
  static VALUE function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc);
90
93
  static void callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data);
91
94
  static bool callback_prep(void* ctx, void* code, Closure* closure, char* errmsg, size_t errmsgsize);
92
95
  static void* callback_with_gvl(void* data);
93
- static VALUE invoke_callback(void* data);
94
- static VALUE save_callback_exception(void* data, VALUE exc);
95
-
96
- #define DEFER_ASYNC_CALLBACK 1
97
-
96
+ static VALUE invoke_callback(VALUE data);
97
+ static VALUE save_callback_exception(VALUE data, VALUE exc);
98
98
 
99
99
  #if defined(DEFER_ASYNC_CALLBACK)
100
100
  static VALUE async_cb_event(void *);
101
101
  static VALUE async_cb_call(void *);
102
102
  #endif
103
103
 
104
- #ifdef HAVE_RB_THREAD_CALL_WITH_GVL
105
- extern void *rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
106
- #endif
104
+ extern int ruby_thread_has_gvl_p(void);
105
+ extern int ruby_native_thread_p(void);
106
+
107
+ static const rb_data_type_t function_data_type = {
108
+ .wrap_struct_name = "FFI::Function",
109
+ .function = {
110
+ .dmark = function_mark,
111
+ .dfree = function_free,
112
+ .dsize = function_memsize,
113
+ ffi_compact_callback( function_compact )
114
+ },
115
+ .parent = &rbffi_pointer_data_type,
116
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
117
+ // macro to update VALUE references, as to trigger write barriers.
118
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | FFI_RUBY_TYPED_FROZEN_SHAREABLE
119
+ };
107
120
 
108
121
  VALUE rbffi_FunctionClass = Qnil;
109
122
 
110
- #if defined(DEFER_ASYNC_CALLBACK)
111
- static VALUE async_cb_thread = Qnil;
112
- #endif
113
-
114
123
  static ID id_call = 0, id_to_native = 0, id_from_native = 0, id_cbtable = 0, id_cb_ref = 0;
115
124
 
116
125
  struct gvl_callback {
@@ -120,7 +129,10 @@ struct gvl_callback {
120
129
  bool done;
121
130
  rbffi_frame_t *frame;
122
131
  #if defined(DEFER_ASYNC_CALLBACK)
132
+ struct async_cb_dispatcher *dispatcher;
123
133
  struct gvl_callback* next;
134
+
135
+ /* Signal when the callback has finished and retval is set */
124
136
  # ifndef _WIN32
125
137
  pthread_cond_t async_cond;
126
138
  pthread_mutex_t async_mutex;
@@ -132,22 +144,78 @@ struct gvl_callback {
132
144
 
133
145
 
134
146
  #if defined(DEFER_ASYNC_CALLBACK)
135
- static struct gvl_callback* async_cb_list = NULL;
147
+ struct async_cb_dispatcher {
148
+ /* the Ractor-local dispatcher thread */
149
+ VALUE thread;
150
+
151
+ /* single linked list of pending callbacks */
152
+ struct gvl_callback* async_cb_list;
153
+
154
+ /* Signal new entries in async_cb_list */
136
155
  # ifndef _WIN32
137
- static pthread_mutex_t async_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
138
- static pthread_cond_t async_cb_cond = PTHREAD_COND_INITIALIZER;
139
- # if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
140
- static int async_cb_pipe[2];
141
- # endif
156
+ pthread_mutex_t async_cb_mutex;
157
+ pthread_cond_t async_cb_cond;
142
158
  # else
143
- static HANDLE async_cb_cond;
144
- static CRITICAL_SECTION async_cb_lock;
145
- # if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
146
- static int async_cb_pipe[2];
147
- # endif
159
+ HANDLE async_cb_cond;
160
+ CRITICAL_SECTION async_cb_lock;
148
161
  # endif
149
- #endif
162
+ };
163
+
164
+ #if HAVE_RB_EXT_RACTOR_SAFE
165
+ static void
166
+ async_cb_dispatcher_mark(void *ptr)
167
+ {
168
+ struct async_cb_dispatcher *ctx = (struct async_cb_dispatcher *)ptr;
169
+ if (ctx) {
170
+ rb_gc_mark(ctx->thread);
171
+ }
172
+ }
150
173
 
174
+ static void
175
+ async_cb_dispatcher_free(void *ptr)
176
+ {
177
+ struct async_cb_dispatcher *ctx = (struct async_cb_dispatcher *)ptr;
178
+ if (ctx) {
179
+ xfree(ctx);
180
+ }
181
+ }
182
+
183
+ struct rb_ractor_local_storage_type async_cb_dispatcher_key_type = {
184
+ async_cb_dispatcher_mark,
185
+ async_cb_dispatcher_free,
186
+ };
187
+
188
+ static rb_ractor_local_key_t async_cb_dispatcher_key;
189
+
190
+ static struct async_cb_dispatcher *
191
+ async_cb_dispatcher_get(void)
192
+ {
193
+ struct async_cb_dispatcher *ctx = (struct async_cb_dispatcher *)rb_ractor_local_storage_ptr(async_cb_dispatcher_key);
194
+ return ctx;
195
+ }
196
+
197
+ static void
198
+ async_cb_dispatcher_set(struct async_cb_dispatcher *ctx)
199
+ {
200
+ rb_ractor_local_storage_ptr_set(async_cb_dispatcher_key, ctx);
201
+ }
202
+ #else
203
+ // for ruby 2.x
204
+ static struct async_cb_dispatcher *async_cb_dispatcher = NULL;
205
+
206
+ static struct async_cb_dispatcher *
207
+ async_cb_dispatcher_get(void)
208
+ {
209
+ return async_cb_dispatcher;
210
+ }
211
+
212
+ static void
213
+ async_cb_dispatcher_set(struct async_cb_dispatcher *ctx)
214
+ {
215
+ async_cb_dispatcher = ctx;
216
+ }
217
+ #endif
218
+ #endif
151
219
 
152
220
  static VALUE
153
221
  function_allocate(VALUE klass)
@@ -155,28 +223,39 @@ function_allocate(VALUE klass)
155
223
  Function *fn;
156
224
  VALUE obj;
157
225
 
158
- obj = Data_Make_Struct(klass, Function, function_mark, function_free, fn);
226
+ obj = TypedData_Make_Struct(klass, Function, &function_data_type, fn);
159
227
 
160
228
  fn->base.memory.flags = MEM_RD;
161
- fn->base.rbParent = Qnil;
162
- fn->rbProc = Qnil;
163
- fn->rbFunctionInfo = Qnil;
229
+ RB_OBJ_WRITE(obj, &fn->base.rbParent, Qnil);
230
+ RB_OBJ_WRITE(obj, &fn->rbProc, Qnil);
231
+ RB_OBJ_WRITE(obj, &fn->rbFunctionInfo, Qnil);
164
232
  fn->autorelease = true;
165
233
 
166
234
  return obj;
167
235
  }
168
236
 
169
237
  static void
170
- function_mark(Function *fn)
238
+ function_mark(void *data)
171
239
  {
172
- rb_gc_mark(fn->base.rbParent);
173
- rb_gc_mark(fn->rbProc);
174
- rb_gc_mark(fn->rbFunctionInfo);
240
+ Function *fn = (Function *)data;
241
+ rb_gc_mark_movable(fn->base.rbParent);
242
+ rb_gc_mark_movable(fn->rbProc);
243
+ rb_gc_mark_movable(fn->rbFunctionInfo);
175
244
  }
176
245
 
177
246
  static void
178
- function_free(Function *fn)
247
+ function_compact(void *data)
179
248
  {
249
+ Function *fn = (Function *)data;
250
+ ffi_gc_location(fn->base.rbParent);
251
+ ffi_gc_location(fn->rbProc);
252
+ ffi_gc_location(fn->rbFunctionInfo);
253
+ }
254
+
255
+ static void
256
+ function_free(void *data)
257
+ {
258
+ Function *fn = (Function *)data;
180
259
  if (fn->methodHandle != NULL) {
181
260
  rbffi_MethodHandle_Free(fn->methodHandle);
182
261
  }
@@ -188,6 +267,20 @@ function_free(Function *fn)
188
267
  xfree(fn);
189
268
  }
190
269
 
270
+ static size_t
271
+ function_memsize(const void *data)
272
+ {
273
+ const Function *fn = (const Function *)data;
274
+ size_t memsize = sizeof(Function);
275
+
276
+ // Would be nice to better account for MethodHandle and Closure too.
277
+ if (fn->closure) {
278
+ memsize += sizeof(Closure);
279
+ }
280
+
281
+ return memsize;
282
+ }
283
+
191
284
  /*
192
285
  * @param [Type, Symbol] return_type return type for the function
193
286
  * @param [Array<Type, Symbol>] param_types array of parameters types
@@ -265,12 +358,12 @@ VALUE
265
358
  rbffi_Function_ForProc(VALUE rbFunctionInfo, VALUE proc)
266
359
  {
267
360
  VALUE callback, cbref, cbTable;
268
- Function* fp;
269
361
 
270
362
  cbref = RTEST(rb_ivar_defined(proc, id_cb_ref)) ? rb_ivar_get(proc, id_cb_ref) : Qnil;
271
363
  /* If the first callback reference has the same function function signature, use it */
272
364
  if (cbref != Qnil && CLASS_OF(cbref) == rbffi_FunctionClass) {
273
- Data_Get_Struct(cbref, Function, fp);
365
+ Function* fp;
366
+ TypedData_Get_Struct(cbref, Function, &function_data_type, fp);
274
367
  if (fp->rbFunctionInfo == rbFunctionInfo) {
275
368
  return cbref;
276
369
  }
@@ -288,63 +381,82 @@ rbffi_Function_ForProc(VALUE rbFunctionInfo, VALUE proc)
288
381
  rb_ivar_set(proc, id_cb_ref, callback);
289
382
  } else {
290
383
  /* The proc instance has been used as more than one type of callback, store extras in a hash */
291
- cbTable = rb_hash_new();
292
- rb_ivar_set(proc, id_cbtable, cbTable);
384
+ if(cbTable == Qnil) {
385
+ cbTable = rb_hash_new();
386
+ rb_ivar_set(proc, id_cbtable, cbTable);
387
+ }
293
388
  rb_hash_aset(cbTable, rbFunctionInfo, callback);
294
389
  }
295
390
 
296
391
  return callback;
297
392
  }
298
393
 
394
+ #if !defined(_WIN32) && defined(DEFER_ASYNC_CALLBACK)
395
+ static void
396
+ after_fork_callback(void)
397
+ {
398
+ /* Ensure that a new dispatcher thread is started in a forked process */
399
+ async_cb_dispatcher_set(NULL);
400
+ }
401
+ #endif
402
+
299
403
  static VALUE
300
404
  function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc)
301
405
  {
302
406
  Function* fn = NULL;
303
- ffi_status ffiStatus;
304
407
 
305
- Data_Get_Struct(self, Function, fn);
408
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
306
409
 
307
- fn->rbFunctionInfo = rbFunctionInfo;
410
+ RB_OBJ_WRITE(self, &fn->rbFunctionInfo, rbFunctionInfo);
308
411
 
309
- Data_Get_Struct(fn->rbFunctionInfo, FunctionType, fn->info);
412
+ TypedData_Get_Struct(fn->rbFunctionInfo, FunctionType, &rbffi_fntype_data_type, fn->info);
310
413
 
311
414
  if (rb_obj_is_kind_of(rbProc, rbffi_PointerClass)) {
312
415
  Pointer* orig;
313
- Data_Get_Struct(rbProc, Pointer, orig);
416
+ TypedData_Get_Struct(rbProc, Pointer, &rbffi_pointer_data_type, orig);
314
417
  fn->base.memory = orig->memory;
315
- fn->base.rbParent = rbProc;
418
+ RB_OBJ_WRITE(self, &fn->base.rbParent, rbProc);
316
419
 
317
420
  } else if (rb_obj_is_kind_of(rbProc, rb_cProc) || rb_respond_to(rbProc, id_call)) {
318
- #if defined(DEFER_ASYNC_CALLBACK)
319
- if (async_cb_thread == Qnil) {
320
- #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)) && defined(_WIN32)
321
- _pipe(async_cb_pipe, 1024, O_BINARY);
322
- #elif !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
323
- pipe(async_cb_pipe);
324
- fcntl(async_cb_pipe[0], F_SETFL, fcntl(async_cb_pipe[0], F_GETFL) | O_NONBLOCK);
325
- fcntl(async_cb_pipe[1], F_SETFL, fcntl(async_cb_pipe[1], F_GETFL) | O_NONBLOCK);
326
- #endif
327
- async_cb_thread = rb_thread_create(async_cb_event, NULL);
421
+ if (fn->info->closurePool == NULL) {
422
+ fn->info->closurePool = rbffi_ClosurePool_New(sizeof(ffi_closure), callback_prep, fn->info);
423
+ if (fn->info->closurePool == NULL) {
424
+ rb_raise(rb_eNoMemError, "failed to create closure pool");
425
+ }
328
426
  }
329
427
 
428
+ #if defined(DEFER_ASYNC_CALLBACK)
429
+ {
430
+ struct async_cb_dispatcher *ctx = async_cb_dispatcher_get();
431
+ if (ctx == NULL) {
432
+ ctx = (struct async_cb_dispatcher*)ALLOC(struct async_cb_dispatcher);
433
+ ctx->async_cb_list = NULL;
434
+
435
+ #if !defined(_WIN32)
436
+ pthread_mutex_init(&ctx->async_cb_mutex, NULL);
437
+ pthread_cond_init(&ctx->async_cb_cond, NULL);
438
+ if( pthread_atfork(NULL, NULL, after_fork_callback) ){
439
+ rb_warn("FFI: unable to register fork callback");
440
+ }
441
+ #else
442
+ InitializeCriticalSection(&ctx->async_cb_lock);
443
+ ctx->async_cb_cond = CreateEvent(NULL, FALSE, FALSE, NULL);
330
444
  #endif
445
+ ctx->thread = rb_thread_create(async_cb_event, ctx);
331
446
 
332
- fn->closure = rbffi_Closure_Alloc();
333
- fn->closure->info = fn;
447
+ /* Name thread, for better debugging */
448
+ rb_funcall(ctx->thread, rb_intern("name="), 1, rb_str_new2("FFI Callback Dispatcher"));
334
449
 
335
- ffiStatus = ffi_prep_closure_loc(fn->closure->libffi_closure,
336
- &fn->info->ffi_cif, /* callback signature */
337
- callback_invoke,
338
- fn->closure, /* user_data for callback_invoke */
339
- fn->closure->libffi_trampoline);
340
- if (ffiStatus != FFI_OK) {
341
- rb_raise(rb_eRuntimeError, "ffi_prep_closure_loc in function_init failed. status=%#x",
342
- ffiStatus);
450
+ async_cb_dispatcher_set(ctx);
451
+ }
452
+ fn->dispatcher = ctx;
343
453
  }
454
+ #endif
344
455
 
345
- fn->base.memory.address = fn->closure->libffi_trampoline;
456
+ fn->closure = rbffi_Closure_Alloc(fn->info->closurePool);
457
+ fn->closure->info = fn;
458
+ fn->base.memory.address = fn->closure->code;
346
459
  fn->base.memory.size = sizeof(*fn->closure);
347
-
348
460
  fn->autorelease = true;
349
461
 
350
462
  } else {
@@ -352,7 +464,7 @@ function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc)
352
464
  rb_obj_classname(rbProc));
353
465
  }
354
466
 
355
- fn->rbProc = rbProc;
467
+ RB_OBJ_WRITE(self, &fn->rbProc, rbProc);
356
468
 
357
469
  return self;
358
470
  }
@@ -368,7 +480,7 @@ function_call(int argc, VALUE* argv, VALUE self)
368
480
  {
369
481
  Function* fn;
370
482
 
371
- Data_Get_Struct(self, Function, fn);
483
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
372
484
 
373
485
  return (*fn->info->invoke)(argc, argv, fn->base.memory.address, fn->info);
374
486
  }
@@ -384,9 +496,9 @@ static VALUE
384
496
  function_attach(VALUE self, VALUE module, VALUE name)
385
497
  {
386
498
  Function* fn;
387
- char var[1024];
388
499
 
389
- Data_Get_Struct(self, Function, fn);
500
+ StringValue(name);
501
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
390
502
 
391
503
  if (fn->info->parameterCount == -1) {
392
504
  rb_raise(rb_eRuntimeError, "cannot attach variadic functions");
@@ -402,12 +514,6 @@ function_attach(VALUE self, VALUE module, VALUE name)
402
514
  fn->methodHandle = rbffi_MethodHandle_Alloc(fn->info, fn->base.memory.address);
403
515
  }
404
516
 
405
- /*
406
- * Stash the Function in a module variable so it does not get garbage collected
407
- */
408
- snprintf(var, sizeof(var), "@@%s", StringValueCStr(name));
409
- rb_cv_set(module, var, self);
410
-
411
517
  rb_define_singleton_method(module, StringValueCStr(name),
412
518
  rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1);
413
519
 
@@ -429,7 +535,8 @@ function_set_autorelease(VALUE self, VALUE autorelease)
429
535
  {
430
536
  Function* fn;
431
537
 
432
- Data_Get_Struct(self, Function, fn);
538
+ rb_check_frozen(self);
539
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
433
540
 
434
541
  fn->autorelease = RTEST(autorelease);
435
542
 
@@ -441,11 +548,21 @@ function_autorelease_p(VALUE self)
441
548
  {
442
549
  Function* fn;
443
550
 
444
- Data_Get_Struct(self, Function, fn);
551
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
445
552
 
446
553
  return fn->autorelease ? Qtrue : Qfalse;
447
554
  }
448
555
 
556
+ static VALUE
557
+ function_type(VALUE self)
558
+ {
559
+ Function* fn;
560
+
561
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
562
+
563
+ return fn->rbFunctionInfo;
564
+ }
565
+
449
566
  /*
450
567
  * call-seq: free
451
568
  * @return [self]
@@ -456,7 +573,7 @@ function_release(VALUE self)
456
573
  {
457
574
  Function* fn;
458
575
 
459
- Data_Get_Struct(self, Function, fn);
576
+ TypedData_Get_Struct(self, Function, &function_data_type, fn);
460
577
 
461
578
  if (fn->closure == NULL) {
462
579
  rb_raise(rb_eRuntimeError, "cannot free function which was not allocated");
@@ -471,6 +588,7 @@ function_release(VALUE self)
471
588
  static void
472
589
  callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
473
590
  {
591
+ Function* fn;
474
592
  struct gvl_callback cb = { 0 };
475
593
 
476
594
  cb.closure = (Closure *) user_data;
@@ -478,39 +596,32 @@ callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
478
596
  cb.parameters = parameters;
479
597
  cb.done = false;
480
598
  cb.frame = rbffi_frame_current();
599
+ fn = (Function *) cb.closure->info;
481
600
 
482
601
  if (cb.frame != NULL) cb.frame->exc = Qnil;
483
- if (cb.frame != NULL && cb.frame->has_gvl) {
484
- callback_with_gvl(&cb);
485
602
 
486
- #if defined(HAVE_RB_THREAD_CALL_WITH_GVL)
487
- } else if (cb.frame != NULL) {
603
+ if (ruby_native_thread_p()) {
604
+ if(ruby_thread_has_gvl_p()) {
605
+ callback_with_gvl(&cb);
606
+ } else {
488
607
  rb_thread_call_with_gvl(callback_with_gvl, &cb);
489
- #endif
608
+ }
490
609
  #if defined(DEFER_ASYNC_CALLBACK) && !defined(_WIN32)
491
610
  } else {
492
611
  bool empty = false;
612
+ struct async_cb_dispatcher *ctx = fn->dispatcher;
493
613
 
494
614
  pthread_mutex_init(&cb.async_mutex, NULL);
495
615
  pthread_cond_init(&cb.async_cond, NULL);
496
616
 
497
- /* Now signal the async callback thread */
498
- pthread_mutex_lock(&async_cb_mutex);
499
- empty = async_cb_list == NULL;
500
- cb.next = async_cb_list;
501
- async_cb_list = &cb;
502
-
503
- #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
504
- pthread_mutex_unlock(&async_cb_mutex);
505
- /* Only signal if the list was empty */
506
- if (empty) {
507
- char c;
508
- write(async_cb_pipe[1], &c, 1);
509
- }
510
- #else
511
- pthread_cond_signal(&async_cb_cond);
512
- pthread_mutex_unlock(&async_cb_mutex);
513
- #endif
617
+ /* Now signal the async callback dispatcher thread */
618
+ pthread_mutex_lock(&ctx->async_cb_mutex);
619
+ empty = ctx->async_cb_list == NULL;
620
+ cb.next = ctx->async_cb_list;
621
+ ctx->async_cb_list = &cb;
622
+
623
+ pthread_cond_signal(&ctx->async_cb_cond);
624
+ pthread_mutex_unlock(&ctx->async_cb_mutex);
514
625
 
515
626
  /* Wait for the thread executing the ruby callback to signal it is done */
516
627
  pthread_mutex_lock(&cb.async_mutex);
@@ -524,25 +635,18 @@ callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
524
635
  #elif defined(DEFER_ASYNC_CALLBACK) && defined(_WIN32)
525
636
  } else {
526
637
  bool empty = false;
638
+ struct async_cb_dispatcher *ctx = fn->dispatcher;
527
639
 
528
640
  cb.async_event = CreateEvent(NULL, FALSE, FALSE, NULL);
529
641
 
530
- /* Now signal the async callback thread */
531
- EnterCriticalSection(&async_cb_lock);
532
- empty = async_cb_list == NULL;
533
- cb.next = async_cb_list;
534
- async_cb_list = &cb;
535
- LeaveCriticalSection(&async_cb_lock);
536
-
537
- #if !(defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL))
538
- /* Only signal if the list was empty */
539
- if (empty) {
540
- char c;
541
- write(async_cb_pipe[1], &c, 1);
542
- }
543
- #else
544
- SetEvent(async_cb_cond);
545
- #endif
642
+ /* Now signal the async callback dispatcher thread */
643
+ EnterCriticalSection(&ctx->async_cb_lock);
644
+ empty = ctx->async_cb_list == NULL;
645
+ cb.next = ctx->async_cb_list;
646
+ ctx->async_cb_list = &cb;
647
+ LeaveCriticalSection(&ctx->async_cb_lock);
648
+
649
+ SetEvent(ctx->async_cb_cond);
546
650
 
547
651
  /* Wait for the thread executing the ruby callback to signal it is done */
548
652
  WaitForSingleObject(cb.async_event, INFINITE);
@@ -553,6 +657,7 @@ callback_invoke(ffi_cif* cif, void* retval, void** parameters, void* user_data)
553
657
 
554
658
  #if defined(DEFER_ASYNC_CALLBACK)
555
659
  struct async_wait {
660
+ struct async_cb_dispatcher *dispatcher;
556
661
  void* cb;
557
662
  bool stop;
558
663
  };
@@ -560,113 +665,49 @@ struct async_wait {
560
665
  static void * async_cb_wait(void *);
561
666
  static void async_cb_stop(void *);
562
667
 
563
- #if defined(HAVE_RB_THREAD_BLOCKING_REGION) || defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
564
668
  static VALUE
565
- async_cb_event(void* unused)
669
+ async_cb_event(void* ptr)
566
670
  {
567
- struct async_wait w = { 0 };
671
+ struct async_cb_dispatcher *ctx = (struct async_cb_dispatcher *)ptr;
672
+ struct async_wait w = { ctx };
568
673
 
569
674
  w.stop = false;
570
675
  while (!w.stop) {
571
- #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL)
572
676
  rb_thread_call_without_gvl(async_cb_wait, &w, async_cb_stop, &w);
573
- #else
574
- rb_thread_blocking_region(async_cb_wait, &w, async_cb_stop, &w);
575
- #endif
576
677
  if (w.cb != NULL) {
577
678
  /* Start up a new ruby thread to run the ruby callback */
578
- rb_thread_create(async_cb_call, w.cb);
579
- }
580
- }
581
-
582
- return Qnil;
583
- }
584
-
585
- #elif defined(_WIN32)
586
- static VALUE
587
- async_cb_event(void* unused)
588
- {
589
- while (true) {
590
- struct gvl_callback* cb;
591
- char buf[64];
592
- fd_set rfds;
593
-
594
- FD_ZERO(&rfds);
595
- FD_SET(async_cb_pipe[0], &rfds);
596
- rb_thread_select(async_cb_pipe[0] + 1, &rfds, NULL, NULL, NULL);
597
- read(async_cb_pipe[0], buf, sizeof(buf));
598
-
599
- EnterCriticalSection(&async_cb_lock);
600
- cb = async_cb_list;
601
- async_cb_list = NULL;
602
- LeaveCriticalSection(&async_cb_lock);
603
-
604
- while (cb != NULL) {
605
- struct gvl_callback* next = cb->next;
606
- /* Start up a new ruby thread to run the ruby callback */
607
- rb_thread_create(async_cb_call, cb);
608
- cb = next;
679
+ VALUE new_thread = rb_thread_create(async_cb_call, w.cb);
680
+ /* Name thread, for better debugging */
681
+ rb_funcall(new_thread, rb_intern("name="), 1, rb_str_new2("FFI Callback Runner"));
609
682
  }
610
683
  }
611
684
 
612
685
  return Qnil;
613
686
  }
614
- #else
615
- static VALUE
616
- async_cb_event(void* unused)
617
- {
618
- while (true) {
619
- struct gvl_callback* cb;
620
- char buf[64];
621
-
622
- if (read(async_cb_pipe[0], buf, sizeof(buf)) < 0) {
623
- rb_thread_wait_fd(async_cb_pipe[0]);
624
- while (read(async_cb_pipe[0], buf, sizeof (buf)) < 0) {
625
- if (rb_io_wait_readable(async_cb_pipe[0]) != Qtrue) {
626
- return Qfalse;
627
- }
628
- }
629
- }
630
-
631
- pthread_mutex_lock(&async_cb_mutex);
632
- cb = async_cb_list;
633
- async_cb_list = NULL;
634
- pthread_mutex_unlock(&async_cb_mutex);
635
-
636
- while (cb != NULL) {
637
- struct gvl_callback* next = cb->next;
638
- /* Start up a new ruby thread to run the ruby callback */
639
- rb_thread_create(async_cb_call, cb);
640
- cb = next;
641
- }
642
- }
643
-
644
- return Qnil;
645
- }
646
- #endif
647
687
 
648
688
  #ifdef _WIN32
649
689
  static void *
650
690
  async_cb_wait(void *data)
651
691
  {
652
692
  struct async_wait* w = (struct async_wait *) data;
693
+ struct async_cb_dispatcher *ctx = w->dispatcher;
653
694
 
654
695
  w->cb = NULL;
655
696
 
656
- EnterCriticalSection(&async_cb_lock);
697
+ EnterCriticalSection(&ctx->async_cb_lock);
657
698
 
658
- while (!w->stop && async_cb_list == NULL) {
659
- LeaveCriticalSection(&async_cb_lock);
660
- WaitForSingleObject(async_cb_cond, INFINITE);
661
- EnterCriticalSection(&async_cb_lock);
699
+ while (!w->stop && ctx->async_cb_list == NULL) {
700
+ LeaveCriticalSection(&ctx->async_cb_lock);
701
+ WaitForSingleObject(ctx->async_cb_cond, INFINITE);
702
+ EnterCriticalSection(&ctx->async_cb_lock);
662
703
  }
663
704
 
664
- if (async_cb_list != NULL) {
665
- w->cb = async_cb_list;
666
- async_cb_list = async_cb_list->next;
705
+ if (ctx->async_cb_list != NULL) {
706
+ w->cb = ctx->async_cb_list;
707
+ ctx->async_cb_list = ctx->async_cb_list->next;
667
708
  }
668
709
 
669
- LeaveCriticalSection(&async_cb_lock);
710
+ LeaveCriticalSection(&ctx->async_cb_lock);
670
711
 
671
712
  return NULL;
672
713
  }
@@ -675,11 +716,12 @@ static void
675
716
  async_cb_stop(void *data)
676
717
  {
677
718
  struct async_wait* w = (struct async_wait *) data;
719
+ struct async_cb_dispatcher *ctx = w->dispatcher;
678
720
 
679
- EnterCriticalSection(&async_cb_lock);
721
+ EnterCriticalSection(&ctx->async_cb_lock);
680
722
  w->stop = true;
681
- LeaveCriticalSection(&async_cb_lock);
682
- SetEvent(async_cb_cond);
723
+ LeaveCriticalSection(&ctx->async_cb_lock);
724
+ SetEvent(ctx->async_cb_cond);
683
725
  }
684
726
 
685
727
  #else
@@ -687,21 +729,22 @@ static void *
687
729
  async_cb_wait(void *data)
688
730
  {
689
731
  struct async_wait* w = (struct async_wait *) data;
732
+ struct async_cb_dispatcher *ctx = w->dispatcher;
690
733
 
691
734
  w->cb = NULL;
692
735
 
693
- pthread_mutex_lock(&async_cb_mutex);
736
+ pthread_mutex_lock(&ctx->async_cb_mutex);
694
737
 
695
- while (!w->stop && async_cb_list == NULL) {
696
- pthread_cond_wait(&async_cb_cond, &async_cb_mutex);
738
+ while (!w->stop && ctx->async_cb_list == NULL) {
739
+ pthread_cond_wait(&ctx->async_cb_cond, &ctx->async_cb_mutex);
697
740
  }
698
741
 
699
- if (async_cb_list != NULL) {
700
- w->cb = async_cb_list;
701
- async_cb_list = async_cb_list->next;
742
+ if (ctx->async_cb_list != NULL) {
743
+ w->cb = ctx->async_cb_list;
744
+ ctx->async_cb_list = ctx->async_cb_list->next;
702
745
  }
703
746
 
704
- pthread_mutex_unlock(&async_cb_mutex);
747
+ pthread_mutex_unlock(&ctx->async_cb_mutex);
705
748
 
706
749
  return NULL;
707
750
  }
@@ -710,11 +753,12 @@ static void
710
753
  async_cb_stop(void *data)
711
754
  {
712
755
  struct async_wait* w = (struct async_wait *) data;
756
+ struct async_cb_dispatcher *ctx = w->dispatcher;
713
757
 
714
- pthread_mutex_lock(&async_cb_mutex);
758
+ pthread_mutex_lock(&ctx->async_cb_mutex);
715
759
  w->stop = true;
716
- pthread_cond_signal(&async_cb_cond);
717
- pthread_mutex_unlock(&async_cb_mutex);
760
+ pthread_cond_signal(&ctx->async_cb_cond);
761
+ pthread_mutex_unlock(&ctx->async_cb_mutex);
718
762
  }
719
763
  #endif
720
764
 
@@ -748,7 +792,7 @@ callback_with_gvl(void* data)
748
792
  }
749
793
 
750
794
  static VALUE
751
- invoke_callback(void* data)
795
+ invoke_callback(VALUE data)
752
796
  {
753
797
  struct gvl_callback* cb = (struct gvl_callback *) data;
754
798
 
@@ -811,10 +855,10 @@ invoke_callback(void* data)
811
855
  param = rb_float_new(*(double *) parameters[i]);
812
856
  break;
813
857
  case NATIVE_LONGDOUBLE:
814
- param = rbffi_longdouble_new(*(long double *) parameters[i]);
858
+ param = rbffi_longdouble_new(*(long double *) parameters[i]);
815
859
  break;
816
860
  case NATIVE_STRING:
817
- param = (*(void **) parameters[i] != NULL) ? rb_tainted_str_new2(*(char **) parameters[i]) : Qnil;
861
+ param = (*(void **) parameters[i] != NULL) ? rb_str_new2(*(char **) parameters[i]) : Qnil;
818
862
  break;
819
863
  case NATIVE_POINTER:
820
864
  param = rbffi_Pointer_NewInstance(*(void **) parameters[i]);
@@ -824,7 +868,6 @@ invoke_callback(void* data)
824
868
  break;
825
869
 
826
870
  case NATIVE_FUNCTION:
827
- case NATIVE_CALLBACK:
828
871
  case NATIVE_STRUCT:
829
872
  param = rbffi_NativeValue_ToRuby(paramType, rbParamType, parameters[i]);
830
873
  break;
@@ -883,9 +926,14 @@ invoke_callback(void* data)
883
926
  case NATIVE_FLOAT64:
884
927
  *((double *) retval) = NUM2DBL(rbReturnValue);
885
928
  break;
929
+ case NATIVE_LONGDOUBLE:
930
+ *((long double *) retval) = rbffi_num2longdouble(rbReturnValue);
931
+ break;
886
932
  case NATIVE_POINTER:
887
933
  if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
888
- *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
934
+ AbstractMemory* memory;
935
+ TypedData_Get_Struct(rbReturnValue, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
936
+ *((void **) retval) = memory->address;
889
937
  } else {
890
938
  /* Default to returning NULL if not a value pointer object. handles nil case as well */
891
939
  *((void **) retval) = NULL;
@@ -897,17 +945,21 @@ invoke_callback(void* data)
897
945
  break;
898
946
 
899
947
  case NATIVE_FUNCTION:
900
- case NATIVE_CALLBACK:
901
948
  if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
949
+ AbstractMemory* memory;
950
+ TypedData_Get_Struct(rbReturnValue, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
902
951
 
903
- *((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
952
+ *((void **) retval) = memory->address;
904
953
 
905
954
  } else if (rb_obj_is_kind_of(rbReturnValue, rb_cProc) || rb_respond_to(rbReturnValue, id_call)) {
906
955
  VALUE function;
956
+ AbstractMemory* memory;
907
957
 
908
958
  function = rbffi_Function_ForProc(rbReturnType, rbReturnValue);
909
959
 
910
- *((void **) retval) = ((AbstractMemory *) DATA_PTR(function))->address;
960
+ TypedData_Get_Struct(function, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
961
+
962
+ *((void **) retval) = memory->address;
911
963
  } else {
912
964
  *((void **) retval) = NULL;
913
965
  }
@@ -915,7 +967,11 @@ invoke_callback(void* data)
915
967
 
916
968
  case NATIVE_STRUCT:
917
969
  if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_StructClass)) {
918
- AbstractMemory* memory = ((Struct *) DATA_PTR(rbReturnValue))->pointer;
970
+ Struct* s;
971
+ AbstractMemory* memory;
972
+
973
+ TypedData_Get_Struct(rbReturnValue, Struct, &rbffi_struct_data_type, s);
974
+ memory = s->pointer;
919
975
 
920
976
  if (memory->address != NULL) {
921
977
  memcpy(retval, memory->address, returnType->ffiType->size);
@@ -938,7 +994,7 @@ invoke_callback(void* data)
938
994
  }
939
995
 
940
996
  static VALUE
941
- save_callback_exception(void* data, VALUE exc)
997
+ save_callback_exception(VALUE data, VALUE exc)
942
998
  {
943
999
  struct gvl_callback* cb = (struct gvl_callback *) data;
944
1000
 
@@ -948,6 +1004,21 @@ save_callback_exception(void* data, VALUE exc)
948
1004
  return Qnil;
949
1005
  }
950
1006
 
1007
+ static bool
1008
+ callback_prep(void* ctx, void* code, Closure* closure, char* errmsg, size_t errmsgsize)
1009
+ {
1010
+ FunctionType* fnInfo = (FunctionType *) ctx;
1011
+ ffi_status ffiStatus;
1012
+
1013
+ ffiStatus = ffi_prep_closure_loc(closure->pcl, &fnInfo->ffi_cif, callback_invoke, closure, code);
1014
+ if (ffiStatus != FFI_OK) {
1015
+ snprintf(errmsg, errmsgsize, "ffi_prep_closure_loc failed. status=%#x", ffiStatus);
1016
+ return false;
1017
+ }
1018
+
1019
+ return true;
1020
+ }
1021
+
951
1022
  void
952
1023
  rbffi_Function_Init(VALUE moduleFFI)
953
1024
  {
@@ -966,6 +1037,7 @@ rbffi_Function_Init(VALUE moduleFFI)
966
1037
  rb_define_method(rbffi_FunctionClass, "attach", function_attach, 2);
967
1038
  rb_define_method(rbffi_FunctionClass, "free", function_release, 0);
968
1039
  rb_define_method(rbffi_FunctionClass, "autorelease=", function_set_autorelease, 1);
1040
+ rb_define_private_method(rbffi_FunctionClass, "type", function_type, 0);
969
1041
  /*
970
1042
  * call-seq: autorelease
971
1043
  * @return [Boolean]
@@ -985,8 +1057,7 @@ rbffi_Function_Init(VALUE moduleFFI)
985
1057
  id_cb_ref = rb_intern("@__ffi_callback__");
986
1058
  id_to_native = rb_intern("to_native");
987
1059
  id_from_native = rb_intern("from_native");
988
- #if defined(_WIN32)
989
- InitializeCriticalSection(&async_cb_lock);
990
- async_cb_cond = CreateEvent(NULL, FALSE, FALSE, NULL);
1060
+ #if defined(DEFER_ASYNC_CALLBACK) && defined(HAVE_RB_EXT_RACTOR_SAFE)
1061
+ async_cb_dispatcher_key = rb_ractor_local_storage_ptr_newkey(&async_cb_dispatcher_key_type);
991
1062
  #endif
992
1063
  }