显示标签为“MySQL”的博文。显示所有博文
显示标签为“MySQL”的博文。显示所有博文

2009-04-26

MySQL Query cache笔记


有点麻烦,有点难懂.
尽管如此,还是记下目前所理解的大致情况.

首先是两个数据结构.


// sql/sql_cache.h
struct Query_cache_memory_bin
{
Query_cache_memory_bin() {} /* Remove gcc warning */
#ifndef DBUG_OFF
ulong size; //基本的size大小,实际上,free_blocks存放的接近size大小的block.大于前一个bin而小于后一个bin
#endif
uint number; // free_blocks 里的数量
Query_cache_block *free_blocks; //存放相似size大小的block

inline void init(ulong size_arg)
{
#ifndef DBUG_OFF
size = size_arg;
#endif
number = 0;
free_blocks = 0;
}
};

struct Query_cache_memory_bin_step
{
Query_cache_memory_bin_step() {} /* Remove gcc warning */
ulong size; //bin的大小
ulong increment; //bin之间的便宜量
uint idx; //bin群中,第一个关联此step的下标
inline void init(ulong size_arg, uint idx_arg, ulong increment_arg)
{
size = size_arg;
idx = idx_arg;
increment = increment_arg;
}
};



query cache的大概结构式这样的.
+----------------------------------------------------+
| step array 当作cache的一级索引 |
| 存放的主要信息是某一个size大小的bin群起始的位置偏移 |
+----------------------------------------------------+
| bin array 当作cache的二级索引 |
| 存放的主要是size大小的bin群组 |
+----------------------------------------------------+
| 实际的cache 区域 |
+----------------------------------------------------+

对于cache的step和bin的一些换算关系如下
current_bin_count = ( pre_bin_count + QUERY_CACHE_MEM_BIN_PARTS_INC ) * QUERY_CACHE_MEM_BIN_PARTS_MUL;

简单地说,在一级索引step里,下级cache单位块的数量按上级数量的基础,加上一定的增量,以某种倍数增长.

至于大小方面,有
current_bin_size = pre_bin_size >> QUERY_CACHE_MEM_BIN_STEP_PWR2
也就是上级cache的单位块按照一定数量递减.

查看源代码计算一下,可以知道,
current_bin_count * current_bin_size <= pre_bin_size.
意思是,在进行进一步分块的时候,尽管块的数量增加了,但是总量还是小于等于上一级的块的大小.

接着,要提提实际cache的分布了.

源代码里有这么一个图.
sql/sql_cache.cc
For example:
query_cache_size>>QUERY_CACHE_MEM_BIN_FIRST_STEP_PWR2 = 100,
min_allocation_unit = 17,
QUERY_CACHE_MEM_BIN_STEP_PWR2 = 1,
QUERY_CACHE_MEM_BIN_PARTS_INC = 1,
QUERY_CACHE_MEM_BIN_PARTS_MUL = 1
(in followed picture showed right (low) bound of bin):
| 100>>1 50>>1 |25>>1|
| | | | | |
| 100 75 50 41 33 25 21 18 15| 12 | - bins right (low) bounds
|\---/\-----/\--------/\--------|---/ |
| 0 1 2 3 | | - steps
\-----------------------------/ \---/

这图一开始也没明白.
后来似乎懂了.
假设cache块的大小事100.
按照公式计算,每次分块,大小减去一半,数量按照公式计算取整.
50>>1是一次分块,得到块的大小是25.根据公式计算分为( 2 + 1) * 1 = 3块.
于是从25开始,以8为增量划分块.

增量的计算也是有公式的.
inc = (prev_size - mem_bin_size) / mem_bin_count
以50>>1这个例子来说.
pre_size = 50 , men_bin_size = 25 , men_bin_count = 3
所以增量是8.

