main主函数

/home/id/lua.lua
/home/id/lua-c/lua.lua
/home/id/lua-c/metatable.lua
/home/id/lua-c/1.lua

  1. int main(int argc, char **argv) {
  2. return main_cmdline(argc, argv);
  3. }
  1. main_cmdline函数
  1. int main_cmdline(int argc, char **argv) {
  2. //设置底盘的选项
  3. opts = chassis_options_new();
  4. chassis_frontend_set_chassis_options(frontend, opts);
  5. main_entries = chassis_options_to_g_option_entries(opts);
  6.  
  7. //src\chassis-keyfile.c
  8. chassis_keyfile_to_options(frontend->keyfile, "mysql-proxy", main_entries)
  9.  
  10. //src\mysql-proxy-cli.c
  11. //得到用户输入的/usr/local/wm/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/wm/mysql-proxy/conf/source_wtf.cnf
  12. //中的defaults-file
  13. if (chassis_frontend_init_base_options(option_ctx, &argc, &argv, &(frontend->print_version), &(frontend->default_file), &gerr)) {
  14.  
  15. }
  16.  
  17. if (frontend->default_file) {
  18. if (!(frontend->keyfile = chassis_frontend_open_config_file(frontend->default_file, &gerr))) {
  19. g_log_dbproxy(g_critical, "loading config from '%s' failed: %s", frontend->default_file, gerr->message);
  20. exit;
  21. }
  22. }
  23. //得到defaults-file中的参数
  24. if (frontend->keyfile) {
  25. if (chassis_keyfile_to_options(frontend->keyfile, "mysql-proxy", main_entries)) {
  26. GOTO_EXIT(EXIT_FAILURE);
  27. }
  28. }
  29.  
  30. //加载插件
  31. if (chassis_frontend_load_plugins(srv->modules, frontend->plugin_dir, frontend->plugin_names)) {
  32.  
  33. }
  34.  
  35. //初始化插件选项
  36. chassis_frontend_init_plugins(srv->modules, option_ctx, &argc, &argv, frontend->keyfile, "mysql-proxy", srv->base_dir, &gerr)) {
  37.  
  38. }
  39.  
  40. //启动多个线程, 并设置回调函数chassis_event_handle, 使用libevent
  41. if (chassis_mainloop(srv)) {
  42. /* looks like we failed */
  43. g_log_dbproxy(g_critical, "Failure from chassis_mainloop. Shutting down");
  44. exit;
  45. }
  46. }

chassis_options_new
位置:src\chassis-options.c

  1. /**
  2. * create a command-line option
  3. */
  4. chassis_options_t *chassis_options_new() {
  5. chassis_options_t *opt;
  6.  
  7. opt = g_slice_new0(chassis_options_t);
  8.  
  9. return opt;
  10. }

chassis_frontend_set_chassis_options
位置:src\mysql-proxy-cli.c

  1. /**
  2. * setup the options of the chassis
  3. */
  4. int chassis_frontend_set_chassis_options(chassis_frontend_t *frontend, chassis_options_t *opts) {
  5. chassis_options_add(opts, "verbose-shutdown", , , G_OPTION_ARG_NONE, &(frontend->verbose_shutdown), "Always log the exit code when shutting down", NULL, NULL, NULL, );
  6. }

chassis_options_add
位置:src\chassis-options.c

  1. int chassis_options_add(chassis_options_t *opts,
  2. const char *long_name,
  3. gchar short_name,
  4. gint flags,
  5. GOptionArg arg,
  6. gpointer arg_data,
  7. const char *description,
  8. const char *arg_description,
  9. chas_opt_assign_hook assign_hook,
  10. chas_opt_show_hook show_hook,
  11. gint opt_property) {
  12. chassis_option_t *opt;
  13.  
  14. opt = chassis_option_new();
  15. if ( != chassis_option_set(opt,
  16. long_name,
  17. short_name,
  18. flags,
  19. arg,
  20. arg_data,
  21. description,
  22. arg_description,
  23. assign_hook,
  24. show_hook,
  25. opt_property) ||
  26. != chassis_options_add_option(opts, opt)) {
  27. chassis_option_free(opt);
  28. return -;
  29. } else {
  30. return ;
  31. }
  32. }

