[Vala] Locking null references



In Vala, I can lock a null reference.  Is this by design or a side-effect?

class Xyzzy {
    private File file = null;

    public File get_file() {
        File f;
        lock (file) {
            if (file == null)
                file = File.new_for_path("/tmp");

            f = file;
        }

        return f;
    }
}

void main() {
    Xyzzy x = new Xyzzy();
    stdout.printf("%s\n", x.get_file().get_path());
    stdout.printf("%s\n", x.get_file().get_path());
    stdout.printf("%s\n", x.get_file().get_path());
}

I think this is fine and should be the documented behavior.  Because Vala
only allows locking member variables (and not "this"), being unable to lock
a nulled reference would require allocating a dummy object to lock against
or explicitly using a Mutex and forgoing lock().  I find both unappealing.

In other words, I think this is okay because it's locking access to the
reference and not locking the object itself.  This seems right to me -- but
I'd feel better if I knew this won't go away in the future.

-- Jim


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]