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);
}