Making reference bookkeeping thread safe

Additionally added a threadsafety test which we unfortunately can't
make use of at the moment due to the fact that Scala reflection is
currently not thread safe. Fortunately, we're only a week or so away
from thread safe reflection fully merged into scala/master.