Bug 24269 – Members inside synchronized method should be only tail shared
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-12-03T21:18:20Z
Last change time
2023-12-28T10:13:11Z
Keywords
TDPL
Assigned to
No Owner
Creator
Nick Treleaven
Comments
Comment #0 by nick — 2023-12-03T21:18:20Z
TDPL:
https://www.informit.com/articles/article.aspx?p=1609144&seqNum=14
"typing of fields of a synchronized class inside a method goes as follows:
All numeric types are not shared (they have no tail) so they can be manipulated normally.
Array fields declared with type T[] receive type shared(T)[]; that is, the head (the slice limits) is not shared and the tail (the contents of the array) remains shared.
Pointer fields declared with type T* receive type shared(T)*; that is, the head (the pointer itself) is not shared and the tail (the pointed-to data) remains shared.
Class fields declared with type T receive type shared(T). Classes are automatically by-reference, so they're "all tail."
"
And should the same apply to `obj`'s fields inside a `synchronized(obj) statement;` when `obj` is `shared`?
Comment #1 by nick — 2023-12-03T21:23:09Z
class C
{
int i;
synchronized void bar() {
pragma(msg, typeof(i)); // shared(int)
}
}
The pragma should print `int`.
Comment #2 by dfj1esp02 — 2023-12-28T10:13:11Z
It can't, because you can have unlocked methods:
---
class C
{
int i;
synchronized void bar() {
pragma(msg, typeof(i)); // shared(int)
}
void inc() shared {
atomicOp!"+="(i, 1); //expects cooperation
}
}
---