From: <svn...@op...> - 2009-02-25 15:22:31
|
Author: bellmich Date: Wed Feb 25 16:22:25 2009 New Revision: 951 URL: http://libsyncml.opensync.org/changeset/951 Log: added code to handle libxml initialization and cleanup in a thread safe way Modified: trunk/libsyncml/sml_support.c trunk/libsyncml/sml_support.h Modified: trunk/libsyncml/sml_support.c ============================================================================== --- trunk/libsyncml/sml_support.c Wed Feb 25 16:21:22 2009 (r950) +++ trunk/libsyncml/sml_support.c Wed Feb 25 16:22:25 2009 (r951) @@ -33,6 +33,8 @@ #include <string.h> #include <stdio.h> +#include <libxml/parser.h> + GPrivate* current_tabs = NULL; #define G_ERRORCHECK_MUTEXES @@ -507,4 +509,37 @@ smlSafeFree((gpointer *)address); } +static size_t __sml_libxml_ref_count = 0; +GStaticMutex __sml_libxml_mutex = G_STATIC_MUTEX_INIT; + +void smlLibxmlRef() +{ + g_static_mutex_lock(&__sml_libxml_mutex); + if (__sml_libxml_ref_count > 0) { + __sml_libxml_ref_count++; + } else if (__sml_libxml_ref_count == 0) { + __sml_libxml_ref_count++; + xmlInitParser(); + smlTrace(TRACE_INTERNAL, "%s: libxml initialized", __func__); + } + g_static_mutex_unlock(&__sml_libxml_mutex); +} + +void smlLibxmlUnref() +{ + g_static_mutex_lock(&__sml_libxml_mutex); + if (__sml_libxml_ref_count > 1) { + __sml_libxml_ref_count--; + } else if (__sml_libxml_ref_count == 1) { + __sml_libxml_ref_count--; + xmlCleanupParser(); + xmlCleanupGlobals(); + smlTrace(TRACE_INTERNAL, "%s: libxml cleaned up", __func__); + } else if (__sml_libxml_ref_count == 0) { + g_warning("libsyncml: libxml is not referenced but somebody tries to cleanup."); + smlTrace(TRACE_ERROR, "%s: reference counter is already zero.", __func__); + } + g_static_mutex_unlock(&__sml_libxml_mutex); +} + /*@}*/ Modified: trunk/libsyncml/sml_support.h ============================================================================== --- trunk/libsyncml/sml_support.h Wed Feb 25 16:21:22 2009 (r950) +++ trunk/libsyncml/sml_support.h Wed Feb 25 16:22:25 2009 (r951) @@ -66,4 +66,7 @@ void smlSafeFree(gpointer *address); void smlSafeCFree(char **address); +void smlLibxmlRef(); +void smlLibxmlUnref(); + #endif //_SML_SUPPORT_H_ |