From 6a0ddb6ad62d69b3305c79d02471b8eb188b0dcc Mon Sep 17 00:00:00 2001 From: Not Zed Date: Mon, 14 Mar 2005 07:06:19 +0000 Subject: [PATCH] use a single global ref-counted db-env for all opened db's. 2005-03-09 Not Zed * backends/file/e-book-backend-file.c (e_book_backend_file_load_source): use a single global ref-counted db-env for all opened db's. (e_book_backend_file_dispose): clean up the env when finished. --- addressbook/ChangeLog | 7 ++++ addressbook/backends/file/e-book-backend-file.c | 47 ++++++++++++++++++++----- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/addressbook/ChangeLog b/addressbook/ChangeLog index 7263fd4..36ca3a8 100644 --- a/addressbook/ChangeLog +++ b/addressbook/ChangeLog @@ -1,3 +1,10 @@ +2005-03-09 Not Zed + + * backends/file/e-book-backend-file.c + (e_book_backend_file_load_source): use a single global ref-counted + db-env for all opened db's. + (e_book_backend_file_dispose): clean up the env when finished. + 2005-03-07 Sivaiah Nallagatla * backends/groupwise/e-bbok-backend-groupwise.c diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c index 205acb7..c834e19 100644 --- a/addressbook/backends/file/e-book-backend-file.c +++ b/addressbook/backends/file/e-book-backend-file.c @@ -71,6 +71,12 @@ struct _EBookBackendFilePrivate { EBookBackendSummary *summary; }; +static GStaticMutex global_env_lock = G_STATIC_MUTEX_INIT; +static struct { + int ref_count; + DB_ENV *env; +} global_env; + static void string_to_dbt(const char *str, DBT *dbt) { @@ -1037,14 +1043,34 @@ e_book_backend_file_load_source (EBookBackend *backend, return GNOME_Evolution_Addressbook_OtherError; } - db_error = db_env_create (&env, 0); - if (db_error != 0) { - g_warning ("db_env_create failed with %d", db_error); - return GNOME_Evolution_Addressbook_OtherError; + g_static_mutex_lock(&global_env_lock); + if (global_env.ref_count > 0) { + env = global_env.env; + global_env.ref_count++; + } else { + db_error = db_env_create (&env, 0); + if (db_error != 0) { + g_warning ("db_env_create failed with %d", db_error); + g_static_mutex_unlock(&global_env_lock); + return GNOME_Evolution_Addressbook_OtherError; + } + + db_error = env->open (env, NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_THREAD, 0); + if (db_error != 0) { + env->close(env, 0); + g_warning ("db_env_open failed with %d", db_error); + g_static_mutex_unlock(&global_env_lock); + return GNOME_Evolution_Addressbook_OtherError; + } + + env->set_errcall (env, file_errcall); + + global_env.env = env; + global_env.ref_count = 1; } - env->open (env, NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_THREAD, 0); + g_static_mutex_unlock(&global_env_lock); - env->set_errcall (env, file_errcall); + bf->priv->env = env; db_error = db_create (&db, env, 0); if (db_error != 0) { @@ -1271,8 +1297,13 @@ e_book_backend_file_dispose (GObject *object) if (bf->priv->file_db) bf->priv->file_db->close (bf->priv->file_db, 0); - if (bf->priv->env) - bf->priv->env->close (bf->priv->env, 0); + g_static_mutex_lock(&global_env_lock); + global_env.ref_count--; + if (global_env.ref_count == 0) { + global_env.env->close(global_env.env, 0); + global_env.env = NULL; + } + g_static_mutex_unlock(&global_env_lock); if (bf->priv->summary) g_object_unref(bf->priv->summary); -- 2.7.4