Drop unneeded SDK definitions
[TortoiseGit.git] / src / AsyncFramework / JobBase.cpp
blobe73befbcd2aff2153045e624ffe0d36317058921
1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #include "stdafx.h"
22 #include "JobBase.h"
23 #include "JobScheduler.h"
25 namespace async
28 // For now, update the internal @a scheduled flag only.
30 void CJobBase::OnSchedule (CJobScheduler*)
32 if (InterlockedExchange (&scheduled, TRUE) == TRUE)
33 assert (0);
36 // For now, update the internal @a scheduled flag only.
38 void CJobBase::OnUnSchedule (CJobScheduler*)
40 if (InterlockedExchange (&scheduled, FALSE) == FALSE)
41 assert (0);
42 else
43 if (executionDone.Test())
44 deletable.Set();
47 // nothing special during construction / destuction
49 CJobBase::CJobBase(void)
50 : waiting (TRUE)
51 , terminated (FALSE)
52 , scheduled (FALSE)
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)
82 InternalExecute();
84 executionDone.Set();
86 if (scheduled == FALSE)
87 deletable.Set();
91 // may be called by other (observing) threads
93 IJob::Status CJobBase::GetStatus() const
95 if (waiting == TRUE)
96 return IJob::waiting;
98 return executionDone.Test() ? IJob::done : IJob::running;
101 void CJobBase::WaitUntilDone (bool inlineExecution)
103 if (inlineExecution)
104 Execute();
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)
130 if (terminate)
131 Terminate();
133 if (waiting)
134 Execute();
136 deletable.WaitFor();
137 delete this;