Mutex-protect our singleton.
  Updated to allow the code to be loaded as a library.