Ok, now I strongly suspect a bug in the FreeBSD threading library you are using.
The C equivalent of the test to this point is pretty simple:
Code:
pthread_mutex_init(...);
pthread_mutex_lock(...);
pthread_mutex_unlock(...);
pthread_mutex_trylock(...);
The call to trylock should not be returning EDEADLK in this case, since the mutex has clearly been unlocked.
Perhaps you should submit this test to the FreeBSD folks as a bug report.
Take care,
- Mark