Make the DataOkHttpUploader state volatile#2305
Conversation
| ) : DataUploader { | ||
|
|
||
| private var attempts = 1 | ||
| private var attempts: Int = 1 |
There was a problem hiding this comment.
why not making this one as @Volatile or even AtomicInteger. There is no concurrency right now (so probably AtomicInteger is not quite useful), but for the changes at least shouldn't we declare volatile access?
There was a problem hiding this comment.
I was almost convinced that primitives are volatile in JVM. The docs are saying either true or false so let's do this also Volatile. There is no need for Atomicity there.
There was a problem hiding this comment.
Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
Reads and writes are atomic for all variables declared volatile (including long and double variables).
I'm not sure about ART though, but maybe it should maintain this as well.
There was a problem hiding this comment.
Also atomic read/write is different from cross-thread visibility though.
When a field is declared volatile, the compiler and runtime are put on notice that this variable is shared and that operations on it should not be reordered with other memory operations. Volatile variables are not cached in registers or in caches where they are hidden from other processors, so a read of a volatile variable always returns the most recent write by any thread.
There was a problem hiding this comment.
Yes there are 2 different things but I am pretty sure I've read also about volatility once, anyway I guess without going into the bytecode you will never now for sure.
5f8b999 to
3f3102b
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #2305 +/- ##
===========================================
- Coverage 70.32% 70.30% -0.02%
===========================================
Files 736 736
Lines 27466 27471 +5
Branches 4607 4607
===========================================
- Hits 19315 19312 -3
+ Misses 6871 6863 -8
- Partials 1280 1296 +16
|
What does this PR do?
Initially I thought that given the fact that the
DataOkHttpUploaderis being executed sequentially there is no need for thread safety in it. After a team discussion we stumbled upon this corner case where the executor could actually kill that thread and spawn a new one therefor we need to make sure these states are Volatile and kept in the Heap.Motivation
What inspired you to submit this pull request?
Additional Notes
Anything else we should know when reviewing?
Review checklist (to be filled by reviewers)