Fixed locking bug on linux
The underlying lock mechanism on linux does not support upgrading locks from read to write, or vice-versa, and this sometimes causes bugs. The custom re-entrant and upgradeable lock code worked for acquiring the write lock when the read lock was already acquired, but not vice-versa (which is unnecessary, as write access is exclusive, so reading is always safe).
Also noticed a couple of other minor issues with the lock handling code.