如何解释这个增量公式呢.
首先,每次mem_bin_size是折半递减的.
那么,从内存分布上来说,prev_mem_bin_size的大小恰好是当前bin群起始的偏移量.
只要明白这就那个增量公式就好理解了.
先得到bin群的偏移量起点,然后计算当前bin大小的偏移量,就能正确分布了.

但,既然在bin的结构里已经有了size这个成员用来表示bin的大小了,那何必用inc来计算偏移呢.

挣扎了几天给自己的解释是,这是它查找bin的算法决定的.

注意到代码里的注释部分,有这么句话:
When a new block is allocated we find most suitable memory block
(minimal of >= required size). If such a block can not be found, we try
to find max block < required size

也就是在寻找合适的bin的时候,他会后弦尝试寻找合适大小的bin块,当找不到的时候,会在不满足大小的最大的bin群里寻找.
那么,既然大小不满足,为什么要向小的群里找而不是大的群里找呢.

看它寻找bin的算法.


// sql/sql_cache.cc
uint Query_cache::find_bin(ulong size)
{
DBUG_ENTER("Query_cache::find_bin");
// Binary search
int left = 0, right = mem_bin_steps;
do
{
int middle = (left + right) / 2;
if (steps[middle].size > size)
left = middle+1;
else
right = middle;
} while (left < right); //采用折半查找的方式,在step数组里寻找合适的位置.按照降序排列
if (left == 0)
{
// first bin not subordinate of common rules
DBUG_PRINT("qcache", ("first bin (# 0), size %lu",size));
DBUG_RETURN(0);
}//left是不满足size的最大块组,即,应该是满足点的右方
uint bin = steps[left].idx -
(uint)((size - steps[left].size)/steps[left].increment);
//idx可能是step结束的bin的下标.因此后半部计算的是size多出bin_size部分占的大小.也就是说,其实是从小于的step部分取出来的.
DBUG_PRINT("qcache", ("bin %u step %u, size %lu step size %lu",
bin, left, size, steps[left].size));
DBUG_RETURN(bin);
}


过程是寻找不满足大小的第一个bin群(bin群按照降序排列).
此时,size必然是大于left的最小大小,但是小于right的最小大小.

因此,只能在left的群组里以增量方式寻找合适的大小.

这样,这个有些古怪的cache结构及大致记录完毕了.

总结一下,其实或许应该称这个cache结构拥有二层索引.
第一层是一个step范围索引,索引了某个区间范围的bin组.
第二层是一个bin,增量范围索引,索引了bin组内,各个增量的cache快.
+-----+
| | <---step数组 +----------------------+
+-----+ | size = bin | <--- bin 数组
|size | =============> +----------------------+
+-----+ | size = bin + incre |
| | +----------------------+
| | | .... +
| | | |
+-----+ +----------------------+

bin里各自维护了各自大小的block链表.

2009-04-14

load_defualt


感觉条件,或从默认配置文件里读取参数配置,或从指定配置文件里读取参数.

如果指定了配置文件,则从配置文件里读取参数.
如果指定了使用默认配置文件配置,则从默认配置文件读取参数.
如果以上都没有指定,则从默认的配置目录读取配置文件.

参数读取的方式都是递归的.
也就是说,如果配置文件包含类似 !include 之类的语句的话,会递归调用search_default_file 这个函数(位于 mysys/default.c line 574 )


// mysys/default.c
/*
Read options from configurations files

SYNOPSIS
load_defaults()
conf_file Basename for configuration file to search for.
If this is a path, then only this file is read.
groups Which [group] entrys to read.
Points to an null terminated array of pointers
argc Pointer to argc of original program
argv Pointer to argv of original program

IMPLEMENTATION

Read options from configuration files and put them BEFORE the arguments
that are already in argc and argv. This way the calling program can
easily command line options override options in configuration files

NOTES
In case of fatal error, the function will print a warning and do
exit(1)

To free used memory one should call free_defaults() with the argument
that was put in *argv

RETURN
0 ok
1 The given conf_file didn't exists
*/


