More Related Content
Similar to Mysql multi threaded_server_introduction
Similar to Mysql multi threaded_server_introduction (20)
Mysql multi threaded_server_introduction
- 4. 主线程处理流程 主线程处理流程(mysqld.cc main函数) 参数初始化 init_common_variables 线程处理机制 get_options – 一个线程处理一个连接 one_thread_per_connection_scheduler(&thread_scheduler) 网络初始化network_init 开启网络监听(tcp/ip socket) 开启本地监听(unix domain socket或named pipe) handle_connections_sockets(0),进入主线程loop 接受客户端连接请求(tcp/ip socket connect request) 开启线程处理客户端query请求(dml,ddl,.etc)
- 5. handle_connections_sockets handle_connections_sockets loop 建立网络连接( socket accept) 建立THD, 将thd和新建立的socket连接关联起来 my_net_init(&thd->net,vio_tmp)) 开启线程服务客户 create_new_thread(thd) create_new_thread调用了thread_scheduler.add_connection(thd), thread_scheduler是一个全局变量,mysqld启动的时候被初始化 (one_thread_per_connection_scheduler(&thread_scheduler)
- 9. 新建线程(pthread_create) create_thread_to_handle_connection 刚开始,thread pool是空的,调用pthread_create创建新的线程 新线程处理函数:handle_one_connection 线程初始化 setup_connection_thread_globals 登录,用户信息验证 login_connection(thd) 进入loop, 处理客户端的query do_command(…) 客户端退出后,断开网络连接,将线程放入thread pool 断开网络连接 end_connection 线程进入thread pool睡眠 thread_scheduler.end_thread(thd,1)
- 14. Passing thd 主线程创建thd, 传递给其他线程 新建线程时,通过pthread_create传递thd 从线程池唤醒一个线程时 主线程将thd放到链表. 子线程从链表中取出thd, 存到thread local storage 子线程从tls中取出thd 传递的是指向THD的指针,thread共享地址空间
- 19. Mysql multi-threaded server building blocks Tcp/Ip socket, Unix domain socket, named pipes Posix thread(pthread_create) Condition variables, mutex locks pthread_cond_wait, pthread_cond_timedwait pthread_cond_signal pthread_cond_broadcast Thread local storage pthread_setspecific pthread_getspecific