Bug 18474 – Postblit not working in shared structs
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-20T16:16:36Z
Last change time
2018-11-17T12:56:08Z
Assigned to
No Owner
Creator
Eduard Staniloiu
Comments
Comment #0 by edi33416 — 2018-02-20T16:16:36Z
The postblit for shared struct is not working as expected, as it is not infering the `shared` attribute.
For the snippet bellow
shared struct A
{
this(this);
}
struct B
{
A a;
}
void main()
{
shared B b1;
auto b2 = b1;
}
the compiler gives the following error:
Error: non-shared method test.B.__fieldPostblit is not callable using a shared object
The current workaround for this is to define `A` s postblit as
this(shared this);
Comment #1 by issues.dlang — 2018-02-22T06:09:05Z
I don't think that this is actually a bug. shared isn't inferred by anything let alone postlbit constructors, and fundamentally, the same code does not work as both thread-local and shared. And I don't know how safe copying is with shared in general. The copy needs to be thread-safe for it to work properly, but there are no mutexes involved when the struct is blitted. A mutex could then be used in the postblit constructor to protect it, but that gets a bit funny, because really, the it needs to be a mutex shared with the struct being copied in order to properly protect the shared reference types, and once the blitting has happened, you're mutating a copy. But fundamentally, I don't know that blitting the struct can even work properly with shared, since that needs to either be atomic or use a mutex, and AFAIK, it does neither.
Really, I think that copying structs is one of those areas where shared has not been fleshed out as well as it needs to be.
Comment #2 by dfj1esp02 — 2018-02-22T09:42:54Z
Some shared objects can be copied just fine like mutex itself or FILE*, but they don't need much of postblit. And if shared postblit is unlikely to work, you might want to @disable this(shared this) shared;
Comment #3 by razvan.nitu1305 — 2018-03-28T15:01:02Z