chassis_option_set
位置:src\chassis-options.c

  1. /**
  2. * add a option
  3. *
  4. * GOptionEntry
  5. */
  6. int chassis_option_set(chassis_option_t *opt,
  7. const char *long_name,
  8. gchar short_name,
  9. gint flags,
  10. GOptionArg arg,
  11. gpointer arg_data,
  12. const char *description,
  13. const char *arg_description,
  14. chas_opt_assign_hook assign_hook,
  15. chas_opt_show_hook show_hook,
  16. gint opt_property) {
  17. opt->long_name = g_strdup(long_name);
  18. opt->short_name = short_name;
  19. opt->flags = flags;
  20. opt->arg = arg;
  21. opt->arg_data = arg_data;
  22. opt->description = g_strdup(description);
  23. opt->arg_description = g_strdup(arg_description);
  24. opt->assign_opts_hook = assign_hook;
  25. opt->show_hook = show_hook;
  26. opt->opt_property = opt_property;
  27.  
  28. return ;
  29. }

chassis_options_add_option
位置:src\chassis-options.c

  1. /**
  2. * add a option
  3. *
  4. * GOptionEntry
  5. */
  6. int chassis_options_add_option(chassis_options_t *opts,
  7. chassis_option_t *opt) {
  8.  
  9. opts->options = g_list_append(opts->options, opt);
  10.  
  11. return ;
  12. }

chassis_options_to_g_option_entries
位置:src\chassis-options.c

  1. /**
  2. * convert the chassis_options into a GOptionEntry
  3. *
  4. * @see chassis_options_free_g_option_entries
  5. */
  6. GOptionEntry *chassis_options_to_g_option_entries(chassis_options_t *opts) {
  7. GOptionEntry *entries;
  8. int count;
  9. GList *node;
  10.  
  11. /* build the GOptionEntry block */
  12. for (node = opts->options, count = ; node; node = node->next) {
  13. count++;
  14. }
  15.  
  16. entries = g_new0(GOptionEntry, count + );
  17. for (node = opts->options, count = ; node; node = node->next) {
  18. chassis_option_t *opt = node->data;
  19.  
  20. entries[count].long_name = g_strdup(opt->long_name);
  21. entries[count].short_name = opt->short_name;
  22. entries[count].flags = opt->flags;
  23. entries[count].arg = opt->arg;
  24. entries[count].arg_data = opt->arg_data;
  25. entries[count].description = g_strdup(opt->description);
  26. entries[count].arg_description = g_strdup(opt->arg_description);
  27. count++;
  28. }
  29.  
  30. entries[count].long_name = NULL;
  31. entries[count].short_name = ;
  32. entries[count].flags = ;
  33. entries[count].arg = ;
  34. entries[count].arg_data = NULL;
  35. entries[count].description = NULL;
  36. entries[count].arg_description = NULL;
  37.  
  38. return entries;
  39. }

