I am looking for "idiomatic" suggestions on this. Suppose there are many goroutines, processes and mutexes. I know want to ensure that the use of the locks are not bad. With "bad", I mean for example avoiding blocking, and that a lock shall never be requested for a specific mutex if it has already been given to the goroutine. I am not an expert on semaphore techniques, and the question (as explained below) is in some parts specific to Go.
What I have done is to create my own replacement of the "sync" package, with the same interface as "sync". It is an add-on to "sync", adding debug features. That way, I can simply import my own package instead of sync, and use the name "sync" in the important statement. I now have the possibility to produce statistics as well as some more advanced information (what function it was that released a lock I had to wait for).
I would like to be able to detect the case when a goroutine calls a lock twice (and thus blocks on itself), but I am uncertain on how to design such a test. Preferably, it should not change the interface from "sync", so that I easily can put the original "sync" back again when validation is finished. It may seem trivial to detect when a goroutine blocks itself, But not if you have a lot of them and do not keep perfect track of which ones are blocked from an expected behaviour and which ones of them are blocked from design errors.
For acquisition of more than one lock, I have a pre defined order that is supposed to be followed. I would like to be able to detect when this order is not adhered to by a goroutine.
I always use the same goroutine for lock and unlock, which should make it possible for some automatic testing.