Re: [swift-users] Data races with copy-on-write

2017-12-06 Thread Romain Jacquinot via swift-users
Thank you Jordan. I’ve filed a bug at bugs.swift.org : https://bugs.swift.org/browse/SR-6543 . > On Dec 6, 2017, at 2:47 AM, Jordan Rose wrote: > > > >> On Dec 5, 2017, at 16:24, Romain Jacquinot

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Jordan Rose via swift-users
> On Dec 5, 2017, at 16:24, Romain Jacquinot wrote: > >> Ah, it's a Swift bug in the SynchronizedArray / MyClass cases > > If I remove the intermediate copy in the myArray getter, should it — without > Swift bug — also return a unique copy? > > public var myArray: Array {

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Romain Jacquinot via swift-users
Sorry, forgot the “return" keyword in my example: public var myArray: Array { lock.lock() defer { lock.unlock() } return _myArray } > On Dec 6, 2017, at 1:24 AM, Romain Jacquinot via swift-users > wrote: > > public var myArray: Array { > lock.lock() >

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Romain Jacquinot via swift-users
> Ah, it's a Swift bug in the SynchronizedArray / MyClass cases If I remove the intermediate copy in the myArray getter, should it — without Swift bug — also return a unique copy? public var myArray: Array { lock.lock() defer { lock.unlock() } _myArray } By the way, here is a

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Jordan Rose via swift-users
Ah, it's a Swift bug in the SynchronizedArray / MyClass cases, and your bug in the very first example with the global (since access to the global itself is not synchronized). The case I actually tested myself was with your most recent example ("Yet, data race can occur here"). Jordan > On

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Romain Jacquinot via swift-users
Hi Jordan, For which specific code sample(s) do you think it’s a bug? In the previous code samples, are there cases where you think a data race is to be expected? Thanks. > On Dec 6, 2017, at 12:05 AM, Jordan Rose wrote: > > I'm seeing the race too when compiling with

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Jordan Rose via swift-users
I'm seeing the race too when compiling with -O (and TSan enabled). I'm 95% sure this should be valid Swift code without any further synchronization, so please file a bug at https://bugs.swift.org. (But I'm only 95% sure because concurrency is hard.) Looking at the backtraces, it looks like one

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Romain Jacquinot via swift-users
Hi Jens, In the SynchronizedArray class, I use a lock to perform mutating operations on the array. However, my questions concern the case where an array is exposed as a public property of a thread-safe class, like this: public class MyClass { private var _myArray: Array = []

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Jens Alfke via swift-users
> On Dec 5, 2017, at 6:24 AM, Michel Fortin via swift-users > wrote: > > The array *storage* is copy on write. The array variable (which essentially > contains a pointer to the storage) is not copy on write. If you refer to the > same array variable from multiple

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Romain Jacquinot via swift-users
Thank you Michel. > Rather, use a different copy of the variable to each thread. How should I copy a variable to each thread? I’m not sure to understand what you mean by “copy” in this case. If I have this: var a = Array() var b = a do you consider “b” as being a copy of the variable “a”? >

Re: [swift-users] Data races with copy-on-write

2017-12-05 Thread Michel Fortin via swift-users
The array *storage* is copy on write. The array variable (which essentially contains a pointer to the storage) is not copy on write. If you refer to the same array variable from multiple threads, you have a race. Rather, use a different copy of the variable to each thread. Copied variables will