chassis_keyfile_to_options
位置:src\chassis-keyfile.c

  1. int chassis_keyfile_to_options(GKeyFile* keyfile, const gchar *ini_group_name, GOptionEntry *config_entries) {
  2. GError *gerr = NULL;
  3. int ret = ;
  4. int i, j;
  5.  
  6. /* all the options are in the group for "mysql-proxy" */
  7.  
  8. if (!keyfile) return -;
  9. if (!g_key_file_has_group(keyfile, ini_group_name)) return ;
  10.  
  11. /* set the defaults */
  12. for (i = ; config_entries[i].long_name; i++) {
  13. GOptionEntry *entry = &(config_entries[i]);
  14. gchar *arg_string;
  15. gchar **arg_string_array;
  16. gboolean arg_bool = ;
  17. gint arg_int = ;
  18. gdouble arg_double = ;
  19. gsize len = ;
  20.  
  21. switch (entry->arg) {
  22. case G_OPTION_ARG_FILENAME:
  23. case G_OPTION_ARG_STRING:
  24. /* is this option set already */
  25. if (NULL == entry->arg_data || NULL != *(gchar **)(entry->arg_data)) break;
  26.  
  27. arg_string = g_key_file_get_string(keyfile, ini_group_name, entry->long_name, &gerr);
  28. if (!gerr) {
  29. /* strip trailing spaces */
  30. *(gchar **)(entry->arg_data) = g_strchomp(arg_string);
  31. }
  32. break;
  33. case G_OPTION_ARG_FILENAME_ARRAY:
  34. case G_OPTION_ARG_STRING_ARRAY:
  35. /* is this option set already */
  36. if (NULL == entry->arg_data || NULL != *(gchar ***)(entry->arg_data)) break;
  37.  
  38. arg_string_array = g_key_file_get_string_list(keyfile, ini_group_name, entry->long_name, &len, &gerr);
  39. if (!gerr) {
  40. for (j = ; arg_string_array[j]; j++) {
  41. arg_string_array[j] = g_strstrip(arg_string_array[j]);
  42. }
  43. *(gchar ***)(entry->arg_data) = arg_string_array;
  44. }
  45. break;
  46. case G_OPTION_ARG_NONE:
  47. arg_bool = g_key_file_get_boolean(keyfile, ini_group_name, entry->long_name, &gerr);
  48. if (!gerr) {
  49. *(int *)(entry->arg_data) = arg_bool;
  50. }
  51. break;
  52. case G_OPTION_ARG_INT:
  53. arg_int = g_key_file_get_integer(keyfile, ini_group_name, entry->long_name, &gerr);
  54. if (!gerr) {
  55. *(gint *)(entry->arg_data) = arg_int;
  56. }
  57. break;
  58. #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 12
  59. case G_OPTION_ARG_DOUBLE:
  60. arg_double = g_key_file_get_double(keyfile, ini_group_name, entry->long_name, &gerr);
  61. if (!gerr) {
  62. *(gdouble *)(entry->arg_data) = arg_double;
  63. }
  64. break;
  65. #endif
  66. default:
  67. g_log_dbproxy(g_error, "(keyfile) the option %d can't be handled", entry->arg);
  68. break;
  69. }
  70.  
  71. if (gerr) {
  72. if (gerr->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
  73. g_log_dbproxy(g_critical, "%s", gerr->message);
  74. ret = -;
  75. }
  76.  
  77. g_error_free(gerr);
  78. gerr = NULL;
  79. }
  80. }
  81.  
  82. return ret;
  83. }

chassis_frontend_init_base_options
位置:src\chassis-frontend.c

  1. int chassis_frontend_init_base_options(GOptionContext *option_ctx,
  2. int *argc_p, char ***argv_p,
  3. int *print_version,
  4. char **config_file,
  5. GError **gerr) {
  6. chassis_options_t *opts;
  7. GOptionEntry *base_main_entries;
  8. int ret = ;
  9.  
  10. opts = chassis_options_new();
  11. chassis_options_set_cmdline_only_options(opts, print_version, config_file);
  12. base_main_entries = chassis_options_to_g_option_entries(opts);
  13.  
  14. g_option_context_add_main_entries(option_ctx, base_main_entries, NULL);
  15. g_option_context_set_help_enabled(option_ctx, FALSE);
  16. g_option_context_set_ignore_unknown_options(option_ctx, TRUE);
  17.  
  18. if (FALSE == g_option_context_parse(option_ctx, argc_p, argv_p, gerr)) {
  19. ret = -;
  20. }
  21.  
  22. /* do not use chassis_options_free_g_options... here, we need to hang on to the data until the end of the program! */
  23. chassis_frontend_options_free(base_main_entries);
  24.  
  25. chassis_options_free(opts);
  26.  
  27. return ret;
  28. }

chassis_options_set_cmdline_only_options
位置: src\chassis-frontend.c

  1. /**
  2. * setup the options that can only appear on the command-line
  3. */
  4. int chassis_options_set_cmdline_only_options(chassis_options_t *opts,
  5. int *print_version,
  6. char **config_file) {
  7.  
  8. chassis_options_add(opts,
  9. "version", 'V', , G_OPTION_ARG_NONE, print_version, "Show version", NULL, NULL, NULL, );
  10.  
  11. chassis_options_add(opts,
  12. "defaults-file", , , G_OPTION_ARG_STRING, config_file, "configuration file", "<file>", NULL, NULL, );
  13.  
  14. return ;
  15. }