int load_defaults(const char *conf_file, const char **groups,
int *argc, char ***argv)
{
DYNAMIC_ARRAY args;
TYPELIB group;
my_bool found_print_defaults= 0;
uint args_used= 0;
int error= 0;
MEM_ROOT alloc;
char *ptr,**res;
struct handle_option_ctx ctx;
DBUG_ENTER("load_defaults");

init_default_directories();
init_alloc_root(&alloc,512,0);
/*
Check if the user doesn't want any default option processing
--no-defaults is always the first option
*/
if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults"))
{
/* remove the --no-defaults argument and return only the other arguments */
uint i;
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(*argc + 1)*sizeof(char*)))) //申请内存,并将其添加到alloc的free链表里.返回实际可用区间的指针.
goto err;
res= (char**) (ptr+sizeof(alloc)); //上面char* 指针存放的开始位置.位MEM_ROOT结构预留位置
res[0]= **argv; /* Copy program name */
for (i=2 ; i < (uint) *argc ; i++) //存放参数,忽略已有的 --no-defaults 参数
res[i-1]=argv[0][i];
res[i-1]=0; /* End pointer */
(*argc)--; //忽略 --no-defaults 参数
*argv=res; //重置参数列表
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
DBUG_RETURN(0);
} //以上动作是把main的命令行参数存到自己的内存区域里.MEM_ROOT链表.

group.count=0;
group.name= "defaults";
group.type_names= groups; //groups = { "mysql","client",0 }.

for (; *groups ; groups++)
group.count++;
//至此.group结构初始化完毕.
if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32)) //将args指向指定大小的内存区域.
goto err;

ctx.alloc= &alloc;
ctx.args= &args;
ctx.group= &group;
//conf_file = "my".递归将 'my' 的配置项目写入ctx指定的上下文环境中
error= my_search_option_files(conf_file, argc, argv, &args_used,
handle_default_option, (void *) &ctx); //conf_file = "my" 以上动作,handle_default_option 将option插入到指定的上下文中
/*
Here error contains <> 0 only if we have a fully specified conf_file
or a forced default file
*/
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(args.elements + *argc +1) *sizeof(char*)))) // 在alloc指定的上下文环境中,为参数开辟内存区域
goto err;
res= (char**) (ptr+sizeof(alloc)); //偏移内存链表头数据结构位置

/* copy name + found arguments + command line arguments to new array */
res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */
memcpy((uchar*) (res+1), args.buffer, args.elements*sizeof(char*)); //将命令行参数复制到上下文环境
/* Skip --defaults-xxx options */
(*argc)-= args_used;
(*argv)+= args_used;

/*
Check if we wan't to see the new argument list
This options must always be the last of the default options
*/
if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
{
found_print_defaults=1;
--*argc; ++*argv; /* skip argument */
}
// 经过场面的参数过滤,重新复制参数到上下文环境
if (*argc)
memcpy((uchar*) (res+1+args.elements), (char*) ((*argv)+1),
(*argc-1)*sizeof(char*));
res[args.elements+ *argc]=0; /* last null */

(*argc)+=args.elements; // 更新参数个数,加上从配置文件读取的参数个数
*argv= (char**) res; // 将agrv重形象到上下文环境的参数区域.即现在存放原有参数和配置文件参数的那块区域
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
delete_dynamic(&args); //释放已经不需要的aggrs.其内容已经放置到上下文环境中了.
if (found_print_defaults)
{
int i;
printf("%s would have been started with the following arguments:\n",
**argv);
for (i=1 ; i < *argc ; i++)
printf("%s ", (*argv)[i]);
puts("");
exit(0);
}
DBUG_RETURN(error);

err:
fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
exit(1);
return 0; /* Keep compiler happy */
}

alloc_root


此函数经由某些宏开关,被分成了两个部分.
而实际,主要是从170行开始.之前的是另一个部分.
这个函数主要做的是,从free链表里面获取lengt大小的内存块.
如果没有合适大小的块,则用my_malloc申请新的块.

