Share Pastebin
Guest
Public paste!

russell

By: a guest | Sep 6th, 2010 | Syntax: None | Size: 2.07 KB | Hits: 23 | Expires: Never
Copy text to clipboard
  1. Index: main/db.c
  2. ===================================================================
  3. --- main/db.c   (revision 285158)
  4. +++ main/db.c   (working copy)
  5. @@ -50,7 +50,10 @@
  6.  
  7.  static DB *astdb;
  8.  AST_MUTEX_DEFINE_STATIC(dblock);
  9. +static ast_cond_t dbcond;
  10.  
  11. +static void db_sync(void);
  12. +
  13.  static int dbinit(void)
  14.  {
  15.         if (!astdb && !(astdb = dbopen(ast_config_AST_DB, O_CREAT | O_RDWR, AST_FILE_MODE, DB_BTREE, NULL))) {
  16. @@ -130,7 +133,7 @@
  17.                         counter++;
  18.                 }
  19.         }
  20. -       astdb->sync(astdb, 0);
  21. +       db_sync();
  22.         ast_mutex_unlock(&dblock);
  23.         return counter;
  24.  }
  25. @@ -155,7 +158,7 @@
  26.         data.data = (char *) value;
  27.         data.size = strlen(value) + 1;
  28.         res = astdb->put(astdb, &key, &data, 0);
  29. -       astdb->sync(astdb, 0);
  30. +       db_sync();
  31.         ast_mutex_unlock(&dblock);
  32.         if (res)
  33.                 ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
  34. @@ -224,7 +227,7 @@
  35.         key.size = fullkeylen + 1;
  36.        
  37.         res = astdb->del(astdb, &key, 0);
  38. -       astdb->sync(astdb, 0);
  39. +       db_sync();
  40.        
  41.         ast_mutex_unlock(&dblock);
  42.  
  43. @@ -662,8 +665,46 @@
  44.         return 0;
  45.  }
  46.  
  47. +/*!
  48. + * \internal
  49. + * \brief Signal the astdb sync thread to do its thing.
  50. + *
  51. + * \note dblock is assumed to be held when calling this function.
  52. + */
  53. +static void db_sync(void)
  54. +{
  55. +       ast_cond_signal(&dbcond);
  56. +}
  57. +
  58. +/*!
  59. + * \internal
  60. + * \brief astdb sync thread
  61. + *
  62. + * This thread is in charge of syncing astdb to disk after every change.
  63. + * By pushing it off to this thread to take care of, this I/O bound operation
  64. + * will not block other threads from performing other critical processing.
  65. + */
  66. +static void *db_sync_thread(void *data)
  67. +{
  68. +       for (;;) {
  69. +               ast_mutex_lock(&dblock);
  70. +               ast_cond_wait(&dbcond, &dblock);
  71. +               astdb->sync(astdb, 0);
  72. +               ast_mutex_unlock(&dblock);
  73. +       }
  74. +
  75. +       return NULL;
  76. +}
  77. +
  78.  int astdb_init(void)
  79.  {
  80. +       pthread_t dont_care;
  81. +
  82. +       ast_cond_init(&dbcond, NULL);
  83. +       if (ast_pthread_create_background(&dont_care, NULL, db_sync_thread, NULL)) {
  84. +               return -1;
  85. +       }
  86. +
  87.         dbinit();
  88.         ast_cli_register_multiple(cli_database, ARRAY_LEN(cli_database));
  89.         ast_manager_register("DBGet", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, manager_dbget, "Get DB Entry");