OpendTect-6_4  6.4
threadwork.h
Go to the documentation of this file.
1 #ifndef threadwork_h
2 #define threadwork_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 4-11-2002
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 -*/
15 
16 #include "basicmod.h"
17 #include "task.h"
18 #include "genc.h"
19 #include "objectset.h"
20 #include "callback.h"
21 #include "bufstringset.h"
22 
23 typedef bool (*StaticTaskFunction)();
24 typedef bool (CallBacker::*TaskFunction)();
25 
26 namespace Threads
27 {
28 class Thread;
29 class ConditionVar;
30 class WorkThread;
31 class Work;
32 
33 
40 {
41 public:
42 
43  //Interface from outside world
44  WorkManager(int nrthreads=-1);
45  ~WorkManager();
46 
47  enum QueueType { MultiThread, SingleThread, Manual };
48  int addQueue(QueueType type,const char* name);
54  void setQueueName(int queueid,const char*);
55  int queueSize(int queueid) const;
56  void emptyQueue(int queueid,bool finishall);
57  void removeQueue(int queueid,bool finishall);
60  static int cDefaultQueueID() { return 0; }
61  bool executeQueue(int queueid);
65  void addWork(const Work&,CallBack* finished =0,
66  int queueid=cDefaultQueueID(),
67  bool putfirstinline=false,
68  bool discardduplicates=false,
69  bool forcedifferentthread=false);
75  bool addWork(TypeSet<Work>&, int queueid = -1,
76  bool firstinline = false);
77 
78  bool executeWork( Work*, int sz, int queueid = -1,
79  bool firstinline = false );
81  bool removeWork(const Work&);
87  const Work* getWork(CallBacker*) const;
95  bool getWorkExitStatus(CallBacker*) const;
104  int nrThreads() const { return threads_.size(); }
105  int nrFreeThreads() const;
107  bool isWorkThread() const;
108 
110 
111  static Threads::WorkManager& twm();
112 
114  void shutdown();
115  void setQuickStop( bool yn ) { quickstop_ = yn; }
117 
118 protected:
119 
120  int queueSizeNoLock(int queueid) const;
121  int reportFinishedAndAskForMore(WorkThread*,
122  int oldqueueid );
123  inline void reduceWorkload(int queueidx);
124 
125  friend class WorkThread;
126 
127  //Linked (one entry per que-entry)
131 
135 
136  //Linked (one entry per queue)
138  TypeSet<int> queueworkload_; //Nr threads working on it
142 
144 
145  int freeid_;
146  const int twmid_;
148 };
149 
150 
183 {
184 public:
185  inline Work();
186  inline Work(const CallBack&);
187  inline Work(CallBacker* o,TaskFunction f);
188  inline Work(StaticTaskFunction f);
189  inline Work(Task& t,bool takeover);
190  bool operator==(const Work&) const;
191 
192  inline bool isOK() const;
193  inline bool doRun();
194 
195 protected:
196 
197  friend class WorkThread;
198  friend class WorkManager;
199  void destroy();
204  bool takeover_;
205 };
206 
207 #define mSTFN(clss,fn) ((::TaskFunction)(&clss::fn))
208 #define mWMT(obj,clss,fn) ::Threads::Work( obj, mSTFN(clss,fn) )
209 
210 
211 }; // Namespace
212 
214  : obj_( 0 ), cbf_( 0 ), tf_( 0 ), stf_( 0 ) {}
215 
216 
217 inline Threads::Work::Work( const CallBack& cb )
218  : obj_( const_cast<CallBacker*>(cb.cbObj()) )
219  , cbf_( cb.cbFn() ), tf_( 0 ), stf_( 0 ) {}
220 
221 
223  : obj_( o ), cbf_( 0 ), tf_( f ), stf_( 0 ), takeover_( false ) {}
224 
225 
227  : obj_( 0 ), cbf_( 0 ), tf_( 0 ), stf_( f ) {}
228 
229 
230 inline Threads::Work::Work( Task& t, bool takeover )
231  : obj_( &t ), cbf_( 0 ), tf_( mSTFN(Task,execute) ), stf_( 0 )
232  , takeover_( takeover ) {}
233 
234 
235 inline bool Threads::Work::isOK() const
236 { return stf_ || (obj_ && (tf_ || cbf_ ) ); }
237 
238 
239 inline bool Threads::Work::doRun()
240 {
241  if ( stf_ ) return stf_();
242  if ( tf_ )
243  {
244  const bool res = (obj_->*tf_)();
245  if ( takeover_ ) delete obj_;
246  return res;
247  }
248 
249  (obj_->*cbf_)( 0 );
250 
251  return true;
252 }
253 
254 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:108
To be able to send and/or receive CallBacks, inherit from this class.
Definition: callback.h:272
TypeSet< int > workqueueid_
Definition: threadwork.h:129
void setQuickStop(bool yn)
Only for MMP batchprogs.
Definition: threadwork.h:115
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
Notifier< WorkManager > isShuttingDown
Definition: threadwork.h:113
TypeSet< int > queueids_
Definition: threadwork.h:137
CallBacker * obj_
Definition: threadwork.h:200
bool isOK() const
Definition: threadwork.h:235
void(CallBacker::* CallBackFunction)(CallBacker *)
Definition: callback.h:38
bool(* StaticTaskFunction)()
Definition: threadwork.h:23
Set of BufferString objects.
Definition: bufstringset.h:28
#define mSTFN(clss, fn)
Definition: threadwork.h:207
int nrThreads() const
Definition: threadwork.h:104
CallBackFunction cbf_
Definition: threadwork.h:201
Takes work and puts it on a queue for execution either in parallel, singlethread or manual...
Definition: threadwork.h:39
Work()
Definition: threadwork.h:213
Definition: threadwork.h:47
Definition: callback.h:254
bool(CallBacker::* TaskFunction)()
Definition: threadwork.h:24
interface to threads that should be portable.
Definition: atomic.h:28
ObjectSet< const void > threadids_
Definition: threadwork.h:133
Set of (small) copyable elements.
Definition: commontypes.h:30
BoolTypeSet queueisclosing_
Definition: threadwork.h:141
const int twmid_
Only for debugging.
Definition: threadwork.h:146
QueueType
Definition: threadwork.h:47
bool takeover_
Definition: threadwork.h:204
TaskFunction tf_
Definition: threadwork.h:202
BufferStringSet queuenames_
Definition: threadwork.h:140
ObjectSet< WorkThread > threads_
Definition: threadwork.h:132
ObjectSet< WorkThread > freethreads_
Definition: threadwork.h:134
ConditionVar & workloadcond_
Definition: threadwork.h:143
TypeSet< QueueType > queuetypes_
Definition: threadwork.h:139
bool doRun()
Definition: threadwork.h:239
static int cDefaultQueueID()
Definition: threadwork.h:60
bool quickstop_
Definition: threadwork.h:147
int freeid_
Definition: threadwork.h:145
Notifier< WorkManager > isidle
Definition: threadwork.h:109
CallBacks object-oriented (object + method).
Definition: callback.h:57
StaticTaskFunction stf_
Definition: threadwork.h:203
Generalization of something (e.g. a computation) that needs to be done in multiple steps...
Definition: task.h:28
TypeSet< int > queueworkload_
Definition: threadwork.h:138
TypeSet< CallBack > callbacks_
Definition: threadwork.h:130
TypeSet< Work > workload_
Definition: threadwork.h:128
The abstraction of something that can be done. It can be an ordinary CallBack, a static function (mus...
Definition: threadwork.h:182

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019