8

I'm trying to get the Thread ID which called the lock on the mutex object in Mac OS X. But the mutex object in Mac OS X doesn't seem to have the owner thread id of the mutex object.

definition of pthread_mutex_t in Linux :

typedef union
{
  struct __pthread_mutex_s
  {
    int __lock;
    unsigned int __count;
    int __owner;

...

} pthread_mutex_t;

definition of pthread_mutex_t in Mac OS X/FreeBSD :

struct _opaque_pthread_mutex_t
{
    long __sig;
    char __opaque[__PTHREAD_MUTEX_SIZE__]; 
};

As you can see there seems to be no info,which i can make use of to get the owning thread id of the mutex, or am i missing something ..?

In Mac OS X, how do i get the Mutex's owner ..? I have been going thru the pthread header files in Mac OS X and couldn't make anything useful. Please shed some light on this.

I Know there is a similar question : How can I debug mutex issues on Mac OS X?

But it's neither answered and i don't want to use lldb. I want the mutex's owner in my code itself to avoid recursive locks.

Community
  • 1
  • 1
Manikandaraj Srinivasan
  • 3,557
  • 5
  • 35
  • 62
  • 1
    You can allow recursive locking with `pthread_mutexattr_settype PTHREAD_MUTEX_RECURSIVE`. Without this, it is non-recursive. Also check it out, `PTHREAD_MUTEX_ERRORCHECK`. – 9dan May 27 '14 at 07:52

1 Answers1

0

The PThread-API does not allow to retrieve the thread which is holding a specific mutex.

For debugging purpose add extensive logging to the code, telling you which thread called pthread_mutex_lock()/pthread_mutex_unlock() on which mutex.

alk
  • 69,737
  • 10
  • 105
  • 255
  • You mean it in OS X alone right ..? It does so in Linux. – Manikandaraj Srinivasan May 27 '14 at 08:16
  • It does in Linux using which API-function? @ManikandarajS – alk May 27 '14 at 08:17
  • The Mutex Object itself will give you the owner thread id, "int __owner;" from the pthread_mutex_t struct. – Manikandaraj Srinivasan May 27 '14 at 08:20
  • 1
    Check out this Link : http://www.opensource.apple.com/source/Libc/Libc-167/pthreads.subproj/pthread_mutex.c and search for word 'owner' in the link. But that's not the pthread definition in OS X anymore. Lot has changed and i'm trying to figure out some way to get the owner of mutex object, like it used to be. – Manikandaraj Srinivasan May 27 '14 at 08:24
  • This is cheating, as one is not using the API. `mutex_t` along with the other types defined by the PThread-API are meant to be opaque. Their content shall not be accessed directly. @ManikandarajS – alk May 27 '14 at 08:25
  • 1
    What's wrong in accessing the data, if it's accessible ..? I have accessed the info and avoided recursive lock from same thread in Linux. I'm just looking for similar functionality in Linux. – Manikandaraj Srinivasan May 27 '14 at 08:30
  • @ManikandarajS: You might like to read this: http://en.wikipedia.org/wiki/Opaque_data_type – alk May 27 '14 at 08:33