得到块之后,并没有讲块从free里移除.依然在free链表里.
内存块的大小也是不固定的.

alloc_root的代码

// mysys/my_alloc.c
void *alloc_root(MEM_ROOT *mem_root, size_t length)
{
#if defined(HAVE_purify) && defined(EXTRA_DEBUG)
reg1 USED_MEM *next;
DBUG_ENTER("alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));

DBUG_ASSERT(alloc_root_inited(mem_root));

length+=ALIGN_SIZE(sizeof(USED_MEM));
if (!(next = (USED_MEM*) my_malloc(length,MYF(MY_WME))))
{
if (mem_root->error_handler)
(*mem_root->error_handler)();
DBUG_RETURN((uchar*) 0); /* purecov: inspected */
}
next->next= mem_root->used;
next->size= length;
mem_root->used= next; // 将next链接到root链表
DBUG_PRINT("exit",("ptr: 0x%lx", (long) (((char*) next)+
ALIGN_SIZE(sizeof(USED_MEM)))));
DBUG_RETURN((uchar*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
#else
size_t get_size, block_size;
uchar* point;
reg1 USED_MEM *next= 0;
reg2 USED_MEM **prev;
DBUG_ENTER("alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
DBUG_ASSERT(alloc_root_inited(mem_root));

length= ALIGN_SIZE(length);
if ((*(prev= &mem_root->free)) != NULL) //有可用内存区域,此时prev与free链表指向相同.
{
if ((*prev)->left < length &&
mem_root->first_block_usage++ >= ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP &&
(*prev)->left < ALLOC_MAX_BLOCK_TO_DROP)
{
next= *prev;
*prev= next->next; /* Remove block from list */
next->next= mem_root->used;
mem_root->used= next;
mem_root->first_block_usage= 0;
}
for (next= *prev ; next && next->left < length ; next= next->next)
prev= &next->next;
}
if (! next) //上面的例程没找到合适大小的内存块,则申请新的内存区域
{ /* Time to alloc new block */
block_size= mem_root->block_size * (mem_root->block_num >> 2);
get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
get_size= max(get_size, block_size);

if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME))))
{
if (mem_root->error_handler)
(*mem_root->error_handler)();
return((void*) 0); /* purecov: inspected */
}
mem_root->block_num++;
next->next= *prev; //将新块添加到free链表头部
next->size= get_size;
next->left= get_size-ALIGN_SIZE(sizeof(USED_MEM));
*prev=next; //重新让pre指向free链表头部
}
//获得内存块之后,减去链表数据结构的大小,得到实际可用内存块大小的起点.
point= (uchar*) ((char*) next+ (next->size-next->left));
/*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
if ((next->left-= length) < mem_root->min_malloc)
{ /* Full block */
*prev= next->next; /* Remove block from list */
next->next= mem_root->used;
mem_root->used= next;
mem_root->first_block_usage= 0;
}
DBUG_PRINT("exit",("ptr: 0x%lx", (ulong) point));
DBUG_RETURN((void*) point);// 返回实际可用内存块的起始位置指针.此时内存块已经列入free链表里
#endif
}

init_alloc_root


代码如下
主要是预处理一下相关数据结构的内容.

init_alloc_root的代码

// my_sys/my_alloc.c
/*
Initialize memory root

SYNOPSIS
init_alloc_root()
mem_root - memory root to initialize
block_size - size of chunks (blocks) used for memory allocation
(It is external size of chunk i.e. it should include
memory required for internal structures, thus it
should be no less than ALLOC_ROOT_MIN_BLOCK_SIZE)
pre_alloc_size - if non-0, then size of block that should be
pre-allocated during memory root initialization.

DESCRIPTION
This function prepares memory root for further use, sets initial size of
chunk for memory allocation and pre-allocates first block if specified.
Altough error can happen during execution of this function if
pre_alloc_size is non-0 it won't be reported. Instead it will be
reported as error in first alloc_root() on this memory root.
*/