chassis_frontend_init_plugins
位置:src\chassis-frontend.c

  1. int chassis_frontend_init_plugins(GPtrArray *plugins,
  2. GOptionContext *option_ctx,
  3. int *argc_p, char ***argv_p,
  4. GKeyFile *keyfile,
  5. const char* keyfile_section_name,
  6. const char *base_dir,
  7. GError **gerr) {
  8. guint i;
  9.  
  10. for (i = ; i < plugins->len; i++) {
  11. GOptionEntry *config_entries = NULL;
  12. chassis_plugin *p = plugins->pdata[i];
  13. chassis_options_t *plugin_opts = NULL;
  14.  
  15. if (NULL != (plugin_opts = chassis_plugin_get_options(p))) {
  16. gchar *group_desc = g_strdup_printf("%s-module", p->option_grp_name);
  17. gchar *help_msg = g_strdup_printf("Show options for the %s-module", p->option_grp_name);
  18. const gchar *group_name = p->option_grp_name;
  19.  
  20. config_entries = chassis_options_to_g_option_entries(plugin_opts);
  21.  
  22. GOptionGroup *option_grp = g_option_group_new(group_name, group_desc, help_msg, NULL, NULL);
  23. g_option_group_add_entries(option_grp, config_entries);
  24. g_option_context_add_group(option_ctx, option_grp);
  25.  
  26. g_free(help_msg);
  27. g_free(group_desc);
  28.  
  29. /* parse the new options */
  30. if (FALSE == g_option_context_parse(option_ctx, argc_p, argv_p, gerr)) {
  31. return -;
  32. }
  33.  
  34. if (keyfile) {
  35. if (chassis_keyfile_to_options(keyfile, keyfile_section_name, config_entries)) {
  36. return -;
  37. }
  38. }
  39.  
  40. /* resolve the path names for these config entries */
  41. chassis_keyfile_resolve_path(base_dir, config_entries);
  42. }
  43.  
  44. if (config_entries != NULL) {
  45. chassis_frontend_options_free(config_entries);
  46. }
  47. }
  48.  
  49. return ;
  50. }

chassis_plugin_get_options
位置:src\chassis-plugin.c

  1. chassis_options_t* chassis_plugin_get_options(chassis_plugin *p) {
  2. chassis_options_t * options;
  3.  
  4. if (!p->get_options) return NULL;
  5.  
  6. if (NULL == (options = p->get_options(p->config))) {
  7. g_log_dbproxy(g_critical, "adding config options for module '%s' failed", p->name);
  8. }
  9.  
  10. return options;
  11. }

chassis_frontend_load_plugins
位置src\chassis-frontend.c

  1. int chassis_frontend_load_plugins(GPtrArray *plugins, const gchar *plugin_dir, gchar **plugin_names) {
  2. int i;
  3.  
  4. /* load the plugins */
  5. for (i = ; plugin_names && plugin_names[i]; i++) {
  6. chassis_plugin *p;
  7.  
  8. char *plugin_filename;
  9. /* skip trying to load a plugin when the parameter was --plugins=
  10. that will never work...
  11. */
  12. if (!g_strcmp0("", plugin_names[i])) {
  13. continue;
  14. }
  15.  
  16. plugin_filename = g_strdup_printf("%s%c%s%s.%s",
  17. plugin_dir,
  18. G_DIR_SEPARATOR,
  19. G_MODULE_PREFIX,
  20. plugin_names[i],
  21. SHARED_LIBRARY_SUFFIX);
  22.  
  23. p = chassis_plugin_load(plugin_filename);
  24. g_free(plugin_filename);
  25.  
  26. if (NULL == p) {
  27. g_log_dbproxy(g_critical, "setting --plugin-dir=<dir> might help");
  28. return -;
  29. }
  30. p->option_grp_name = g_strdup(plugin_names[i]);
  31.  
  32. g_ptr_array_add(plugins, p);
  33. }
  34. return ;
  35. }

chassis_plugin_load
位置:src\chassis-plugin.c

  1. chassis_plugin* chassis_plugin_load(const gchar *name) {
  2. int (*plugin_init)(chassis_plugin *p);
  3. chassis_plugin *p = chassis_plugin_new();
  4.  
  5. p->module = g_module_open(name, G_MODULE_BIND_LOCAL);
  6.  
  7. if (!p->module) {
  8. g_log_dbproxy(g_critical, "loading module '%s' failed: %s", name, g_module_error());
  9.  
  10. chassis_plugin_free(p);
  11.  
  12. return NULL;
  13. }
  14.  
  15. /* each module has to have a plugin_init function */
  16. if (!g_module_symbol(p->module, "plugin_init", (gpointer) &plugin_init)) {
  17. g_log_dbproxy(g_critical, "module '%s' doesn't have a init-function: %s", name, g_module_error());
  18. chassis_plugin_free(p);
  19. return NULL;
  20. }
  21.  
  22. if ( != plugin_init(p)) {
  23. g_log_dbproxy(g_critical, "init-function for module '%s' failed", name);
  24. chassis_plugin_free(p);
  25. return NULL;
  26. }
  27.  
  28. if (p->magic != CHASSIS_PLUGIN_MAGIC) {
  29. g_log_dbproxy(g_critical, "plugin '%s' doesn't match the current plugin interface (plugin is %lx, chassis is %lx)", name, p->magic, CHASSIS_PLUGIN_MAGIC);
  30. chassis_plugin_free(p);
  31. return NULL;
  32. }
  33.  
  34. if (p->init) {
  35. p->config = p->init();
  36. }
  37.  
  38. /* if the plugins haven't set p->name provide our own name */
  39. if (!p->name) p->name = g_strdup(name);
  40. /* set dummy version number if the plugin doesn't provide a real one */
  41. if (!p->version) {
  42. g_log_dbproxy(g_critical, "plugin '%s' doesn't set a version number, refusing to load this plugin", name);
  43. chassis_plugin_free(p);
  44. return NULL;
  45. }
  46.  
  47. if (p->new_stats) {
  48. p->stats = p->new_stats();
  49. }
  50.  
  51. return p;
  52. }

plugin_init
位置:plugins\proxy\proxy-plugin.c

  1. G_MODULE_EXPORT int plugin_init(chassis_plugin *p) {
  2. p->magic = CHASSIS_PLUGIN_MAGIC;
  3. p->name = g_strdup("proxy");
  4. p->version = g_strdup(PACKAGE_VERSION);
  5.  
  6. p->init = network_mysqld_proxy_plugin_new;
  7. p->get_options = network_mysqld_proxy_plugin_get_options;
  8. p->apply_config = network_mysqld_proxy_plugin_apply_config;
  9. p->destroy = network_mysqld_proxy_plugin_free;
  10.  
  11. return ;
  12. }

network_mysqld_proxy_plugin_new
位置:plugins\proxy\proxy-plugin.c

  1. //全局变量
  2. chassis_plugin_config *config = NULL;
  3.  
  4. chassis_plugin_config * network_mysqld_proxy_plugin_new(void) {
  5. config = g_new0(chassis_plugin_config, );
  6.  
  7. config->fix_bug_25371 = ; /** double ERR packet on AUTH failures */
  8. config->profiling = ;
  9. config->start_proxy = ;
  10. config->pool_change_user = ; /* issue a COM_CHANGE_USER to cleanup the connection
  11. when we get back the connection from the pool */
  12. config->dt_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)dt_table_free);
  13.  
  14. config->select_where_limit_str = NULL;
  15. config->select_where_limit = SEL_OFF;
  16. config->charset = NULL;
  17.  
  18. config->opts = NULL;
  19.  
  20. config->percentile_value = ;
  21.  
  22. config->check_state_conn_timeout = ;
  23. config->check_state_interval = PROXY_CHECK_STATE_WAIT_TIMEOUT;
  24. config->check_state_retry_times = RETRY_TIMES;
  25. config->check_state_sleep_delay = SLEEP_DELAY;
  26. g_rw_lock_init(&config->config_lock);
  27.  
  28. config->sql_log_mgr = sql_log_t_new();
  29. config->percentile_controller = pt_percentile_new(6.0, 29.0, );
  30.  
  31. config->plugin_threads = g_hash_table_new_full(g_str_hash,
  32. g_str_equal,
  33. g_free,
  34. (GDestroyNotify)plugin_thread_t_free);
  35. config->table_prefix = NULL;
  36. config->table_suffix = NULL;
  37. config->tnw = tbl_name_wrap_new();
  38.  
  39. return config;
  40. }

network_mysqld_proxy_plugin_get_options
位置:plugins\proxy\proxy-plugin.c

  1. 设置插件的选项
  2. 我们更改的id-generate 就在这里
  3. 需要在
  4.  
  5. )config增加新的属性id-generate
  6. ./plugins/proxy/proxy-plugin.h
  7. struct chassis_plugin_config {
  8. ...
  9. gchar* id_generate;
  10. };
  11. )初始化
  12. ./plugins/proxy/proxy-plugin.c
  13. chassis_plugin_config * network_mysqld_proxy_plugin_new(void) {
  14. ...
  15. config->id_generate = NULL;
  16. return config;
  17. }
  1. static chassis_options_t * network_mysqld_proxy_plugin_get_options(chassis_plugin_config *oldconfig) {
  2. if (config->opts == NULL) {
  3. chassis_options_t *opts = chassis_options_new();
  4.  
  5. chassis_options_add(opts, "proxy-address", 'P', , G_OPTION_ARG_STRING, &(config->address), "listening address:port of the proxy-server (default: :4040)", "<host:port>",
  6. NULL, show_proxy_address, SHOW_OPTS_PROPERTY);
  7. chassis_options_add(opts, "id-generate", , , G_OPTION_ARG_STRING, &(config->id_generate), "Run mysql-proxy as user", NULL, NULL, NULL, );
  8.  
  9. config->opts = opts;
  10. }
  11.  
  12. return config->opts;
  13. }

chassis_mainloop
位置:src\chassis-mainloop.c

  1. int chassis_mainloop(void *_chas) {
  2. chassis *chas = _chas;
  3. guint i;
  4. struct event ev_sigterm, ev_sigint;
  5. #ifdef SIGHUP
  6. struct event ev_sighup;
  7. #endif
  8. chassis_event_thread_t *mainloop_thread;
  9.  
  10. /* redirect logging from libevent to glib */
  11. event_set_log_callback(event_log_use_glib);
  12.  
  13. /* add a event-handler for the "main" events */
  14. mainloop_thread = chassis_event_thread_new();
  15. chassis_event_threads_init_thread(mainloop_thread, chas);
  16. g_ptr_array_add(chas->threads, mainloop_thread);
  17.  
  18. chas->event_base = mainloop_thread->event_base; /* all global events go to the 1st thread */
  19.  
  20. g_assert(chas->event_base);
  21.  
  22. /* setup all plugins all plugins */
  23. for (i = ; i < chas->modules->len; i++) {
  24. chassis_plugin *p = chas->modules->pdata[i];
  25.  
  26. g_assert(p->apply_config);
  27. if ( != p->apply_config(chas, p->config)) {
  28. g_log_dbproxy(g_critical, "applying config of plugin %s failed", p->name);
  29. return -;
  30. }
  31. }
  32.  
  33. signal_set(&ev_sigterm, SIGTERM, sigterm_handler, NULL);
  34. event_base_set(chas->event_base, &ev_sigterm);
  35. signal_add(&ev_sigterm, NULL);
  36.  
  37. signal_set(&ev_sigint, SIGINT, sigint_handler, NULL);
  38. event_base_set(chas->event_base, &ev_sigint);
  39. signal_add(&ev_sigint, NULL);
  40.  
  41. #ifdef SIGHUP
  42. signal_set(&ev_sighup, SIGHUP, sighup_handler, chas);
  43. event_base_set(chas->event_base, &ev_sighup);
  44. if (signal_add(&ev_sighup, NULL)) {
  45. g_log_dbproxy(g_critical, "signal_add(SIGHUP) failed");
  46. }
  47. #endif
  48.  
  49. if (chas->event_thread_count < ) chas->event_thread_count = ;
  50.  
  51. /* create the event-threads
  52. *
  53. * - dup the async-queue-ping-fds
  54. * - setup the events notification
  55. * */
  56. for (i = ; i <= (guint)chas->event_thread_count; i++) { /* we already have 1 event-thread running, the main-thread */
  57. chassis_event_thread_t *thread = chassis_event_thread_new(i);
  58.  
  59. chassis_event_threads_init_thread(thread, chas);
  60.  
  61. g_ptr_array_add(chas->threads, thread);
  62. }
  63.  
  64. /* start the event threads */
  65. chassis_event_threads_start(chas->threads);
  66.  
  67. /**
  68. * handle signals and all basic events into the main-thread
  69. *
  70. * block until we are asked to shutdown
  71. */
  72. chassis_mainloop_thread_loop(mainloop_thread);
  73.  
  74. signal_del(&ev_sigterm);
  75. signal_del(&ev_sigint);
  76. #ifdef SIGHUP
  77. signal_del(&ev_sighup);
  78. #endif
  79. return ;
  80. }

chassis_event_thread_new
位置:src\chassis-event-thread.c

  1. /**
  2. * create the data structure for a new event-thread
  3. */
  4. chassis_event_thread_t* chassis_event_thread_new(guint index) {
  5. chassis_event_thread_t *thread = g_new0(chassis_event_thread_t, );
  6.  
  7. thread->index = index;
  8.  
  9. thread->event_queue = g_async_queue_new();
  10.  
  11. g_rw_lock_init(&thread->connection_lock);
  12. thread->connection_list = NULL;
  13.  
  14. thread->exit_phase = EVENT_THREAD_NORMAL;
  15. return thread;
  16. }

