bailuk / AAT

Another Activity Tracker for Android
https://bailu.ch/aat
GNU General Public License v3.0
150 stars 41 forks source link

ContentDescription and thread-safety #86

Closed MaxKellermann closed 4 years ago

MaxKellermann commented 4 years ago

I was wondering about ContentDescription and its FF variable. FF is a helper which puts various thread-unsafe formatters into a ThreadLocal. So when ContentDescription instance is created, it obtains a FF instance for the current thread. Later, when ContentDescription methods are called, this FF instance is used, but who guarantees that the calling thread is the same one which created the ContentDescription?

If there is such a guarantee (or API constraint), it should be documented. I wishh there was more API documentation. I can write it while figuring out the AAT source code, but I need information, and so this issue aims to collect some information.

bailuk commented 4 years ago

I have no idea :-) No seriously. This is what FF looks like:

public class FF {
[...]
    private FF() {}

    private static final ThreadLocal<FF> F = new ThreadLocal<FF>() {
        @Override
        public FF initialValue() {
            return new FF();
        }
    };

    public static FF f() {
        return F.get();
    }

}

FF.f() returns an instance of FF for the current thread. Each thread has its own instance of FF. Now some ContentDescription classes save an instance of FF as a member variable. I did (naively) assume here that ContentDescription is always used in the UI-Thread. But you're right, there is no such guarantee.

bailuk commented 4 years ago

I hope this is fixed.