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

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