made access to translation units threadsafe

also prevented transaltion unit generation from running too often.
Optimized generation of translation unit.

It will now only be generated when accessed, if the application marked
it as dirty. It is also protected for threadsafe access.