pthread_mutex_t struct:lock代表什么?

时间:2014-05-03 20:33:31

标签: locking pthreads posix mutex mutual-exclusion

我正在查看pthreadtypes.h文件中的pthread_mutex_t结构。 “__lock”代表什么?它是否像分配给互斥锁的锁号一样?

typedef union
{
  struct __pthread_mutex_s
  {
     int __lock;
     unsigned int __count;
     int __owner;
     #if __WORDSIZE == 64
     unsigned int __nusers;
     #endif
    /* KIND must stay at this position in the structure to maintain
     binary compatibility.  */
     int __kind;
     #if __WORDSIZE == 64
     int __spins;
     __pthread_list_t __list;
     # define __PTHREAD_MUTEX_HAVE_PREV 1
     #else
     unsigned int __nusers;
     __extension__ union
    {
      int __spins;
      __pthread_slist_t __list;
    };
    #endif
  } __data;
 char __size[__SIZEOF_PTHREAD_MUTEX_T];
 long int __align;

} pthread_mutex_t;

1 个答案:

答案 0 :(得分:3)

__lock struct __pthread_mutex_s的{​​{1}}成员用作Linux上的futex对象。以下许多细节可能会有所不同,具体取决于您所查看的架构:

请参阅__data代码,了解pthread互斥锁的高级锁定功能 - pthread_mutex_lock.c,通常最终会调用__pthread_mutex_lock()以及LLL_MUTEX_LOCK()和朋友的定义,最终在LLL_MUTEX_LOCK()中调用lll_lock()等。

lowlevellock.h宏依次调用lll_lock(),调用__lll_lock_wait_private(),进行lll_futex_wait()系统调用。