[evolution-kolab/ek-wip-porting] CamelIMAPXExtdServer: use GOnce in init() for thread safety
- From: Christian Hilberg <chilberg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-kolab/ek-wip-porting] CamelIMAPXExtdServer: use GOnce in init() for thread safety
- Date: Fri, 20 Jul 2012 14:53:11 +0000 (UTC)
commit 8c2103b56dc208075422d26fa7051ffc42a2250f
Author: Christian Hilberg <hilberg kernelconcepts de>
Date: Fri Jul 20 16:49:46 2012 +0200
CamelIMAPXExtdServer: use GOnce in init() for thread safety
* registering of untagged handler functions
must be done once only, and in a thread-safe
way
src/libekolab/camel-imapx-extd-server.c | 41 ++++++++++++++++++++++---------
1 files changed, 29 insertions(+), 12 deletions(-)
---
diff --git a/src/libekolab/camel-imapx-extd-server.c b/src/libekolab/camel-imapx-extd-server.c
index cae8859..e936d79 100644
--- a/src/libekolab/camel-imapx-extd-server.c
+++ b/src/libekolab/camel-imapx-extd-server.c
@@ -50,6 +50,24 @@ static const CamelIMAPXUntaggedRespHandlerDesc desc = {
/*----------------------------------------------------------------------------*/
+static gpointer
+imapx_extd_server_register_untagged_fn (gpointer data)
+{
+ CamelIMAPXServer *is = CAMEL_IMAPX_SERVER (data);
+ const CamelIMAPXUntaggedRespHandlerDesc *prev = NULL;
+
+ prev = camel_imapx_server_register_untagged_handler (is,
+ IMAPX_IMAP_TOKEN_ANNOTATION,
+ &desc);
+ /* Make sure we do not have been registered before.
+ * Returning non-NULL here means that the handler
+ * has been registered before, which is an error.
+ * This needs to be checked for all handlers which
+ * are registered here.
+ */
+ return (gpointer)prev;
+}
+
static gboolean
camel_imapx_extd_server_untagged_annotation (CamelIMAPXServer *is,
GCancellable *cancellable,
@@ -104,24 +122,23 @@ camel_imapx_extd_server_init (CamelIMAPXServer *is,
GCancellable *cancellable,
GError **err)
{
- const CamelIMAPXUntaggedRespHandlerDesc *prev = NULL;
- static gboolean is_inited = FALSE;
+ static GOnce my_once = G_ONCE_INIT;
g_assert (CAMEL_IS_IMAPX_SERVER (is));
(void)cancellable; /* cancellable may be NULL */
g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
- /* TODO change this for a g_once() */
- if (is_inited)
- return TRUE;
-
- prev = camel_imapx_server_register_untagged_handler (is,
- IMAPX_IMAP_TOKEN_ANNOTATION,
- &desc);
- /* make sure we do not have been registered before */
- g_assert (prev == NULL);
+ g_once (&my_once,
+ imapx_extd_server_register_untagged_fn,
+ is);
- is_inited = TRUE;
+ if (my_once.retval != NULL) {
+ g_set_error (err,
+ CAMEL_IMAPX_ERROR,
+ 1, /* define and set a sensible code here */
+ _("A CamelIMAPXServer extended untagged response handler has been registered twice"));
+ return FALSE;
+ }
return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]