lib: use less unsafe for mutexes
This removes the unsafe use of unsafe for mutexes by replacing uintptr/unsafe.Pointer conversions with a numbering scheme. It is done in two commits, in case the second one is unwanted. (I would choose to keep them both, personally.)
This should fix #123 (closed). However, it may very plausibly cause a performance regression; I have not benchmarked.