From 9916bb3eccfc0d0cf40cdfc677d5b4b2ecbd7a45 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 27 Nov 2010 16:06:55 +0200 Subject: [PATCH] * mu-msg, mu-runtime: make g_mime_init explicit, mu-runtime can do it. --- src/mu-msg.c | 35 ++++++++++++++++------------------- src/mu-msg.h | 18 ++++++++++++++++++ src/mu-runtime.c | 17 ++++++++++++++++- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/mu-msg.c b/src/mu-msg.c index 37ddd824..7fd65b72 100644 --- a/src/mu-msg.c +++ b/src/mu-msg.c @@ -32,37 +32,36 @@ #include "mu-msg-priv.h" /* include before mu-msg.h */ #include "mu-msg.h" - -static guint _refcount = 0; - /* note, we do the gmime initialization here rather than in * mu-runtime, because this way we don't need mu-runtime for simple * cases -- such as our unit tests */ +static gboolean _gmime_initialized = FALSE; -static void -ref_gmime (void) +void +mu_msg_gmime_init (void) { - if (G_UNLIKELY(_refcount == 0)) { - srandom ((unsigned)(getpid()*time(NULL))); + g_return_if_fail (!_gmime_initialized); + #ifdef GMIME_ENABLE_RFC2047_WORKAROUNDS g_mime_init(GMIME_ENABLE_RFC2047_WORKAROUNDS); #else g_mime_init(0); #endif /* GMIME_ENABLE_RFC2047_WORKAROUNDS */ - } - ++_refcount; + + _gmime_initialized = TRUE; } -static void -unref_gmime (void) + +void +mu_msg_gmime_uninit (void) { - g_return_if_fail (_refcount > 0); + g_return_if_fail (_gmime_initialized); - --_refcount; - if (G_UNLIKELY(_refcount == 0)) - g_mime_shutdown(); + g_mime_shutdown(); + _gmime_initialized = FALSE; } + void mu_msg_destroy (MuMsg *msg) { @@ -80,7 +79,6 @@ mu_msg_destroy (MuMsg *msg) g_free (msg->_fields[i]); g_slice_free (MuMsg, msg); - unref_gmime (); } @@ -187,9 +185,8 @@ mu_msg_new (const char* filepath, const gchar* mdir, GError **err) { MuMsg *msg; - g_return_val_if_fail (filepath, NULL); - - ref_gmime (); + g_return_val_if_fail (filepath, NULL); + g_return_val_if_fail (_gmime_initialized, NULL); msg = g_slice_new0 (MuMsg); msg->_prio = MU_MSG_PRIO_NONE; diff --git a/src/mu-msg.h b/src/mu-msg.h index 849bcb28..8ff51981 100644 --- a/src/mu-msg.h +++ b/src/mu-msg.h @@ -31,6 +31,24 @@ G_BEGIN_DECLS struct _MuMsg; typedef struct _MuMsg MuMsg; + + +/** + * initialize the GMime-system; this function needs to be called + * before doing anything else with MuMsg. mu_runtime_init will call + * this function, so if you use that, you should not call this + * function. + */ +void mu_msg_gmime_init (void); + +/** + * uninitialize the GMime-system; this function needs to be called + * after you're done with MuMsg. mu_runtime_uninit will call this + * function, so if you use that, you should not call this function. + */ +void mu_msg_gmime_uninit (void); + + /** * create a new MuMsg* instance which parses a message and provides * read access to its properties; call mu_msg_destroy when done with it. diff --git a/src/mu-runtime.c b/src/mu-runtime.c index 162bf1d2..66957220 100644 --- a/src/mu-runtime.c +++ b/src/mu-runtime.c @@ -22,6 +22,11 @@ #include #include /* for setlocale() */ #include /* for fileno() */ +#include +#include +#include +#include + #include "mu-config.h" #include "mu-log.h" @@ -51,6 +56,10 @@ init_system (void) * terms) won't work */ setlocale (LC_ALL, ""); + /* init the random number generator; this is not really *that* + * random, but good enough for our humble needs... */ + srandom ((unsigned)(getpid()*time(NULL))); + /* on FreeBSD, it seems g_slice_new and friends lead to * segfaults. So we shut if off */ #ifdef __FreeBSD__ @@ -89,6 +98,8 @@ mu_runtime_init (const char* muhome_arg) _data = g_new0 (MuRuntimeData, 1); _data->_muhome = muhome; + + mu_msg_gmime_init (); return _initialized = TRUE; } @@ -127,6 +138,8 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv) } _data->_muhome = g_strdup (_data->_config->muhome); + + mu_msg_gmime_init (); return _initialized = TRUE; } @@ -153,8 +166,10 @@ mu_runtime_uninit (void) { g_return_if_fail (_initialized); - runtime_free (); + mu_msg_gmime_uninit (); + runtime_free (); + _initialized = FALSE; }