digitalmars.D.learn - Thread safety of AAs
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (7/7) May 15 2012 Hi,
- H. S. Teoh (13/19) May 15 2012 [...]
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (7/24) May 15 2012 Assuming the AA implementation only does aligned reads/writes, there
- H. S. Teoh (16/40) May 15 2012 [...]
- =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= (6/44) May 15 2012 See, this is why explicit deallocation of GC memory is bad. ;)
- Steven Schveighoffer (9/12) May 17 2012 nd =
Hi, Suppose that I have an AA that I'm doing lookups on from one thread, and writing to in another. Is this safe at all? Naturally, I'm willing to accept the data races involved, but the question is whether the concurrent lookup + mutation is guaranteed to be safe. -- - Alex
May 15 2012
On Wed, May 16, 2012 at 04:35:17AM +0200, Alex Rønne Petersen wrote:Hi, Suppose that I have an AA that I'm doing lookups on from one thread, and writing to in another. Is this safe at all? Naturally, I'm willing to accept the data races involved, but the question is whether the concurrent lookup + mutation is guaranteed to be safe.[...] Safe as in, no memory corruption? Or safe as in, the data will be consistent (barring any data races)? Memory safety I'm not sure, I _think_ it might be safe, but I have my doubts; data consistency, likely not, because you could potentially be reading partially-copied data (say the mutator was assigning new data to an existing key and the reader is reading that same data simultaneously; you may be seeing a partial copy of the new data intermixed with the old data). T -- Just because you survived after you did it, doesn't mean it wasn't stupid!
May 15 2012
On 16-05-2012 05:03, H. S. Teoh wrote:On Wed, May 16, 2012 at 04:35:17AM +0200, Alex Rønne Petersen wrote:As in no memory corruption.Hi, Suppose that I have an AA that I'm doing lookups on from one thread, and writing to in another. Is this safe at all? Naturally, I'm willing to accept the data races involved, but the question is whether the concurrent lookup + mutation is guaranteed to be safe.[...] Safe as in, no memory corruption? Or safe as in, the data will be consistent (barring any data races)?Memory safety I'm not sure, I _think_ it might be safe, but I have my doubts; data consistency, likely not, because you could potentially be reading partially-copied data (say the mutator was assigning new data to an existing key and the reader is reading that same data simultaneously; you may be seeing a partial copy of the new data intermixed with the old data).Assuming the AA implementation only does aligned reads/writes, there should be no problem with word tearing on any modern architecture. But I don't know if it does that...T-- - Alex
May 15 2012
On Wed, May 16, 2012 at 05:06:54AM +0200, Alex Rønne Petersen wrote:On 16-05-2012 05:03, H. S. Teoh wrote:[...] Hmm. Just noticed that the current aaA.d, in _aaDelX, after a slot is removed from the linked list gc_free is called on the slot. IIRC, if the mutator calls gc_free while the reader holds a reference to the slot, you may be accessing invalid memory. (E.g., reader looks up key being deleted, gets the pointer to that slot before the mutator does, then the CPU context-switches to the mutator, which calls gc_free, which cleans up that slot, now the reader has an invalid pointer.) I don't know if this will lead to memory corruption, but it sure looks dangerous to me.On Wed, May 16, 2012 at 04:35:17AM +0200, Alex Rønne Petersen wrote:As in no memory corruption.Hi, Suppose that I have an AA that I'm doing lookups on from one thread, and writing to in another. Is this safe at all? Naturally, I'm willing to accept the data races involved, but the question is whether the concurrent lookup + mutation is guaranteed to be safe.[...] Safe as in, no memory corruption? Or safe as in, the data will be consistent (barring any data races)?[...] If your data is larger than a word, you'd still have a problem, though. T -- Nothing in the world is more distasteful to a man than to take the path that leads to himself. -- Herman HesseMemory safety I'm not sure, I _think_ it might be safe, but I have my doubts; data consistency, likely not, because you could potentially be reading partially-copied data (say the mutator was assigning new data to an existing key and the reader is reading that same data simultaneously; you may be seeing a partial copy of the new data intermixed with the old data).Assuming the AA implementation only does aligned reads/writes, there should be no problem with word tearing on any modern architecture. But I don't know if it does that...
May 15 2012
On 16-05-2012 05:21, H. S. Teoh wrote:On Wed, May 16, 2012 at 05:06:54AM +0200, Alex Rønne Petersen wrote:See, this is why explicit deallocation of GC memory is bad. ;) I guess I might just resort to using an R/W mutex.On 16-05-2012 05:03, H. S. Teoh wrote:[...] Hmm. Just noticed that the current aaA.d, in _aaDelX, after a slot is removed from the linked list gc_free is called on the slot. IIRC, if the mutator calls gc_free while the reader holds a reference to the slot, you may be accessing invalid memory. (E.g., reader looks up key being deleted, gets the pointer to that slot before the mutator does, then the CPU context-switches to the mutator, which calls gc_free, which cleans up that slot, now the reader has an invalid pointer.) I don't know if this will lead to memory corruption, but it sure looks dangerous to me.On Wed, May 16, 2012 at 04:35:17AM +0200, Alex Rønne Petersen wrote:As in no memory corruption.Hi, Suppose that I have an AA that I'm doing lookups on from one thread, and writing to in another. Is this safe at all? Naturally, I'm willing to accept the data races involved, but the question is whether the concurrent lookup + mutation is guaranteed to be safe.[...] Safe as in, no memory corruption? Or safe as in, the data will be consistent (barring any data races)?It's OK in my case, since I'm just storing a pointer. -- - Alex[...] If your data is larger than a word, you'd still have a problem, though. TMemory safety I'm not sure, I _think_ it might be safe, but I have my doubts; data consistency, likely not, because you could potentially be reading partially-copied data (say the mutator was assigning new data to an existing key and the reader is reading that same data simultaneously; you may be seeing a partial copy of the new data intermixed with the old data).Assuming the AA implementation only does aligned reads/writes, there should be no problem with word tearing on any modern architecture. But I don't know if it does that...
May 15 2012
On Tue, 15 May 2012 22:35:17 -0400, Alex R=C3=B8nne Petersen = <xtzgzorex gmail.com> wrote:Hi, Suppose that I have an AA that I'm doing lookups on from one thread, a=nd =writing to in another. Is this safe at all?No. AA's are not a default-shared type. If you need a counter-case, just consider that your adding thread does a= = rehash when you are traversing in your reading thread. -Steve
May 17 2012