fixed: ThreadMenager should use Lock/Unlock in StartAll() method
added: Job class (system->job object)
a general mechanism for jobs (by using PT::Space as a job structure)
WINIX_JOB plugin message will be sent with a pointer to PT::Space
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@829 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
167
core/job.cpp
Executable file
167
core/job.cpp
Executable file
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2012, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "job.h"
|
||||
#include "plugin.h"
|
||||
#include "log.h"
|
||||
|
||||
|
||||
|
||||
Job::Job()
|
||||
{
|
||||
jobs_queue_tab.resize(WINIX_JOBS_HOW_MANY_PRIORITIES);
|
||||
}
|
||||
|
||||
|
||||
void Job::CheckPriority(int & priority) const
|
||||
{
|
||||
if( priority < 0 )
|
||||
priority = 0;
|
||||
|
||||
if( priority >= WINIX_JOBS_HOW_MANY_PRIORITIES )
|
||||
priority = WINIX_JOBS_HOW_MANY_PRIORITIES - 1;
|
||||
}
|
||||
|
||||
|
||||
// first thread (objects locked)
|
||||
void Job::Add(PT::Space & job, int priority)
|
||||
{
|
||||
CheckPriority(priority);
|
||||
jobs_queue_tab[priority].push(job);
|
||||
WakeUpThread();
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Job::Size(int priority) const
|
||||
{
|
||||
CheckPriority(priority);
|
||||
return jobs_queue_tab[priority].size();
|
||||
}
|
||||
|
||||
|
||||
|
||||
size_t Job::Size() const
|
||||
{
|
||||
size_t sum = 0;
|
||||
|
||||
for(size_t i=0 ; i<WINIX_JOBS_HOW_MANY_PRIORITIES ; ++i)
|
||||
sum += Size(i);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
bool Job::Empty(int priority) const
|
||||
{
|
||||
CheckPriority(priority);
|
||||
return jobs_queue_tab[priority].empty();
|
||||
}
|
||||
|
||||
|
||||
bool Job::Empty() const
|
||||
{
|
||||
for(size_t i=0 ; i<WINIX_JOBS_HOW_MANY_PRIORITIES ; ++i)
|
||||
if( !Empty(i) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
second thread
|
||||
*/
|
||||
|
||||
// second thread (objects locked)
|
||||
bool Job::SignalReceived()
|
||||
{
|
||||
return !Empty();
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::Do()
|
||||
{
|
||||
size_t i = WINIX_JOBS_HOW_MANY_PRIORITIES;
|
||||
bool is_empty;
|
||||
|
||||
while( i-- > 0 && !IsExitSignal() )
|
||||
{
|
||||
do
|
||||
{
|
||||
Lock();
|
||||
is_empty = Empty(i);
|
||||
Unlock();
|
||||
|
||||
if( !is_empty )
|
||||
DoQueue(jobs_queue_tab[i]);
|
||||
}
|
||||
while( !is_empty && !IsExitSignal() );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked, jobs_queue is not empty)
|
||||
void Job::DoQueue(JobsQueue & jobs_queue)
|
||||
{
|
||||
bool is_empty;
|
||||
|
||||
do
|
||||
{
|
||||
Lock();
|
||||
// references will not be invalidated after insertion to jobs_queue
|
||||
// (jobs_queue is std::queue and it uses std::deque by default)
|
||||
PT::Space & job = jobs_queue.front();
|
||||
Unlock();
|
||||
|
||||
DoJob(job);
|
||||
|
||||
Lock();
|
||||
jobs_queue.pop();
|
||||
is_empty = jobs_queue.empty();
|
||||
Unlock();
|
||||
}
|
||||
while( !is_empty && !IsExitSignal() );
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::DoJob(PT::Space & job)
|
||||
{
|
||||
try
|
||||
{
|
||||
// !! FIXME plugin.Call() can log messages
|
||||
// we need Lock/Unlock mechanism there
|
||||
PluginRes res = plugin.Call((Session*)0, WINIX_JOB, &job);
|
||||
|
||||
if( res.res_true == 0 )
|
||||
DoWinixJob(job);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// second thread (objects not locked)
|
||||
void Job::DoWinixJob(PT::Space & job)
|
||||
{
|
||||
Lock();
|
||||
//log << log1 << "standard winix job: " << job.Text(L"type") << logend;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user