rspec/rules/S3475/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

72 lines
1.4 KiB
Plaintext

== Why is this an issue?
Primitives can be read and written to atomically. Except for ``++long++`` and ``++double++``, that is. These 64-bit primitives must be marked ``++volatile++`` in multi-threaded environments, or swapped out for their atomic counterparts: ``++AtomicLong++``, and ``++AtomicDouble++`` to guarantee that their updates are always visible to other threads.
Similarly, to ensure that updates to 32-bit primitives are visible to all threads, they should also be marked ``++volatile++``.
=== Noncompliant code example
[source,java]
----
long m = 0; // Noncompliant
public void increment() {
m++;
}
----
=== Compliant solution
[source,java]
----
volatile long m = 0;
public void increment() {
m++;
}
----
or
[source,java]
----
AtomicLong m = new AtomicLong(0);
public void increment() {
m.incrementAndGet();
}
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/QTdGBQ[CERT, VNA00-J.] - Ensure visibility when accessing shared primitive variables
* https://wiki.sei.cmu.edu/confluence/x/zzdGBQ[CERT, VNA05-J.] - Ensure atomicity when reading and writing 64-bit values
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Mark this "xxx" "volatile" or use an "AtomicXxx" instead.
=== Highlighting
``++long|double xxx++``
'''
== Comments And Links
(visible only on this page)
=== relates to: S3687
endif::env-github,rspecator-view[]