Skip to content
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

Swap is not atomic, is this by design? #51

Open
bkase opened this issue Jun 11, 2017 · 2 comments
Open

Swap is not atomic, is this by design? #51

bkase opened this issue Jun 11, 2017 · 2 comments

Comments

@bkase
Copy link
Member

bkase commented Jun 11, 2017

/// Atomically, take a value from the `MVar`, put a given new value in the
/// `MVar`, then return the `MVar`'s old value.
public func swap(_ x : A) -> A {
	let old = self.take()
	self.put(x)
	return old
}

The thread using swap might context-switch right after the take completes before the put -- if another thread puts then, this put will block.

@CodaFi
Copy link
Member

CodaFi commented Sep 22, 2017

Aha, you're absolutely correct! The atomicity of this is dependent on exclusive putters. We could implement this atomically by making it a primitive (taking all those locks and junk), but for now I'm just going to use #52 to update the docs.

Keeping this issue open as a feature request for a truly atomic swap.

@CodaFi
Copy link
Member

CodaFi commented Sep 28, 2017

Resolved by the merge of #52

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

No branches or pull requests

2 participants