No need to set variable to NULL
[TortoiseGit.git] / src / AsyncFramework / JobBase.cpp
blobd3eb3eef3841e0c5b6ac564670261049f55e4045
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()
53 CJobBase::~CJobBase()
55 assert (deletable.Test());
58 // call this to put the job into the scheduler
60 void CJobBase::Schedule (bool transferOwnership, CJobScheduler* scheduler)
62 if (scheduler == NULL)
63 scheduler = CJobScheduler::GetDefault();
65 scheduler->Schedule (this, transferOwnership);
68 // will be called by job execution thread
70 void CJobBase::Execute()
72 // intended for one-shot execution only
73 // skip jobs that have already been executed or
74 // are already/still being executed.
76 if (InterlockedExchange (&waiting, FALSE) == TRUE)
78 if (terminated == FALSE)
79 InternalExecute();
81 executionDone.Set();
83 if (scheduled == FALSE)
84 deletable.Set();
88 // may be called by other (observing) threads
90 IJob::Status CJobBase::GetStatus() const
92 if (waiting == TRUE)
93 return IJob::waiting;
95 return executionDone.Test() ? IJob::done : IJob::running;
98 void CJobBase::WaitUntilDone (bool inlineExecution)
100 if (inlineExecution)
101 Execute();
103 executionDone.WaitFor();
106 bool CJobBase::WaitUntilDoneOrTimeout(DWORD milliSeconds)
108 return executionDone.WaitForEndOrTimeout(milliSeconds);
111 // handle early termination
113 void CJobBase::Terminate()
115 InterlockedExchange (&terminated, TRUE);
118 bool CJobBase::HasBeenTerminated() const
120 return terminated == TRUE;
123 // Call @a delete on this job as soon as it is safe to do so.
125 void CJobBase::Delete (bool terminate)
127 if (terminate)
128 Terminate();
130 if (waiting)
131 Execute();
133 deletable.WaitFor();
134 delete this;