Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@larskanis
Copy link
Member

@larskanis larskanis commented Apr 16, 2023

This adds support for use of FFI in a Ractor. It follows the philosophy of FFI to make using unsafe code as safe as possible. That means in case of Ractor the isolation is preserved for all FFI ruby objects, but the invoked C code can still bypass the isolation.

In a Ractor it's possible to:

  • load DLLs and call its functions, access its global variables
  • use builtin typedefs
  • use and modify ractor local typedefs
  • define callbacks
  • receive async callbacks from non-ruby threads
  • use frozen FFI::Library based modules with all attributes (enums, structs, typedefs, functions, callbacks)
  • invoke frozen functions and callbacks defined in the main Ractor
  • use FFI::Struct definitions from the main Ractor

In a Ractor it's impossible to:

  • create new FFI::Library based modules
  • create new FFI::Struct definitions
  • use custom global typedefs
  • use non-frozen FFI::Library based modules

This PR also adds methods for retrieval of parameter and return types of attached functions and variables as requested in #975 and it solves #992 by storing FFI::Function instances in @ffi_functions.

See the commit messages for more details.

Fixes #975
Closes #992

All objects are shareable now when frozen.
All objects can be created in a non-main Ractor.

Typedefs are a global mutable state and are not accessable from Ractor other than the main Ractor.
So all Function, Struct, etc. must be defined in the main Ractor and can then be used in other Ractors.
The callback is now routet to the Ractor, that created the FFI::Function .
Within this Ractor a new ruby thread is created to execute the Proc, like before.
No need to share per thread data object accross ractors.
Ractor-local custom typedefs are used now, since global writable typedefs aren't compatible with Ractor isolation.

Now the builtin typedefs are frozen and available for all Ractors.
But all custom typedefs are only per Ractor.

Actually the global typedefs already were useable in Ractors, if they are resolved in C code.
This is because the C code doesn't check the Ractor boundaries.
since there's no need to modify it later on
It is incompatible to our usage of instance variables by the Struct class.
@larskanis larskanis requested a review from eregon April 18, 2023 16:03
@larskanis larskanis force-pushed the ractor branch 2 times, most recently from 0cd7d7e to 744354b Compare April 26, 2023 12:55
@larskanis larskanis changed the title Make FFI Ractor compatible Make FFI Ractor compatible and allow type retrieval of attached functions+variables Apr 26, 2023
larskanis added a commit to larskanis/truffleruby that referenced this pull request Apr 26, 2023
This is the corresponding change to revealed type information introduced in
  ffi/ffi#1023
…o be attached

This allows to freeze the FFI::Function immediately, so that it is shareable by Ractor without the need to freeze the module explicit.
To make it shareable the typedef hash used for variadic functions is duplicated and made frozen.

This creates a small compatibility issue:
Only typedefs defined above the variadic function can be used by that function.
If a typedef is created after the definition of the variadic function, then this typedef can no longer be used as parameter to that variadic function.

Also fix the retrieval of simple (non-struct) global variables per #attached_variables.

Closes ffi#975
The term "return_type" is already used in Truffleruby and sounds better to me.
On the other hand the term "result_type" was only used in C-ffi as FunctionInfo#result_type, which is an internal class only.
Shareable instance variables on modules are essential for most Ractor specs.
They were introduced in ruby-3.1.
And remove support for older rubies.
larskanis added a commit to larskanis/truffleruby that referenced this pull request Apr 27, 2023
This is the corresponding change to revealed type information introduced in
  ffi/ffi#1023
larskanis added a commit to larskanis/truffleruby that referenced this pull request Apr 27, 2023
This is the corresponding change to revealed type information introduced in
  ffi/ffi#1023
@larskanis larskanis mentioned this pull request Apr 27, 2023
larskanis added 3 commits May 2, 2023 11:53
…ary definitions Ractor usable

Freeze objects which are immutable from the start are now freezed.
This allows these objects to be used by Ractor without make_shareable.

This partially reverts commit 4fc6a8c in such a way, that functions are stored in a module variable @ffi_functions.
Enums are implemented per FFI::Library not per attached function.
To make them shareable they would have to be copied and freezed per function.
This would increase memory footprint for the sake of Ractor support.

IMHO it's better to mark the module explicit as finished by .freeze to allow its use in a Ractor.
This also enables querying the enum definitions from a Ractor.

Using a Hash instead of per-function variables allow to use foo! and bar? methods as wished in ffi#971.

Fixes ffi#971
For consistency and to ease proting to JRuby and Truffleruby.
larskanis added a commit to larskanis/truffleruby that referenced this pull request May 8, 2023
This is the corresponding change to revealed type information introduced in
  ffi/ffi#1023
@larskanis larskanis merged commit c6b7139 into ffi:master May 8, 2023
john-heinnickel pushed a commit to thermofisher-jch/truffleruby that referenced this pull request Aug 16, 2023
This is the corresponding change to revealed type information introduced in
  ffi/ffi#1023
kojix2 added a commit to kojix2/ruby-htslib that referenced this pull request Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants