1 /*
2  * ThreadableJob.h - declaration of class ThreadableJob
3  *
4  * Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5  *
6  * This file is part of LMMS - https://lmms.io
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program (see COPYING); if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  *
23  */
24 
25 #ifndef THREADABLE_JOB_H
26 #define THREADABLE_JOB_H
27 
28 #include "AtomicInt.h"
29 
30 #include "lmms_basics.h"
31 
32 
33 class ThreadableJob
34 {
35 public:
36 
37 	enum ProcessingState
38 	{
39 		Unstarted,
40 		Queued,
41 		InProgress,
42 		Done
43 	};
44 
ThreadableJob()45 	ThreadableJob() :
46 		m_state( ThreadableJob::Unstarted )
47 	{
48 	}
49 
state()50 	inline ProcessingState state() const
51 	{
52 		return static_cast<ProcessingState>( (int) m_state );
53 	}
54 
reset()55 	inline void reset()
56 	{
57 		m_state = Unstarted;
58 	}
59 
queue()60 	inline void queue()
61 	{
62 		m_state = Queued;
63 	}
64 
done()65 	inline void done()
66 	{
67 		m_state = Done;
68 	}
69 
process()70 	void process()
71 	{
72 		if( m_state.testAndSetOrdered( Queued, InProgress ) )
73 		{
74 			doProcessing();
75 			m_state = Done;
76 		}
77 	}
78 
79 	virtual bool requiresProcessing() const = 0;
80 
81 
82 protected:
83 	virtual void doProcessing() = 0;
84 
85 	AtomicInt m_state;
86 
87 } ;
88 
89 #endif
90