GVariant: add internal tree-form locking helper
Many of the core GVariant operations have two modes: one for tree-form
and one for serialised.
Once a GVariant is in serialised form it will always be serialised, so
it is safe to simply check for that and proceed with the operation in
that case.
A tree-form GVariant instance always has a chance of being implicitly
serialised, however, so we have to take locks when performing operations
on these.
Write a helper function that reliably checks if the instance is in
tree-form, locking it if it is. Rewrite some of the other functions to
use this helper. In some cases this simplifies the code and in others
it reduces locking.