2011-07-29 00:18:10 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2012-05-19 19:04:33 +02:00
|
|
|
* Copyright (c) 2011-2012, Tomasz Sowa
|
2011-07-29 00:18:10 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include "threadmanager.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
|
|
ThreadManager::ThreadManager()
|
|
|
|
{
|
|
|
|
were_started = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadManager::SetSynchro(Synchro * psynchro)
|
|
|
|
{
|
|
|
|
synchro = psynchro;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadManager::Init()
|
|
|
|
{
|
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGTERM);
|
|
|
|
sigaddset(&set, SIGINT);
|
|
|
|
|
|
|
|
// blocking SIGTERM and SIGINT
|
|
|
|
// new threads will have the signals blocked too
|
|
|
|
pthread_sigmask(SIG_BLOCK, &set, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-19 19:04:33 +02:00
|
|
|
void ThreadManager::Add(BaseThread * pbase, const wchar_t * thread_name)
|
2011-07-29 00:18:10 +02:00
|
|
|
{
|
2012-05-19 19:04:33 +02:00
|
|
|
ThreadItem item;
|
|
|
|
|
|
|
|
item.object = pbase;
|
|
|
|
item.name = thread_name;
|
|
|
|
thread_tab.push_back(item);
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
if( were_started )
|
|
|
|
Start(thread_tab.size() - 1);
|
|
|
|
else
|
2012-05-19 19:04:33 +02:00
|
|
|
log << log4 << "TM: added a thread to the queue, number: " << (thread_tab.size()-1)
|
|
|
|
<< ", name: " << thread_name << logend;
|
2011-07-29 00:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-19 19:04:33 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ThreadManager::StartAll()
|
|
|
|
{
|
2012-04-26 00:24:37 +02:00
|
|
|
synchro->Lock();
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
|
|
|
Start(i);
|
|
|
|
|
2012-04-26 00:24:37 +02:00
|
|
|
synchro->Unlock();
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
were_started = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadManager::Start(size_t i)
|
|
|
|
{
|
|
|
|
if( i < thread_tab.size() )
|
|
|
|
{
|
2012-05-19 19:04:33 +02:00
|
|
|
thread_tab[i].object->SetSynchro(synchro);
|
2011-09-05 17:38:09 +02:00
|
|
|
|
2012-05-19 19:04:33 +02:00
|
|
|
if( thread_tab[i].object->StartThread() )
|
2011-07-29 00:18:10 +02:00
|
|
|
{
|
2012-05-19 19:04:33 +02:00
|
|
|
log << log4 << "TM: thread " << i << " (" << thread_tab[i].object->ThreadId() << ", name: "
|
|
|
|
<< thread_tab[i].name << ") started" << logend;
|
2011-07-29 00:18:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-19 19:04:33 +02:00
|
|
|
log << log4 << "TM: cannot run a thread, thread number: " << i
|
|
|
|
<< ", name: " << thread_tab[i].name << logend;
|
2011-07-29 00:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadManager::StopAll()
|
|
|
|
{
|
|
|
|
if( !were_started )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// WakeUpThread() should be used with Lock/Unlock
|
|
|
|
synchro->Lock();
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
2012-05-19 19:04:33 +02:00
|
|
|
thread_tab[i].object->WakeUpThread();
|
2011-07-29 00:18:10 +02:00
|
|
|
|
|
|
|
synchro->Unlock();
|
|
|
|
|
|
|
|
|
|
|
|
for(size_t i=0 ; i<thread_tab.size() ; ++i)
|
|
|
|
{
|
2012-05-19 19:04:33 +02:00
|
|
|
log << log4 << "TM: waiting for thread " << i << " (" << thread_tab[i].object->ThreadId()
|
|
|
|
<< ", name: " << thread_tab[i].name << ")" << logend;
|
|
|
|
|
|
|
|
thread_tab[i].object->WaitForThread();
|
|
|
|
|
2011-07-29 00:18:10 +02:00
|
|
|
log << log4 << "TM: thread " << i << " terminated" << logend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|