[evolution-data-server] EBookBackend: Make the "cache-dir" property thread-safe.
- From: Matthew Barnes <mbarnes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server] EBookBackend: Make the "cache-dir" property thread-safe.
- Date: Mon, 1 Apr 2013 13:20:07 +0000 (UTC)
commit 246a3c62d4bd83c322d09f72a3a535796b3b6f3f
Author: Matthew Barnes <mbarnes redhat com>
Date: Mon Apr 1 08:48:04 2013 -0400
EBookBackend: Make the "cache-dir" property thread-safe.
New function:
e_book_backend_dup_cache_dir()
addressbook/libedata-book/e-book-backend.c | 64 ++++++++++++++++----
addressbook/libedata-book/e-book-backend.h | 1 +
.../libedata-book/libedata-book-sections.txt | 1 +
3 files changed, 53 insertions(+), 13 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend.c b/addressbook/libedata-book/e-book-backend.c
index 1f64bf6..a6a4a3d 100644
--- a/addressbook/libedata-book/e-book-backend.c
+++ b/addressbook/libedata-book/e-book-backend.c
@@ -35,12 +35,12 @@ struct _EBookBackendPrivate {
ESourceRegistry *registry;
EDataBook *data_book;
- gboolean writable;
-
GMutex views_mutex;
GList *views;
+ GMutex property_lock;
gchar *cache_dir;
+ gboolean writable;
GMutex operation_lock;
GHashTable *operation_ids;
@@ -387,8 +387,8 @@ book_backend_get_property (GObject *object,
{
switch (property_id) {
case PROP_CACHE_DIR:
- g_value_set_string (
- value, e_book_backend_get_cache_dir (
+ g_value_take_string (
+ value, e_book_backend_dup_cache_dir (
E_BOOK_BACKEND (object)));
return;
@@ -442,6 +442,7 @@ book_backend_finalize (GObject *object)
priv = E_BOOK_BACKEND_GET_PRIVATE (object);
g_mutex_clear (&priv->views_mutex);
+ g_mutex_clear (&priv->property_lock);
g_free (priv->cache_dir);
@@ -577,7 +578,7 @@ e_book_backend_init (EBookBackend *backend)
backend->priv->views = NULL;
g_mutex_init (&backend->priv->views_mutex);
-
+ g_mutex_init (&backend->priv->property_lock);
g_mutex_init (&backend->priv->operation_lock);
backend->priv->operation_ids = g_hash_table_new_full (
@@ -591,9 +592,9 @@ e_book_backend_init (EBookBackend *backend)
* e_book_backend_get_cache_dir:
* @backend: an #EBookBackend
*
- * Returns the cache directory for the given backend.
+ * Returns the cache directory path used by @backend.
*
- * Returns: the cache directory for the backend
+ * Returns: the cache directory path
*
* Since: 2.32
**/
@@ -606,15 +607,46 @@ e_book_backend_get_cache_dir (EBookBackend *backend)
}
/**
+ * e_book_backend_dup_cache_dir:
+ * @backend: an #EBookBackend
+ *
+ * Thread-safe variation of e_book_backend_get_cache_dir().
+ * Use this function when accessing @backend from multiple threads.
+ *
+ * The returned string should be freed with g_free() when no longer needed.
+ *
+ * Returns: a newly-allocated copy of #EBookBackend:cache-dir
+ *
+ * Since: 3.10
+ **/
+gchar *
+e_book_backend_dup_cache_dir (EBookBackend *backend)
+{
+ const gchar *protected;
+ gchar *duplicate;
+
+ g_return_val_if_fail (E_IS_BOOK_BACKEND (backend), NULL);
+
+ g_mutex_lock (&backend->priv->property_lock);
+
+ protected = e_book_backend_get_cache_dir (backend);
+ duplicate = g_strdup (protected);
+
+ g_mutex_unlock (&backend->priv->property_lock);
+
+ return duplicate;
+}
+
+/**
* e_book_backend_set_cache_dir:
* @backend: an #EBookBackend
- * @cache_dir: a local cache directory
+ * @cache_dir: a local cache directory path
*
- * Sets the cache directory for the given backend.
+ * Sets the cache directory path for use by @backend.
*
- * Note that #EBookBackend is initialized with a usable default based on
- * the #ESource given to e_book_backend_open(). Backends should
- * not override the default without good reason.
+ * Note that #EBookBackend is initialized with a default cache directory
+ * path which should suffice for most cases. Backends should not override
+ * the default path without good reason.
*
* Since: 2.32
**/
@@ -625,12 +657,18 @@ e_book_backend_set_cache_dir (EBookBackend *backend,
g_return_if_fail (E_IS_BOOK_BACKEND (backend));
g_return_if_fail (cache_dir != NULL);
- if (g_strcmp0 (backend->priv->cache_dir, cache_dir) == 0)
+ g_mutex_lock (&backend->priv->property_lock);
+
+ if (g_strcmp0 (backend->priv->cache_dir, cache_dir) == 0) {
+ g_mutex_unlock (&backend->priv->property_lock);
return;
+ }
g_free (backend->priv->cache_dir);
backend->priv->cache_dir = g_strdup (cache_dir);
+ g_mutex_unlock (&backend->priv->property_lock);
+
g_object_notify (G_OBJECT (backend), "cache-dir");
}
diff --git a/addressbook/libedata-book/e-book-backend.h b/addressbook/libedata-book/e-book-backend.h
index d6107fc..67cad03 100644
--- a/addressbook/libedata-book/e-book-backend.h
+++ b/addressbook/libedata-book/e-book-backend.h
@@ -173,6 +173,7 @@ struct _EBookBackendClass {
GType e_book_backend_get_type (void) G_GNUC_CONST;
const gchar * e_book_backend_get_cache_dir (EBookBackend *backend);
+gchar * e_book_backend_dup_cache_dir (EBookBackend *backend);
void e_book_backend_set_cache_dir (EBookBackend *backend,
const gchar *cache_dir);
EDataBook * e_book_backend_ref_data_book (EBookBackend *backend);
diff --git a/docs/reference/addressbook/libedata-book/libedata-book-sections.txt
b/docs/reference/addressbook/libedata-book/libedata-book-sections.txt
index 153df5f..276ca45 100644
--- a/docs/reference/addressbook/libedata-book/libedata-book-sections.txt
+++ b/docs/reference/addressbook/libedata-book/libedata-book-sections.txt
@@ -7,6 +7,7 @@ BOOK_BACKEND_PROPERTY_REQUIRED_FIELDS
BOOK_BACKEND_PROPERTY_SUPPORTED_FIELDS
BOOK_BACKEND_PROPERTY_REVISION
e_book_backend_get_cache_dir
+e_book_backend_dup_cache_dir
e_book_backend_set_cache_dir
e_book_backend_ref_data_book
e_book_backend_set_data_book
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]