[threadsafety] unsynched reading from main thread

Reviewed By: sblackshear

Differential Revision: D4729224

fbshipit-source-id: 576d001
master
Peter O'Hearn 8 years ago committed by Facebook Github Bot
parent 434cfbfb15
commit 089600bdcd

@ -43,6 +43,34 @@ class RaceWithMainThread{
x = f; x = f;
} }
/*There is a particularly subtle idiom which avoids races, where a
variable can be read without protection on the main thread, if
it is written with protection on the main thread and read with
protection off. The next three methods do this safely, and the fourth
unsafely.
*/
Integer i;
void protected_write_on_main_thread_OK() {
o.assertMainThread();
synchronized (this) {
i = 99;
}
}
void unprotected_read_on_main_thread_OK() {
Integer x;
o.assertMainThread();
x = i;
}
void protected_read_off_main_thread_OK() {
Integer x;
synchronized (this) {
x = i;
}
}
/*TODO: There should be a warning either here or in main_thread_OK() /*TODO: There should be a warning either here or in main_thread_OK()
or maybe even in both.*/ or maybe even in both.*/
void read_protected_unthreaded_Bad_FN(){ void read_protected_unthreaded_Bad_FN(){

Loading…
Cancel
Save