Fix log threadname lookup handling not beeing threadsafe
authorsapier <Sapier at GMX dot net>
Sat, 30 Nov 2013 20:22:15 +0000 (21:22 +0100)
committersapier <Sapier at GMX dot net>
Sat, 30 Nov 2013 20:22:15 +0000 (21:22 +0100)
src/jthread/pthread/jmutex.cpp
src/jthread/win32/jmutex.cpp
src/log.cpp

index 29e0baac00d32b1eb871ce36e3edbe540bcdea83..3dfad5e6c0d3c988c21a9d0010975b355e9211fd 100644 (file)
@@ -29,7 +29,8 @@
 
 JMutex::JMutex()
 {
-       initialized = false;
+       pthread_mutex_init(&mutex,NULL);
+       initialized = true;
 }
 
 JMutex::~JMutex()
@@ -40,19 +41,14 @@ JMutex::~JMutex()
 
 int JMutex::Init()
 {
-       if (initialized)
-               return ERR_JMUTEX_ALREADYINIT;
-       
-       pthread_mutex_init(&mutex,NULL);
-       initialized = true;
-       return 0;       
+       return 0;
 }
 
 int JMutex::Lock()
 {
        if (!initialized)
                return ERR_JMUTEX_NOTINIT;
-               
+
        pthread_mutex_lock(&mutex);
        return 0;
 }
@@ -61,7 +57,7 @@ int JMutex::Unlock()
 {
        if (!initialized)
                return ERR_JMUTEX_NOTINIT;
-       
+
        pthread_mutex_unlock(&mutex);
        return 0;
 }
index d079d448d70c512596aca7e5373a46bce968e919..8a31495cd255373cb175e008e6935bde1843df13 100644 (file)
 
 JMutex::JMutex()
 {
-       initialized = false;
+#ifdef JMUTEX_CRITICALSECTION
+       InitializeCriticalSection(&mutex);
+#else
+       mutex = CreateMutex(NULL,FALSE,NULL);
+       if (mutex == NULL)
+               return ERR_JMUTEX_CANTCREATEMUTEX;
+#endif // JMUTEX_CRITICALSECTION
+       initialized = true;
 }
 
 JMutex::~JMutex()
@@ -44,16 +51,6 @@ JMutex::~JMutex()
 
 int JMutex::Init()
 {
-       if (initialized)
-               return ERR_JMUTEX_ALREADYINIT;
-#ifdef JMUTEX_CRITICALSECTION
-       InitializeCriticalSection(&mutex);
-#else
-       mutex = CreateMutex(NULL,FALSE,NULL);
-       if (mutex == NULL)
-               return ERR_JMUTEX_CANTCREATEMUTEX;
-#endif // JMUTEX_CRITICALSECTION
-       initialized = true;
        return 0;
 }
 
index 366d83b64d30d5c826ec3cde0f0afc1b33112a02..527f544802966835b7d59968824e93b9e7f83b3f 100644 (file)
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 std::list<ILogOutput*> log_outputs[LMT_NUM_VALUES];
 std::map<threadid_t, std::string> log_threadnames;
+JMutex                            log_threadnamemutex;
 
 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
 {
@@ -60,13 +61,17 @@ void log_remove_output(ILogOutput *out)
 void log_register_thread(const std::string &name)
 {
        threadid_t id = get_current_thread_id();
+       log_threadnamemutex.Lock();
        log_threadnames[id] = name;
+       log_threadnamemutex.Unlock();
 }
 
 void log_deregister_thread()
 {
        threadid_t id = get_current_thread_id();
+       log_threadnamemutex.Lock();
        log_threadnames.erase(id);
+       log_threadnamemutex.Unlock();
 }
 
 static std::string get_lev_string(enum LogMessageLevel lev)
@@ -144,7 +149,7 @@ public:
                }
                m_buf += c;
        }
-       
+
 private:
        enum LogMessageLevel m_lev;
        std::string m_buf;