void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
size_t pre_alloc_size __attribute__((unused)))
{
DBUG_ENTER("init_alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
//为MEM_ROOT结构各部分置初始值.保持各链表为空.
mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
mem_root->min_malloc= 32;
mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
mem_root->error_handler= 0;
mem_root->block_num= 4; /* We shift this with >>2 */
mem_root->first_block_usage= 0;

#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
if (pre_alloc_size)
{
if ((mem_root->free= mem_root->pre_alloc=
(USED_MEM*) my_malloc(pre_alloc_size+ ALIGN_SIZE(sizeof(USED_MEM)),
MYF(0))))
{
mem_root->free->size= pre_alloc_size+ALIGN_SIZE(sizeof(USED_MEM));
mem_root->free->left= pre_alloc_size;
mem_root->free->next= 0;
}
}
#endif
DBUG_VOID_RETURN;
}






相关的数据结构如下.
USED_MEM代表的是一个内存块.
而MEM_ROOT是一个内存链表.意义参见实现.
这两个数据结构会被反复用到.
参见相应的标签

USED_MEM和MEM_ROOT的代码

// include/my_alloc.h
typedef struct st_used_mem
{ /* struct for once_alloc (block) */
struct st_used_mem *next; /* Next block in use */
unsigned int left; /* memory left in block */
unsigned int size; /* size of block */
} USED_MEM;


typedef struct st_mem_root
{
USED_MEM *free; /* blocks with free memory in it */
USED_MEM *used; /* blocks almost without free memory */
USED_MEM *pre_alloc; /* preallocated block */
/* if block have less memory it will be put in 'used' list */
size_t min_malloc;
size_t block_size; /* initial block size */
unsigned int block_num; /* allocated blocks counter */
/*
first free block in queue test counter (if it exceed
MAX_BLOCK_USAGE_BEFORE_DROP block will be dropped in 'used' list)
*/
unsigned int first_block_usage;

void (*error_handler)(void);
} MEM_ROOT;

2009-04-13

line_391--2009-4-14 16:38:09


由 391 至 404 , 通过duplicate 一个stdout的descriptor来判读stdout是否有效.
代码如下:

main的代码

//clien/mysql.cc
int main(int argc,char *argv[])
{
char buff[80];

MY_INIT(argv[0]); // 初始话化THR_KEY_mysys指向的线程环境,并初始化一些互斥量和锁.
DBUG_ENTER("main");
DBUG_PROCESS(argv[0]);
#pragma region SET_TERMINAL //设置交互终端的状态.
delimiter_str= delimiter;
default_prompt = my_strdup(getenv("MYSQL_PS1") ?
getenv("MYSQL_PS1") :
"mysql> ",MYF(MY_WME));
current_prompt = my_strdup(default_prompt,MYF(MY_WME));
prompt_counter=0;

outfile[0]=0; // no (default) outfile
strmov(pager, "stdout"); // the default, if --pager wasn't given
{
char *tmp=getenv("PAGER");
if (tmp && strlen(tmp))
{
default_pager_set= 1;
strmov(default_pager, tmp);
}
}
if (!isatty(0) || !isatty(1))
{
status.batch=1; opt_silent=1;
ignore_errors=0;
}
else
status.add_to_history=1;
status.exit_status=1;
#pragma endregion SET_TERMINAL
{ #pragma region PROMOTE_STDOUT //保证stdout有效,且可以访问.
/*
The file descriptor-layer may be out-of-sync with the file-number layer,
so we make sure that "stdout" is really open. If its file is closed then
explicitly close the FD layer.
*/
int stdout_fileno_copy;
stdout_fileno_copy= dup(fileno(stdout)); /* Okay if fileno fails. */
if (stdout_fileno_copy == -1)
fclose(stdout);
else
close(stdout_fileno_copy); /* Clean up dup(). */
}
#pragma endregion PROMOTE_STDOUT
...
}

line_364--2009-4-14 16:37:25

  main
  由 364 至 389 , 设置交互终端的一些信息,如提示符类型和这段设备状态等.
代码如下:
main的代码
// clien/mysql.cc
int main(int argc,char *argv[])
{
    char buff[80];

    MY_INIT(argv[0]); // 初始话化THR_KEY_mysys指向的线程环境,并初始化一些互斥量和锁.
    DBUG_ENTER("main");
    DBUG_PROCESS(argv[0]);
#pragma  region SET_TERMINAL //设置交互终端的状态.
    delimiter_str= delimiter; 
    default_prompt = my_strdup(getenv("MYSQL_PS1") ? 
    getenv("MYSQL_PS1") : 
    "mysql> ",MYF(MY_WME)); 
    current_prompt = my_strdup(default_prompt,MYF(MY_WME)); 
    prompt_counter=0;

    outfile[0]=0;   // no (default) outfile
    strmov(pager, "stdout"); // the default, if --pager wasn't given
    {
        char *tmp=getenv("PAGER");
        if (tmp && strlen(tmp))
        {
            default_pager_set= 1;
            strmov(default_pager, tmp);
        }
    }
    if (!isatty(0) || !isatty(1))
    {
        status.batch=1; opt_silent=1;
        ignore_errors=0;
    }
    else
    status.add_to_history=1;
    status.exit_status=1;
#pragma endregion SET_TERMINAL
    ...
}

MY_INIT


main里的入口
代码如下:

main的代码

// clien/mysql.cc
int main(int argc,char *argv[])
{
char buff[80];

MY_INIT(argv[0]);

...
}






include/my_sys.h line 40
MY_INIT
设置程序名字,并调用my_init()进行另外一些初始化
代码如下:

INIT的代码

#define MY_INIT(name); { my_progname= name; my_init(); }






my_init
代码见末尾.
1.检测init的状态,如果已经初始化完毕,或之前已经进行过初始化了,那么直接跳出.
2.设置初始化标记,表示初始化例程被调用过,即 1 中的 my_init_done标记.
3.递增初始化计数器.
4.设置默认的文件以及文件夹访问权限.
root用户以及所有者具有文件的读写权限,文件夹的所有权限
owner只具有文件的读写权限.
其余人没有任何权限
5.依据线程方面的一些开关
1)#if defined(THREAD) && defined(SAFE_MUTEX) /mysys/my_init.c line 80
进行名为safe_mutex_global_init 的操作
safe_mutex_global_init /mysys/thr_mutex.c line 50
|
|-- pthread_mutex_init /include/my_pthread line 236
|
|-- my_pthread_mutex_init /mysys/my_pthread.c 432
根据初始化互斥标记,进行互斥量的初始化
底层依靠的是 pthread 库的 pthread_mutex_init 函数
这段动作将初始化一个名为 THR_LOCK_mutex 的线程锁.
2)#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined (SAFE_MUTEX) /mysys/my_init.c line 83
进行名为fastmutex_global_init 的操作
设置了一个名为 cpu_count int类型静态量.
应该是CPU数量的计数器.
无其他任何动作.
根据线程方面的开关.在两个线程互斥模型中最多选择一个使用于系统.
注意到两个互斥的开关条件 defined(SAFE_MUTEX)
6.调用pthread的pthread_init初始化线程环境.
7.调用my_thread_global_init,并对此初始化例程的结果进行跟踪检测
函数动作包括
my_thread_global_init /mysys/my_thr_init.c line 81
1)调用get_thread_lib获得当前环境所使用的线程库类型.
线程库ID位于文件 /include/my_pthread.h line 691
2)利用pthread_keycreate建立一个新的线程上下文环境,并以THR_KEY_mysys作为此环境的索引
THR_KEY_mysys 类型未明. /mysys/my_thr_init.c line 86
3)判断是否在目标为linux环境
如果是,则在此版本的代码编写期间,pthread_exit函数存在race condition的bug.
使用一些代码避免这个bug
4)根据一些条件,做一些线程方面的设置工作.
主要牵涉到 pthread_mutexattr_init 和 pthread_mutexattr_settype 两个函数
5)初始化一些线程锁
名字列表:
THR_LOCK_malloc
THR_LOCK_open
THR_LOCK_lock
THR_LOCK_isam
THR_LOCK_myisam
THR_LOCK_heap
THR_LOCK_net
THR_LOCK_charset
THR_LOCK_threads
THR_LOCK_time
THR_COND_threads
注意到这些锁的行为由之前设定的一些attr决定.
6)调用pthread_cond_init函数
底层是pthread_cond_init,初始化名为THR_COND_threads的条件量
7)对于win平台,初始化一个THR_LOCK_thread
8)根据一些条件,有选择的初始化LOCK_localtime_r , LOCK_gethostbyname_r
9)调用my_thread_init,并检测状态.
获得之前建立的线程上下文环境,用THR_KEY_mysys索引,尝试得到一个上下文环境的指针.
然后将此数据结构放入用THR_KEY_mysys索引的上下文环境中.
数据结构为一个线程描述结构.
struct st_my_thread_var /include/my_pthread.h 662
10)如果没有异常,则进行扫尾工作.调用my_thread_global_end.
内部例程里利用线程锁,逐步等待线程结束.
清理之前申请的线程上下文环境 THR_KEY_mysys;
释放之前申请的所有的锁.参见 5)
8.对win环境读写一些注册表,并初始化winsock环境

