本文整理自网络,侵删。
mysqld是MySQL服务器端主进程,可以说mysqld是MySQL的真正核心,一切工作都是围绕mysqld进程进行的。所以要解剖mysql这个庞然大物,mysqld的代码是最好的突破口。
一切都是从熟悉的main()函数开始的,其实是从mysqld_main()函数开始的。这些代码都在mysqld.cc。mysqld_main()随后调用了win_main)()。win_main()函数主要是做了一些初始化的工作。
初始化工作完成之后,MySQL已经做好准备接受连接了。然后我们的主角Handle_connections_methods()函数登场了。这个函数的主要工作是新建3个子进程,他们分别接受TCP/IP、命名管道以及共享内存这三种方式的连接。一般情况下客户都是用TCP/IP(socket)来连接MySQL服务器的,这是最有弹性的通信方式。但是在嵌入式软件的应用环境中,需要采用后两种通信方式。
简化后的handle_connections_methods()函数:
代码如下:
static void handle_connections_methods()
{
mysql_mutex_lock(&LOCK_thread_count);
mysql_cond_init(key_COND_handler_count, &COND_handler_count, NULL);
handler_count=0;
handler_count++;
mysql_thread_create(key_thread_handle_con_namedpipes, &hThread, &connection_attrib, handle_connections_namedpipes, 0));
handler_count++;
mysql_thread_create(key_thread_handle_con_sockets, &hThread, &connection_attrib, handle_connections_sockets_thread, 0));
handler_count++;
mysql_thread_create(key_thread_handle_con_sharedmem, &hThread, &connection_attrib, handle_connections_shared_memory, 0))
while (handler_count > 0)
mysql_cond_wait(&COND_handler_count, &LOCK_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
}
新建了3个线程之后,handle_connectins_methods()函数进入一个长时间循环,直到3个连接线程全部退出后才退出。这里我主要看看socket的连接线程,我们的研究对象就是这个handle_connections_sockets_thread。这个线程把自己初始化之后,就直接调用了handle_connections_sockets();
书籍
static void handle_connections_methods()
{
mysql_mutex_lock(&LOCK_thread_count);
mysql_cond_init(key_COND_handler_count, &COND_handler_count, NULL);
handler_count=0;
handler_count++;
mysql_thread_create(key_thread_handle_con_namedpipes, &hThread, &connection_attrib, handle_connections_namedpipes, 0));
handler_count++;
mysql_thread_create(key_thread_handle_con_sockets, &hThread, &connection_attrib, handle_connections_sockets_thread, 0));
handler_count++;
mysql_thread_create(key_thread_handle_con_sharedmem, &hThread, &connection_attrib, handle_connections_shared_memory, 0))
while (handler_count > 0)
mysql_cond_wait(&COND_handler_count, &LOCK_thread_count);
mysql_mutex_unlock(&LOCK_thread_count);
}
新建了3个线程之后,handle_connectins_methods()函数进入一个长时间循环,直到3个连接线程全部退出后才退出。这里我主要看看socket的连接线程,我们的研究对象就是这个handle_connections_sockets_thread。这个线程把自己初始化之后,就直接调用了handle_connections_sockets();
相关阅读 >>
更多相关阅读请进入《mysql》频道 >>
数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。