Simplified, refactored and unified (timed_)lock code based on try_lock(). There were several bugs in when handling timeout expirations.

[SVN r86511]