Skip to content

Add _withUnsafeGuaranteedRef to Unmanaged #1890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

aschwaighofer
Copy link
Contributor

What's in this pull request?

This is an internal method that will go away in the future.

Get the value of the unmanaged referenced as a managed reference without
consuming an unbalanced retain of it and pass it to the closure. Asserts
that there is some other reference ('the owning reference') to the
unmanaged reference that guarantees the lifetime of the unmanaged reference
for the duration of the '_withUnsafeGuaranteedRef' call.

  var owningReference = Instance()
  ... 
  withFixedLifetime(owningReference) {
    let u = Unmanaged.passUnretained(owningReference)

    for i in 0 ..< 100 {

      // Assert that the reference in 'u' is kept alive by another storage
      // location or value that holds the same reference.
      u._withUnsafeGuaranteedRef {
        $0.doSomething()
      }

    } 
  } // owningReference's lifetime (reference count > 0) is guaranteed

@aschwaighofer
Copy link
Contributor Author

@swift-ci Please smoke test

@gribozavr
Copy link
Contributor

Please add an execution test.

@aschwaighofer aschwaighofer force-pushed the unsafe_guaranteed_prototype branch 2 times, most recently from 492540f to c3f2ea9 Compare March 27, 2016 02:08
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please test

@aschwaighofer aschwaighofer force-pushed the unsafe_guaranteed_prototype branch from c3f2ea9 to 0a7c539 Compare March 27, 2016 13:06
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please smoke test linux platform

@aschwaighofer aschwaighofer force-pushed the unsafe_guaranteed_prototype branch from 0a7c539 to fc103bc Compare March 27, 2016 13:29
@aschwaighofer
Copy link
Contributor Author

@swift-ci Please smoke test linux platform

These builtins can be used to denote a scope over which the parameter passed to
unsafeGuaranteed can be assume to be @guaranteed: That is there is another
reference that keeps the value alive.

  // Scope begin
  strong_retain %0 : $Foo
  %2 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
  %3 = tuple_extract %2 : $(Foo, Builtin.Int8), 0
  %4 = tuple_extract %2 : $(Foo, Builtin.Int8), 1
  %5 = class_method %3 : $Foo, #Foo.beep!1 : (Foo) -> () -> ()
  %6 = apply %5(%3) : $@convention(method) (@guaranteed Foo) -> ()
  strong_release %3 : $Foo
  %8 = builtin "unsafeGuaranteedEnd"(%4 : $Builtin.Int8) : $(
  // Scope end

is semantically equivalent to:

  sil @guaranteed_lifetime : $@convention(method) (@guaranteed Foo) -> () {
  bb(%3: $Foo):
    %5 = class_method %3 : $Foo, #Foo.beep!1 : (Foo) -> () -> ()
    %6 = apply %5(%3) : $@convention(method) (@guaranteed Foo) -> ()
  }

  ...

  // Scope begin
  apply @guaranteed_lifetime(%0)
  // Scope end
We can remove the retain/release pair preceeding the builtins based on the
knowledge that the lifetime of the reference is guaranteed by someone hanging on
to the reference elsewhere.
This is an internal method that will go away in the future.

Get the value of the unmanaged referenced as a managed reference without
consuming an unbalanced retain of it and pass it to the closure. Asserts
that there is some other reference ('the owning reference') to the
unmanaged reference that guarantees the lifetime of the unmanaged reference
for the duration of the '_withUnsafeGuaranteedRef' call.

  var owningReference = Instance()
  ...
  withExtendedLifetime(owningReference) {
    let u = Unmanaged.passUnretained(owningReference)

    for i in 0 ..< 100 {

      // Assert that the reference in 'u' is kept alive by another storage
      // location or value that holds the same reference.
      u._withUnsafeGuaranteedRef {
        $0.doSomething()
      }

    }
  } // owningReference's lifetime (reference count > 0) is guaranteed
    // for this scope.

rdar://25129935
@aschwaighofer aschwaighofer force-pushed the unsafe_guaranteed_prototype branch from fc103bc to d454c93 Compare March 27, 2016 13:48
@aschwaighofer aschwaighofer merged commit 30bd7bb into swiftlang:master Mar 27, 2016
MaxDesiatov pushed a commit that referenced this pull request Oct 13, 2020
[pull] swiftwasm-release/5.3 from release/5.3
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