Add locking primitives for concurrency; add rudimentary test; fix file h...

...andle leak