1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "JobScheduler.h"
28 // For now, update the internal @a scheduled flag only.
30 void CJobBase::OnSchedule (CJobScheduler
*)
32 if (InterlockedExchange (&scheduled
, TRUE
) == TRUE
)
36 // For now, update the internal @a scheduled flag only.
38 void CJobBase::OnUnSchedule (CJobScheduler
*)
40 if (InterlockedExchange (&scheduled
, FALSE
) == FALSE
)
43 if (executionDone
.Test())
47 // nothing special during construction / destuction
49 CJobBase::CJobBase(void)
56 CJobBase::~CJobBase(void)
58 assert (deletable
.Test());
61 // call this to put the job into the scheduler
63 void CJobBase::Schedule (bool transferOwnership
, CJobScheduler
* scheduler
)
65 if (scheduler
== NULL
)
66 scheduler
= CJobScheduler::GetDefault();
68 scheduler
->Schedule (this, transferOwnership
);
71 // will be called by job execution thread
73 void CJobBase::Execute()
75 // intended for one-shot execution only
76 // skip jobs that have already been executed or
77 // are already/still being executed.
79 if (InterlockedExchange (&waiting
, FALSE
) == TRUE
)
81 if (terminated
== FALSE
)
86 if (scheduled
== FALSE
)
91 // may be called by other (observing) threads
93 IJob::Status
CJobBase::GetStatus() const
98 return executionDone
.Test() ? IJob::done
: IJob::running
;
101 void CJobBase::WaitUntilDone (bool inlineExecution
)
106 executionDone
.WaitFor();
109 bool CJobBase::WaitUntilDoneOrTimeout(DWORD milliSeconds
)
111 return executionDone
.WaitForEndOrTimeout(milliSeconds
);
114 // handle early termination
116 void CJobBase::Terminate()
118 InterlockedExchange (&terminated
, TRUE
);
121 bool CJobBase::HasBeenTerminated() const
123 return terminated
== TRUE
;
126 // Call @a delete on this job as soon as it is safe to do so.
128 void CJobBase::Delete (bool terminate
)