代码如下:

my_init的代码

// mysys/my_init.c
my_bool my_init(void)
{
char * str;
if (my_init_done)
return 0;
my_init_done=1;
mysys_usage_id++;
my_umask= 0660; /* Default umask for new files */
my_umask_dir= 0700; /* Default umask for new directories */
#if defined(THREAD) && defined(SAFE_MUTEX)
safe_mutex_global_init(); /* Must be called early */
#endif
#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
fastmutex_global_init(); /* Must be called early */
#endif
netware_init();
#ifdef THREAD
#if defined(HAVE_PTHREAD_INIT)
pthread_init(); /* Must be called before DBUG_ENTER */
#endif
if (my_thread_global_init())
return 1;
#if !defined( __WIN__) && !defined(__NETWARE__)
sigfillset(&my_signals); /* signals blocked by mf_brkhant */
#endif
#endif /* THREAD */
{
DBUG_ENTER("my_init");
DBUG_PROCESS((char*) (my_progname ? my_progname : "unknown"));
if (!home_dir)
{ /* Don't initialize twice */
my_win_init();
if ((home_dir=getenv("HOME")) != 0)
home_dir=intern_filename(home_dir_buff,home_dir);
#ifndef VMS
/* Default creation of new files */
if ((str=getenv("UMASK")) != 0)
my_umask=(int) (atoi_octal(str) 0600);
/* Default creation of new dir's */
if ((str=getenv("UMASK_DIR")) != 0)
my_umask_dir=(int) (atoi_octal(str) 0700);
#endif
#ifdef VMS
init_ctype(); /* Stupid linker don't link _ctype.c */
#endif
DBUG_PRINT("exit",("home: '%s'",home_dir));
}
#ifdef __WIN__
win32_init_tcp_ip();
#endif
DBUG_RETURN(0);
}
} /* my_init */


小结:
MY_INIT 宏所做的工作主要是:
1.初始化THR_KEY_mysys索引的线程环境.
2.初始化一些锁变量和线程条件变量.

投其所好

最近看了持械入宋,发觉AI的发展或者说经济化还是非常一日千里的. 虽然说短剧本身也不是什么新事物了. 甚至AI制作也不是最近才有的. 只是从质量上来说,还是看得出来是有区分度,或者说ROI门槛了的. 这片虽然从服化道方面来说还是普通的AI剧,甚至剧情大半夜还是传统的龙王归来模式....