4,436
社区成员
发帖
与我相关
我的任务
分享
struct _opaque_pthread_mutex_t { long __sig; char __opaque[__PTHREAD_MUTEX_SIZE__]; };
#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);
}
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;
确定代码实现和头文件是同一系统的么?