2012-04-26 00:24:37 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2012-2014, Tomasz Sowa
|
2012-04-26 00:24:37 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2012-04-26 00:24:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "job.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-26 00:24:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2012-04-26 00:24:37 +02:00
|
|
|
|