依据《深入理解MySQL》的内容,5.1版本号时存储引擎的插件化都还不是彻底,确切的说是刚加入的特性。为MySQL加入一个存储引擎时,须要更改一些上层代码,零散的更改本来就有点麻烦,同一时候project也要又一次编译一次。我听别人说,已经能够不改C/C++代码就直接加入引擎了。这种话,折腾存储引擎的话就更方便了!
mysql_declare_plugin(innobase)
{
  MYSQL_STORAGE_ENGINE_PLUGIN,
  &innobase_storage_engine,
  innobase_hton_name,
  plugin_author,
  "Supports transactions, row-level locking, and foreign keys",
  PLUGIN_LICENSE_GPL,
  innobase_init, /* Plugin Init */
  NULL, /* Plugin Deinit */
  INNODB_VERSION_SHORT,
  innodb_status_variables_export,/* status variables             */
  innobase_system_variables, /* system variables */
  NULL, /* reserved */
  0,    /* flags */
},
i_s_innodb_trx,
i_s_innodb_locks,
i_s_innodb_lock_waits,
i_s_innodb_cmp,
i_s_innodb_cmp_reset,
i_s_innodb_cmpmem,
i_s_innodb_cmpmem_reset
mysql_declare_plugin_end;    《深入理解MySQL》里面的demo存储引擎倒是有比較具体的解释,可是感觉和innodb这种“高大上”还是没法比的。
没办法了,仅仅能硬着头皮上代码了。我是带着问题来看代码的。写的东西肯定不是系统的。
这个动态库内部实现了多线程架构,总觉的不太踏实。假设这动态库被别的MySQL实例载入了。究竟会不会有问题?(当然这个问题感觉我短时间回答不了)。
- innobase_init是一个函数,它被赋值给谁了呢?看plugin.h中的代码片段。
struct st_mysql_plugin
{
  int type;             /* the plugin type (a MYSQL_XXX_PLUGIN value)   */
  void *info;           /* pointer to type-specific plugin descriptor   */
  const char *name;     /* plugin name                                  */
  const char *author;   /* plugin author (for I_S.PLUGINS)              */
  const char *descr;    /* general descriptive text (for I_S.PLUGINS)   */
  int license;          /* the plugin license (PLUGIN_LICENSE_XXX)      */
  int (*init)(void *);  /* the function to invoke when plugin is loaded */
  int (*deinit)(void *);/* the function to invoke when plugin is unloaded */
  unsigned int version; /* plugin version (for I_S.PLUGINS)             */
  struct st_mysql_show_var *status_vars;
  struct st_mysql_sys_var **system_vars;
  void * __reserved1;   /* reserved for dependency checking             */
  unsigned long flags;  /* flags for plugin */
};innobase_init()函数会调用innobase_start_or_create_for_mysql()函数。
我们能够细致看看这个函数。多线程的玄机都在这个函数中。
我把新建线程的代码摘取了出来。
    for (i = 0; i < srv_n_file_io_threads; i++) {
        n[i] = i;
        os_thread_create(io_handler_thread, n + i, thread_ids + i);
    }
    /*****
    *****/
        /* Create the thread which watches the timeouts for lock waits */
    os_thread_create(&srv_lock_timeout_thread, NULL,
             thread_ids + 2 + SRV_MAX_N_IO_THREADS);
    /* Create the thread which warns of long semaphore waits */
    os_thread_create(&srv_error_monitor_thread, NULL,
             thread_ids + 3 + SRV_MAX_N_IO_THREADS);
    /* Create the thread which prints InnoDB monitor info */
    os_thread_create(&srv_monitor_thread, NULL,
             thread_ids + 4 + SRV_MAX_N_IO_THREADS);
    /*****
    *****/
    os_thread_create(&srv_master_thread, NULL, thread_ids
         + (1 + SRV_MAX_N_IO_THREADS));除了上面的几个线程,另一个可选的purge线程。
普通情况下,innodb存储引擎会有大概八个线程。这个结论和《MySQL技术内幕》的还是非常接近的,我们看的innodb版本号应该不一样。这大概就是innodb引擎多线程架构的初始化过程,进一步研究innodb的架构就应该去看各个线程的工作函数,以及它们之间的同步及通信机制了。 
       至于我那个“动态库被别的MySQL实例载入的问题”。
我认为在这个情形下,动态库的机制并非为了代码反复利用,而是基于模块化的软件设计思想。这里的引擎不应该被别的MySQL实例载入。
原文:http://www.cnblogs.com/mengfanrong/p/5189521.html