From db9d381a438247d31af44cd8384c7f03ac45ac99 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 19 May 2012 17:04:33 +0000 Subject: [PATCH] added: to thread manager: names of the threads the names are shown in the log file git-svn-id: svn://ttmath.org/publicrep/winix/trunk@837 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/app.cpp | 2 +- core/system.cpp | 4 ++-- core/threadmanager.cpp | 48 +++++++++++++++++++++++++++++++---------- core/threadmanager.h | 16 +++++++++++--- notify/notify.cpp | 2 +- plugins/export/init.cpp | 2 +- 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/core/app.cpp b/core/app.cpp index 4c6302a..4540a82 100755 --- a/core/app.cpp +++ b/core/app.cpp @@ -1305,7 +1305,7 @@ void App::StartThreads() // special thread only for signals pthread_create(&signal_thread, 0, SpecialThreadForSignals, this); - system.thread_manager.Add(&session_manager); + system.thread_manager.Add(&session_manager, L"session_manager"); system.thread_manager.StartAll(); } diff --git a/core/system.cpp b/core/system.cpp index a46cbb6..3d20bcb 100755 --- a/core/system.cpp +++ b/core/system.cpp @@ -87,13 +87,13 @@ void System::Init() image.SetDb(db); image.SetConfig(config); image.SetSystem(this); - thread_manager.Add(&image); + thread_manager.Add(&image, L"image"); crypt.SetConfig(config); // SetSynchro will be called by ThreadManager itself // job.ReadFromFile(); - thread_manager.Add(&job); + thread_manager.Add(&job, L"job"); } diff --git a/core/threadmanager.cpp b/core/threadmanager.cpp index 60c2008..bae9780 100755 --- a/core/threadmanager.cpp +++ b/core/threadmanager.cpp @@ -2,7 +2,7 @@ * This file is a part of Winix * and is not publicly distributed * - * Copyright (c) 2011, Tomasz Sowa + * Copyright (c) 2011-2012, Tomasz Sowa * All rights reserved. * */ @@ -40,17 +40,38 @@ sigset_t set; -void ThreadManager::Add(BaseThread * pbase) +void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name) { - thread_tab.push_back(pbase); +ThreadItem item; + + item.object = pbase; + item.name = thread_name; + thread_tab.push_back(item); if( were_started ) Start(thread_tab.size() - 1); else - log << log4 << "TM: added a thread to the queue, number: " << (thread_tab.size()-1) << logend; + log << log4 << "TM: added a thread to the queue, number: " << (thread_tab.size()-1) + << ", name: " << thread_name << logend; } +void ThreadManager::Add(BaseThread & pbase, const wchar_t * thread_name) +{ + Add(&pbase, thread_name); +} + + +void ThreadManager::Add(BaseThread * pbase, const std::wstring & thread_name) +{ + Add(pbase, thread_name.c_str()); +} + +void ThreadManager::Add(BaseThread & pbase, const std::wstring & thread_name) +{ + Add(&pbase, thread_name.c_str()); +} + void ThreadManager::StartAll() @@ -70,15 +91,17 @@ void ThreadManager::Start(size_t i) { if( i < thread_tab.size() ) { - thread_tab[i]->SetSynchro(synchro); + thread_tab[i].object->SetSynchro(synchro); - if( thread_tab[i]->StartThread() ) + if( thread_tab[i].object->StartThread() ) { - log << log4 << "TM: thread " << i << " (" << thread_tab[i]->ThreadId() << ") started" << logend; + log << log4 << "TM: thread " << i << " (" << thread_tab[i].object->ThreadId() << ", name: " + << thread_tab[i].name << ") started" << logend; } else { - log << log4 << "TM: cannot run a thread, thread number: " << i << logend; + log << log4 << "TM: cannot run a thread, thread number: " << i + << ", name: " << thread_tab[i].name << logend; } } } @@ -93,15 +116,18 @@ void ThreadManager::StopAll() synchro->Lock(); for(size_t i=0 ; iWakeUpThread(); + thread_tab[i].object->WakeUpThread(); synchro->Unlock(); for(size_t i=0 ; iThreadId() << ")" << logend; - thread_tab[i]->WaitForThread(); + log << log4 << "TM: waiting for thread " << i << " (" << thread_tab[i].object->ThreadId() + << ", name: " << thread_tab[i].name << ")" << logend; + + thread_tab[i].object->WaitForThread(); + log << log4 << "TM: thread " << i << " terminated" << logend; } } diff --git a/core/threadmanager.h b/core/threadmanager.h index 9afee50..58aaa05 100755 --- a/core/threadmanager.h +++ b/core/threadmanager.h @@ -2,7 +2,7 @@ * This file is a part of Winix * and is not publicly distributed * - * Copyright (c) 2011, Tomasz Sowa + * Copyright (c) 2011-2012, Tomasz Sowa * All rights reserved. * */ @@ -10,6 +10,7 @@ #ifndef headerfile_winix_core_threadmanager #define headerfile_winix_core_threadmanager +#include #include #include "basethread.h" #include "synchro.h" @@ -31,7 +32,10 @@ public: // adding a new thread to the queue // the thread will be running only if we call StartAll() before // otherwise the thread will be waiting for StartAll() - void Add(BaseThread * pbase); + void Add(BaseThread * pbase, const wchar_t * thread_name); + void Add(BaseThread & pbase, const wchar_t * thread_name); + void Add(BaseThread * pbase, const std::wstring & thread_name); + void Add(BaseThread & pbase, const std::wstring & thread_name); // starting all threads void StartAll(); @@ -42,8 +46,14 @@ public: private: + struct ThreadItem + { + BaseThread * object; + std::wstring name; + }; + Synchro * synchro; - typedef std::vector ThreadTab; + typedef std::vector ThreadTab; ThreadTab thread_tab; bool were_started; diff --git a/notify/notify.cpp b/notify/notify.cpp index cc481e6..b597a22 100755 --- a/notify/notify.cpp +++ b/notify/notify.cpp @@ -56,7 +56,7 @@ void Notify::Init() notify_thread.SetUsers(users); notify_thread.SetNotifyPool(¬ify_pool); notify_thread.SetPatterns(&patterns); - thread_manager->Add(¬ify_thread); + thread_manager->Add(¬ify_thread, L"notifications"); patterns.SetUTF8(config->utf8); patterns.SetDirectories(config->txt_templates_dir, config->txt_templates_dir_default); diff --git a/plugins/export/init.cpp b/plugins/export/init.cpp index d747163..fe28fb1 100755 --- a/plugins/export/init.cpp +++ b/plugins/export/init.cpp @@ -53,7 +53,7 @@ void InitPlugin(PluginInfo & info) export_thread.SetUTF8(info.config->utf8); export_thread.SetBaseUrl(info.config->base_url); - info.system->thread_manager.Add(&export_thread); + info.system->thread_manager.Add(&export_thread, L"export"); export_info.ReadExportDirs(); }