您的位置:首页 > 数据库 > MySQL

mysql在windows平台下线程同步的实现方式

2011-05-04 21:22 477 查看
今天在看mysqlslap源码的时候,发现mysqlslap里面对于线程的同步使用的是pthread_mutex_t,pthread_cond_t等,这不是linux下的东西吗?怎么在windows下也能这样使用 。难道这是C的库函数 。为了确认一下,自己建了一个工程,声明变量pthread_mutex_t MyMutex,hi,该类型居然不能够被识别。包含了几个C的库进来还是不行。于是对mysqlslap中的该类型进行了到底的追踪。

最后发现该类型被定义于 MySQL的my_pthread.h头文件中。如:

typedef CRITICAL_SECTION pthread_mutex_t;

该文件明确说了,其这样的目的是为了实现向linux中的posix线程。该说明位置在my_pthread.h的实现文件中。

在my_winthread.c中的原话是:

/*****************************************************************************

** Simulation of posix threads calls for Windows

*****************************************************************************/

相似的还有

typedef DWORD pthread_t;

typedef struct thread_attr {

DWORD dwStackSize ;

DWORD dwCreatingFlag ;

} pthread_attr_t ;

typedef struct { int dummy; } pthread_condattr_t;

/* Implementation of posix conditions */

typedef struct st_pthread_link {
DWORD thread_id;
struct st_pthread_link *next;
} pthread_link;

/**
Implementation of Windows condition variables.
We use native conditions on Vista and later, and fallback to own
implementation on earlier OS version.
*/
typedef union
{
/* Native condition (used on Vista and later) */
CONDITION_VARIABLE native_cond;

/* Own implementation (used on XP) */
struct
{
uint32 waiting;
CRITICAL_SECTION lock_waiting;
enum
{
SIGNAL= 0,
BROADCAST= 1,
MAX_EVENTS= 2
} EVENTS;
HANDLE events[MAX_EVENTS];
HANDLE broadcast_block_event;
};
} pthread_cond_t;

typedef int pthread_mutexattr_t;
#define pthread_self() GetCurrentThreadId()
#define pthread_handler_t EXTERNC void * __cdecl
typedef void * (__cdecl *pthread_handler)(void *);

typedef volatile LONG my_pthread_once_t;
#define MY_PTHREAD_ONCE_INIT 0
#define MY_PTHREAD_ONCE_INPROGRESS 1
#define MY_PTHREAD_ONCE_DONE 2

/*
Struct and macros to be used in combination with the
windows implementation of pthread_cond_timedwait
*/

/*
Declare a union to make sure FILETIME is properly aligned
so it can be used directly as a 64 bit value. The value
stored is in 100ns units.
*/
union ft64 {
FILETIME ft;
__int64 i64;
};
struct timespec {
union ft64 tv;
/* The max timeout value in millisecond for pthread_cond_timedwait */
long max_timeout_msec;
};
#define set_timespec(ABSTIME,SEC) { /
GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); /
(ABSTIME).tv.i64+= (__int64)(SEC)*10000000; /
(ABSTIME).max_timeout_msec= (long)((SEC)*1000); /
}
#define set_timespec_nsec(ABSTIME,NSEC) { /
GetSystemTimeAsFileTime(&((ABSTIME).tv.ft)); /
(ABSTIME).tv.i64+= (__int64)(NSEC)/100; /
(ABSTIME).max_timeout_msec= (long)((NSEC)/1000000); /
}

上面这些都是对实现线程同步时定义的一些结构和宏。MySQL为了在使用上和linux一样。于是后面定义处理函数的名称的时候,也完全按linux下相应的功能来进行命名的。真是够费心的了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: