Skip to content

Conversation

@andersio
Copy link
Member

Replace the manual allocation of UnsafeAtomicState with withUnsafeMutablePointer.

return OSAtomicCompareAndSwap32Barrier(expected.rawValue,
next.rawValue,
valuePointer)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is valid.

This:

The pointer argument to body is valid only for the lifetime of the closure. Do not escape it from the closure for later use.

Makes me think that the whole construct may not be atomic.

Copy link
Member Author

@andersio andersio May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did check the compiler source and generated object code in #342. The compiler differentiates kinds of variable storage, and maps them into different access semantics. The most relevant access semantic to this is DirectToStorage. On top of this, withUnsafeMutablePointer is just the unsafe form of the inout model.

IOW as long as it is stored property that is statically dispatched or a variable, it is atomic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure this is something we should rely on. 😕

This is the danger of OSS: can you depend on undocumented implementation details?

Copy link
Member Author

@andersio andersio May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this bit being dropped in the foreseeable future. It is essential, well-known part of the inout model since Swift 1.0. Better yet, Swift is OSS, so you can also say that the compiler source code already serves as the documentation, and we would always know ahead if anything could break it.

😅

@andersio andersio closed this May 12, 2017
@andersio andersio deleted the unsafe-atomic-state branch May 12, 2017 14:09
@andersio
Copy link
Member Author

I guess it'd be better to follow Apple's suit.

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.

3 participants