Prevent duplicate waiting threads in sema/mutex.

If something did a tight loop of a short wait/timeout, it might end
up getting on the waitingThreads list multiple times.

Eventually this caused a massive performance problem.
Merge pull request #304 from unknownbrackets/sema-timeout

Prevent duplicate waiting threads in sema/mutex