From 7a6dcdf385d8641afe3dea66a0822c43151b67d4 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Tue, 8 Dec 2009 10:21:33 +0000 Subject: [PATCH] Fixed non thread-safe getLocalCharset() function (thanks to Bartek Szurgot). --- src/platforms/posix/posixHandler.cpp | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/platforms/posix/posixHandler.cpp b/src/platforms/posix/posixHandler.cpp index 104bc404..2c113072 100644 --- a/src/platforms/posix/posixHandler.cpp +++ b/src/platforms/posix/posixHandler.cpp @@ -36,6 +36,11 @@ #include #include +#include + +#if VMIME_HAVE_PTHREAD +# include +#endif // VMIME_HAVE_PTHREAD /* #ifdef _POSIX_PRIORITY_SCHEDULING @@ -44,6 +49,43 @@ */ +#if VMIME_HAVE_PTHREAD + +namespace +{ + // This construction ensures mutex will be initialized in compile-time + // and will be available any time in the runtime. + pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; + + // Helper lock, to be exception safe all the time. + class PLockHelper + { + public: + + PLockHelper() + { + if (pthread_mutex_lock(&g_mutex) != 0) + assert(!"unable to lock mutex - thread safety's void"); + } + + ~PLockHelper() + { + if (pthread_mutex_unlock(&g_mutex) != 0) + assert(!"unable to unlock mutex - application's dead..."); + } + + private: + + // Object cannot be copied + PLockHelper(const PLockHelper&); + const PLockHelper& operator=(const PLockHelper&); + }; + +} // unnamed namespace + +#endif // VMIME_HAVE_PTHREAD + + namespace vmime { namespace platforms { namespace posix { @@ -109,6 +151,8 @@ const vmime::datetime posixHandler::getCurrentLocalTime() const const vmime::charset posixHandler::getLocaleCharset() const { + const PLockHelper lock; + const char* prevLocale = ::setlocale(LC_ALL, ""); vmime::charset ch(::nl_langinfo(CODESET)); ::setlocale(LC_ALL, prevLocale);