Add a lock for folks using threading. Mucho helpful.

I can't see any *downside* to including a lock here. Basically, it
just says that two threads running in parallel can't touch the code
between the lock.acquire() <--> lock.release() statements
concurrently. We use it in low-level util functions to prevent
concurrent file reads/writes.

This was a bug that showed up while running our auto-documentation
examples.
whoops. we assume this directory is accessible here. I'm pretty sure there's a check for this higher-up, but that's not obvious and should be changed.