Bug 17048 – [REG 2.071] Synchronized class methods give warnings for RMW operations
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-12-31T03:28:09Z
Last change time
2022-12-27T17:22:36Z
Assigned to
No Owner
Creator
kirsybuu
Comments
Comment #0 by kirsybuu — 2016-12-31T03:28:09Z
Synchronized class methods are implicitly (and undocumentedly?) typed as shared, thus they can only be called from shared instances of the class and DMD treats the bodies of their methods as shared even though they are used with mutual exclusion. A synchronized method should not be treated the same as other shared methods due to this guarantee, such as allowing the following safe code.
This depreciation warning is printed by DMD v2.072.1 and v2.071.0 but not v2.070.0.
$ cat sync.d
import std.stdio;
synchronized class Test {
private int x = 0;
int get() { return ++x; }
}
pragma(msg,typeof(Test.get));
unittest {
shared Test o = new Test();
writeln(o.get());
}
kirsybuu@kirsybuntu:~/Desktop/2017 Spring/Dlang Jan Meetup$ rdmd -unittest -main sync.d
shared int()
sync.d(4): Deprecation: read-modify-write operations are not allowed for shared variables. Use core.atomic.atomicOp!"+="(this.x, 1) instead.
1
Comment #1 by dfj1esp02 — 2017-02-20T10:41:10Z
Errm, deprecation is not a regression. Synchronized methods do not provide guarantee of mutually exclusive access. The synchronized method alone does use synchronization, but this tells nothing about other code, which may not use synchronization, also the data accessed is not necessarily unique.
Comment #2 by dfj1esp02 — 2017-02-21T12:54:59Z
Though it's a good illustration that hand-holding restrictions on shared types make them more painful to use even though people complain that shared is already difficult to use. More ceremony only make situation worse.