chassis_event_threads_init_thread
位置:src\chassis-event-thread.c

  1. /**
  2. * setup the notification-fd of a event-thread
  3. *
  4. * all event-threads listen on the same notification pipe
  5. *
  6. * @see chassis_event_handle()
  7. */
  8. int chassis_event_threads_init_thread(chassis_event_thread_t *thread, chassis *chas) {
  9. thread->event_base = event_base_new();
  10. thread->chas = chas;
  11.  
  12. int fds[];
  13. if (pipe(fds)) {
  14. int err;
  15. err = errno;
  16. g_log_dbproxy(g_error, "evutil_socketpair() failed: %s (%d)", g_strerror(err), err);
  17. }
  18. thread->notify_receive_fd = fds[];
  19. thread->notify_send_fd = fds[];
  20.  
  21. event_set(&(thread->notify_fd_event), thread->notify_receive_fd, EV_READ | EV_PERSIST, chassis_event_handle, thread);
  22. event_base_set(thread->event_base, &(thread->notify_fd_event));
  23. event_add(&(thread->notify_fd_event), NULL);
  24.  
  25. return ;
  26. }

chassis_event_threads_start
位置:src\chassis-event-thread.c

  1. /**
  2. * start all the event-threads
  3. *
  4. * starts all the event-threads that got added by chassis_event_threads_add()
  5. *
  6. * @see chassis_event_threads_add
  7. */
  8. void chassis_event_threads_start(GPtrArray *threads) {
  9. guint i;
  10.  
  11. g_log_dbproxy(g_message, "starting %d threads", threads->len - );
  12.  
  13. for (i = ; i < threads->len; i++) { /* the 1st is the main-thread and already set up */
  14. chassis_event_thread_t *thread = threads->pdata[i];
  15. GError *gerr = NULL;
  16.  
  17. thread->thr = g_thread_try_new("event thread", (GThreadFunc)chassis_event_thread_loop, thread, &gerr);
  18. if (gerr) {
  19. g_log_dbproxy(g_critical, "%s", gerr->message);
  20. g_error_free(gerr);
  21. gerr = NULL;
  22. }
  23. }
  24. }

chassis_mainloop_thread_loop
位置:src\chassis-event-thread.c

  1. /**
  2. * event-handler thread
  3. *
  4. */
  5. void* chassis_mainloop_thread_loop(chassis_event_thread_t *thread) {
  6. cur_thid = thread->index;
  7. gboolean is_all_work_thread_exit = FALSE;
  8. g_assert(thread->index == );
  9.  
  10. /**
  11. * check once a second if we shall shutdown the proxy
  12. */
  13. while (!chassis_is_shutdown() || !is_all_work_thread_exit) {
  14. struct timeval timeout;
  15. int r;
  16.  
  17. timeout.tv_sec = ;
  18. timeout.tv_usec = ;
  19.  
  20. g_assert(event_base_loopexit(thread->event_base, &timeout) == );
  21.  
  22. r = event_base_dispatch(thread->event_base);
  23.  
  24. if (r == -) {
  25. if (errno == EINTR) continue;
  26. g_log_dbproxy(g_critical, "leaving chassis_event_thread_loop early, errno != EINTR was: %s (%d)", g_strerror(errno), errno);
  27. break;
  28. }
  29.  
  30. if (chassis_is_shutdown()) {
  31. gint i = ;
  32. for (; i < thread->chas->threads->len; i++) {
  33. chassis_event_thread_t *event_thread = g_ptr_array_index(thread->chas->threads, i);
  34. if (g_atomic_int_get(&event_thread->exit_phase) != EVENT_THREAD_EXITED) break;
  35. }
  36. if (i == thread->chas->threads->len) {
  37. is_all_work_thread_exit = TRUE;
  38. }
  39. }
  40. }
  41.  
  42. g_log_dbproxy(g_message, "main loop thread will exit");
  43.  
  44. return NULL;
  45. }

