pthread_mutex_t 结构:锁代表什么?

Fra*_*nXh 5 posix mutex locking pthreads 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;
Run Code Online (Sandbox Code Playgroud)

pthread_mutex_t;

Mic*_*urr 4

__lock的成员在struct __pthread_mutex_s __dataLinux 上用作 futex 对象。以下许多详细信息可能会有所不同,具体取决于您所查看的架构:

请参阅pthread_mutex_lock.cpthread 互斥体的高级锁定函数的代码 - __pthread_mutex_lock(),它通常最终会调用LLL_MUTEX_LOCK()以及 和 的定义LLL_MUTEX_LOCK(),最终会调用lll_lock()lowlevellock.h

lll_lock()依次调用__lll_lock_wait_private(),后者调用lll_futex_wait(),后者进行sys_futex系统调用。