Refactored: More information hiding
[TortoiseGit.git] / src / AsyncFramework / JobBase.h
blob6851ff2878774f8a65448f0f912eed021135d461
1 /***************************************************************************
2 * Copyright (C) 2009-2010, 2012 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 #pragma once
23 #include "IJob.h"
24 #include "WaitableEvent.h"
26 namespace async
29 /**
30 * Common base implementation of \ref IJob.
31 * All your job class deriving from this one
32 * has to do is to implement \ref InternalExecute.
35 class CJobBase : public IJob
37 private:
39 /// waitable event. Will be set after \ref Execute() finished.
41 COneShotEvent executionDone;
43 /// waitable event. Will be set when the job can be deleted.
45 COneShotEvent deletable;
47 /// TRUE until Execute() is called
49 volatile LONG waiting;
51 /// if set, we should not run at all or at least try to terminate asap
53 volatile LONG terminated;
55 /// if set, \ref finished will not be signalled unless
56 /// \ref Execute is called from the scheduler.
58 volatile LONG scheduled;
60 /// For now, update the internal @a scheduled flag only.
62 void OnSchedule (CJobScheduler* scheduler);
64 /// For now, update the internal @a scheduled flag only.
66 void OnUnSchedule (CJobScheduler* scheduler);
68 protected:
70 /// base class is not intended for creation
72 CJobBase(void);
74 /// implement this in your job class
76 virtual void InternalExecute() = 0;
78 /// asserts that the job is deletable
80 virtual ~CJobBase(void);
82 public:
84 /// call this to put the job into the scheduler
86 virtual void Schedule (bool transferOwnership, CJobScheduler* scheduler) override;
88 // will be called by job execution thread
90 virtual void Execute() override;
92 /// may be called by other (observing) threads
94 virtual Status GetStatus() const override;
96 /// wait until job execution finished.
97 /// If @ref inlineExecution is set, the job will be
98 /// executed in the current thread if it is still waiting.
100 virtual void WaitUntilDone (bool inlineExecution = false) override;
102 /// returns false in case of a timeout
104 virtual bool WaitUntilDoneOrTimeout(DWORD milliSeconds);
106 /// request early termination.
107 /// Will even prevent execution if not yet started.
108 /// Execution will still finish 'successfully', i.e
109 /// results in \ref IJob::done state.
111 virtual void Terminate();
113 /// \returns true if termination has been requested.
114 /// Please note that execution may still be ongoing
115 /// despite the termination request.
117 virtual bool HasBeenTerminated() const;
119 /// Call @a delete on this job as soon as it is safe to do so.
121 virtual void Delete (bool terminate);