pthread_mutex_t的源码求助,类型的定义的问题

iceeajp 2013-12-21 05:23:56
今天在看pthread_mutex_init的源码的时候,发现pthread_mutex_t 的使用出现了我不可理解的两种不同的定义。
追踪pthread.h里面 pthread_mutex_t的定义,在_types.h里找到的如下:
struct _opaque_pthread_mutex_t { long __sig; char __opaque[__PTHREAD_MUTEX_SIZE__]; };


但是在pthread_mutex_init函数中却使用了 pthread_mutex_t mx; mx->lock_idx ,
代码中把 pthread_mutex_t 当做指针来使用了。。
这是为什么呢??求各位大神指导。

pthread_mutex_init的实现代码如下:




#include "pthread.h"
#include "implement.h"


int
pthread_mutex_init (pthread_mutex_t * mutex, const pthread_mutexattr_t * attr)
{
int result = 0;
pthread_mutex_t mx;

if (mutex == NULL)
{
return EINVAL;
}

if (attr != NULL
&& *attr != NULL && (*attr)->pshared == PTHREAD_PROCESS_SHARED)
{
/**
* Creating mutex that can be shared between
* processes.
*/
#if _POSIX_THREAD_PROCESS_SHARED >= 0

/**
* Not implemented yet.
*/

#error ERROR [__FILE__, line __LINE__]: Process shared mutexes are not supported yet.

#else

return ENOSYS;

#endif /** _POSIX_THREAD_PROCESS_SHARED */

}

mx = (pthread_mutex_t) calloc (1, sizeof (*mx));

if (mx == NULL)
{
result = ENOMEM;
}
else
{
mx->lock_idx = 0;
mx->recursive_count = 0;
mx->kind = (attr == NULL || *attr == NULL
? PTHREAD_MUTEX_DEFAULT : (*attr)->kind);
mx->ownerThread.p = NULL;

mx->event = CreateEvent (NULL, PTW32_FALSE, /** manual reset = No */
PTW32_FALSE, /** initial state = not signaled */
NULL); /** event name */

if (0 == mx->event)
{
result = ENOSPC;
free (mx);
mx = NULL;
}
}

*mutex = mx;

return (result);
}
...全文
654 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
iceeajp 2013-12-25
  • 打赏
  • 举报
回复
引用 1 楼 erhou134 的回复:
我的VM里 pthread_mutex_t 定义在. bits/pthreadtypes.h
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;
确定代码实现和头文件是同一系统的么?
我的是在mac下面的。我也怀疑是不一致。。谢谢你的帮助
空的 2013-12-22
  • 打赏
  • 举报
回复
我的VM里 pthread_mutex_t 定义在. bits/pthreadtypes.h
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;
确定代码实现和头文件是同一系统的么?

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