dbproxy-main函数的更多相关文章

  1. [C#] 了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数

    了解过入口函数 Main() 吗?带你用批处理玩转 Main 函数 目录 简介 特点 方法的参数 方法的返回值 与批处理交互的一个示例 简介 我们知道,新建一个控制台应用程序的时候,IDE 会同时创建 ...

  2. 选择目录,选择文件夹的COM组件问题。在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。

    异常: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式.请确保您的 Main 函数带有 STAThreadAttribute 标记. 只有将调试器附加到该进程才会引发此异常. ...

  3. eclipse的maven项目,如何使用java run main函数

    项目使用maven管理,一般说来就使用jetty:run了.但是对于做功能测试和集成测试的用例,需要使用自定义的quickrun来运行进行测试环境的参数设定和功能隔离,google一番发现maven有 ...

  4. [汇编与C语言关系]2. main函数与启动例程

    为什么汇编程序的入口是_start,而C程序的入口是main函数呢?以下就来解释这个问题 在<x86汇编程序基础(AT&T语法)>一文中我们汇编和链接的步骤是: $ as hell ...

  5. 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)

    这小节我们要介绍Go里面的流程控制以及函数操作. 流程控制 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.Go中流程控制分三大类:条件判断,循环控制和 ...

  6. linux c 笔记-2 Hello World & main函数

    按照惯例撸一个hello_world.c #include <stdio.h> int main(int argc, char * argv[]) { printf("hello ...

  7. Spark&Hadoop:scala编写spark任务jar包,运行无法识别main函数,怎么办?

    昨晚和同事一起看一个scala写的程序,程序都写完了,且在idea上debug运行是ok的.但我们不能调试的方式部署在客户机器上,于是打包吧.打包时,我们是采用把外部引入的五个包(spark-asse ...

  8. [ASM C/C++] C语言的main 函数

    C语言有两种可能的运行环境 1. 独立(freestanding) 在独立环境中,C程序执行不需要操作系统的支持,因此只具有最小的链接库能力. 2. 宿主(hosted) 在宿主的环境中,C程序会在操 ...

  9. main函数的详解

    public : 公共的. 权限是最大,在任何情况下都可以访问. 原因: 为了保证让jvm在任何情况下都可以访问到main方法. static: 静态.静态可以让jvm调用main函数的时候更加的方便 ...

  10. Linux中Main函数的执行过程

    1. 问题:Linux如何执行main函数. 本文使用一个简单的C程序(simple.c)作为例子讲解.代码如下, int main() { return(0); } 2.  编译 -#gcc -o ...

随机推荐

  1. 150. Evaluate Reverse Polish Notation (Stack)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. c++ 迭代器模式(iterator)

    提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示.当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍 历的时候,就应该考虑用迭代器模式.同时需要对聚集有多种方式遍历时,可以 ...

  3. Linux分区挂载点介绍

    一.Linux分区挂载点介绍 Linux分区挂载点介绍,推荐容量仅供参考不是绝对,跟各系统用途以及硬盘空间配额等因素实际调整: 分区类型 介绍 备注 /boot 启动分区 一般设置100M-200M, ...

  4. Greeplum 系列(三) 基本用法

    Greeplum 系列(三) 基本用法 <PostgreSQL 教程>:https://www.yiibai.com/postgresql 一.Greeplum 登陆与创建 1.1 登陆 ...

  5. HBase预分区方法

    (what)什么是预分区? HBase表在刚刚被创建时,只有1个分区(region),当一个region过大(达到hbase.hregion.max.filesize属性中定义的阈值,默认10GB)时 ...

  6. html传参数 js工具类

    var QueryUtils = { GetQueryString: function (name) { var reg = new RegExp("(^|&)" + na ...

  7. C# GDI+绘图介绍

    最近查阅网上资料,将GDI+的基本知识汇总如下: 一.基本的知识 GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能; 在C#. ...

  8. Java对称加密算法

    对称加密算法概念 加密密钥和解密密钥相同,大部分算法加密揭秘过程互逆. 特点:算法公开.(相比非对称加密)计算量小.加密速度快.效率高. 弱点:双方都使用同样的密钥,安全性得不到保证. 常用对称加密算 ...

  9. 【小梅哥SOPC学习笔记】切换NIOS II CPU的主内存后软件中需要注意的几点设置

    切换NIOS II CPU的主内存后软件中需要注意的几点设置 有时候,我们可能面对这样一种情况: 1. 我们创建一个SOPC系统,并在QSYS中设置NIOS II的复位地址和异常地址都指向SRAM: ...

  10. Python之迭代器,生成器与装饰器

    1>迭代器原理及使用: 1>原理: 迭代器是访问集合元素的一种方式,迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束:迭代器只能往前不会后退,不过这也没什         ...