mysql中对每个connection建一个thread

在mysqld_main函数的最后,通过下面的code来建立来自客户端的连接
mysqld_socket_acceptor->connection_event_loop();
  void connection_event_loop() {
    Connection_handler_manager *mgr =
        Connection_handler_manager::get_instance();
    while (!connection_events_loop_aborted()) {
      Channel_info *channel_info = m_listener->listen_for_connection_event();
      if (channel_info != NULL) mgr->process_new_connection(channel_info);
    }
  }

上次我们讲过listen_for_connection_event,这里我们看看process_new_connection的实现
void Connection_handler_manager::process_new_connection(
    Channel_info *channel_info) {
  if (connection_events_loop_aborted() || !check_and_incr_conn_count()) {
    channel_info->send_error_and_close_channel(ER_CON_COUNT_ERROR, 0, true);
    delete channel_info;
    return;
  }
#这里通过add_connection 来添加connection,这里有两种选择,一种是每个connection 对应一个thread,一个是所有connection统一用一个thread,这里以每个connection 对应一个thread 为例,这也是目前默认的选择
  if (m_connection_handler->add_connection(channel_info)) {
    inc_aborted_connects();
    delete channel_info;
  }
}

add_connection的实现如下:

bool Per_thread_connection_handler::add_connection(Channel_info *channel_info) {
  int error = 0;
  my_thread_handle id;

  DBUG_ENTER("Per_thread_connection_handler::add_connection");

  // Simulate thread creation for test case before we check thread cache
  DBUG_EXECUTE_IF("fail_thread_create", error = 1; goto handle_error;);

  if (!check_idle_thread_and_enqueue_connection(channel_info))
    DBUG_RETURN(false);

  /*
    There are no idle threads avaliable to take up the new
    connection. Create a new thread to handle the connection
  */
  channel_info->set_prior_thr_create_utime();
#通过mysql_thread_create 来创建thread,这里的mysql_thread_create 就是pthread_create的包装,注意这里就新建一个
#thread来处理client的connetion,注意这里的回调函数是handle_connection
  error =
      mysql_thread_create(key_thread_one_connection, &id, &connection_attrib,
                          handle_connection, (void *)channel_info);

}


static void *handle_connection(void *arg) {
  

  for (;;) {
    THD *thd = init_new_thd(channel_info);
    thd_manager->add_thd(thd);

    if (thd_prepare_connection(thd))
      handler_manager->inc_aborted_connects();
    else {
      while (thd_connection_alive(thd)) {
#这里的核心就是调用do_command 来处理client发送过来的命令
        if (do_command(thd)) break;
      }
      end_connection(thd);
    }
  }

}

 

你可能感兴趣的:(mysql)