开发者

Finding out statuses of active threads from the current thread. (Implementation of Robust Mutexes)

We are trying to port some code from Solaris to HPUX. While Solaris uses it's own thread API, HPUX uses the Pthread API. One problem that we faced during migration is that Robust mutexes are not being implemented on HPUX, since it was not required to be implemented to remain POSIX compliant.

We tried us开发者_运维知识库ing pthread_mutex_trylock to make the threads in the wait queue to not block. We want some way to find out if the thread which is the owner of the mutex is alive. How do I get the status of that thread from a calling thread?

Thanks a ton in advance, Aditya.


Unfortunately Anthony's approach has multiple race conditions:

  1. There is an interval of time between a thread acquiring the mutex and storing itself as the owner. If another thread fails at trylock and wants to check the owner, it might find the owner still unwritten. In this case it could wait in hopes that the owner will write its identity, but if the owner died at just the right (wrong) moment, waiting will deadlock.
  2. Whatever owner identifier is being stored with the mutex could be reassigned after the owner dies. Note that storing just a thread id would be useless since thread ids are not unique across processes and robust mutex semantics are only meaningful for process-shared mutexes. (Within a single process you have full control over thread termination; they cannot terminate unpredictably so you can just have threads properly clean up and release their mutexes when they exit.) If you're using PIDs, the only situation where you can avoid a PID being reassigned is if the process is your own child process and you haven't waited on it yet.

Proper implementation of robust mutexes really does require assistance from the kernel to avoid race conditions. Of course you could use nasty slow techniques where every lock and unlock operation require a syscall to emulate robust mutexes on a system without proper futex-like robust mutex support, e.g. using fcntl locking or SysV semaphores, both of which have semantics by which automatic and atomic unlocking owner registration could be achieved...


In general, you cannot tell which threads owns a mutex, never mind whether it is alive or not.

If you wish to know this information then you will have to store it yourself --- immediately after locking a mutex store the thread ID somewhere, and then clear that value before unlocking the mutex. These stores will have to either be atomic, or be protected by a mutex themselves (obviously, if you use a mutex then you can't check for problems with this mutex or you'll have a recursion problem). You can then use this stored thread ID to check whether the thread is alive.

This assumes that (a) your thread won't die between acquiring the mutex and setting the owner, and (b) the thread ID is not reused. It also glosses over the "check whether the thread is alive" part --- this itself is not trivial.

If you're assuming that a thread might die at any point then building "robust mutexes" is not really possible without OS support.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