OpendTect  6.6
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  RCS: $Id$
10 ________________________________________________________________________
11 
12 
13 -*/
14 
15 #include "basicmod.h"
16 
17 #include "bufstringset.h"
18 #include "callback.h"
19 #include "genc.h"
20 #include "objectset.h"
21 #include "task.h"
22 #include "uistring.h"
23 
24 typedef bool (*StaticTaskFunction)();
25 typedef bool (CallBacker::*TaskFunction)();
26 
27 namespace Threads
28 {
29 class Thread;
30 class ConditionVar;
31 class WorkThread;
32 class Work;
33 
34 
42 public:
43 
44  //Interface from outside world
45  WorkManager(int nrthreads=-1);
47 
48  enum QueueType { MultiThread, SingleThread, Manual };
49  int addQueue(QueueType type,const char* name);
55  void setQueueName(int queueid,const char*);
56  int queueSize(int queueid) const;
57  void emptyQueue(int queueid,bool finishall);
58  void removeQueue(int queueid,bool finishall);
61  static int cDefaultQueueID() { return 0; }
62  bool executeQueue(int queueid);
66  void addWork(const Work&,CallBack* finished =0,
67  int queueid=cDefaultQueueID(),
68  bool putfirstinline=false,
69  bool discardduplicates=false,
70  bool forcedifferentthread=false);
76  bool addWork(TypeSet<Work>&, int queueid = -1,
77  bool firstinline = false);
82  bool executeWork( Work*, int sz, int queueid = -1,
83  bool firstinline = false );
85  bool removeWork(const Work&);
91  const Work* getWork(CallBacker*) const;
113  int nrThreads() const { return threads_.size(); }
114  int nrFreeThreads() const;
116  bool isWorkThread() const;
117 
119 
121 
123  void shutdown();
124 
125 protected:
126 
127  int queueSizeNoLock(int queueid) const;
129  int oldqueueid );
130  inline void reduceWorkload(int queueidx);
131 
132  friend class WorkThread;
133 
134  //Linked (one entry per que-entry)
138 
142 
143  //Linked (one entry per queue)
145  TypeSet<int> queueworkload_; //Nr threads working on it
149 
151 
152  int freeid_;
153  const int twmid_;
154 };
155 
156 
190 public:
191  inline Work();
192  inline Work(const CallBack&);
193  inline Work(CallBacker* o,TaskFunction f);
194  inline Work(StaticTaskFunction f);
195  inline Work(Task& t,bool takeover);
196 
197  bool operator==(const Work&) const;
198 
199  inline bool isOK() const;
200  inline bool execute();
201  inline uiString errMsg() const { return msg_; }
202 
203 private:
204 
205  void destroy();
210  bool takeover_;
211 
213 
214  friend class WorkThread;
215  friend class WorkManager;
216 };
217 
218 #define mSTFN(clss,fn) ((::TaskFunction)(&clss::fn))
219 #define mWMT(obj,clss,fn) ::Threads::Work( obj, mSTFN(clss,fn) )
220 
221 
222 }; // Namespace
223 
225  : obj_( 0 ), cbf_( 0 ), tf_( 0 ), stf_( 0 ) {}
226 
227 
228 inline Threads::Work::Work( const CallBack& cb )
229  : obj_( const_cast<CallBacker*>(cb.cbObj()) )
230  , cbf_( cb.cbFn() ), tf_( 0 ), stf_( 0 ) {}
231 
232 
234  : obj_( o ), cbf_( 0 ), tf_( f ), stf_( 0 ), takeover_( false ) {}
235 
236 
238  : obj_( 0 ), cbf_( 0 ), tf_( 0 ), stf_( f ) {}
239 
240 
241 inline Threads::Work::Work( Task& t, bool takeover )
242  : obj_( &t ), cbf_( 0 ), tf_( mSTFN(Task,execute) ), stf_( 0 )
243  , takeover_( takeover ) {}
244 
245 
246 inline bool Threads::Work::isOK() const
247 { return stf_ || (obj_ && (tf_ || cbf_) ); }
248 
249 
251 {
252  if ( stf_ ) return stf_();
253  if ( tf_ )
254  {
255  const bool res = (obj_->*tf_)();
256  if ( res )
257  msg_ = uiString::emptyString();
258  else
259  {
260  mDynamicCastGet(Task*,task,obj_)
261  msg_ = task ? task->uiMessage() : uiString::emptyString();
262  }
263 
264  if ( takeover_ ) delete obj_;
265 
266  return res;
267  }
268 
269  (obj_->*cbf_)( 0 );
270 
271  return true;
272 }
Threads::WorkManager::shutdown
void shutdown()
Threads::WorkManager::addWork
void addWork(const Work &, CallBack *finished=0, int queueid=cDefaultQueueID(), bool putfirstinline=false, bool discardduplicates=false, bool forcedifferentthread=false)
Threads::Work::obj_
CallBacker * obj_
Definition: threadwork.h:206
Threads::WorkManager::queuenames_
BufferStringSet queuenames_
Definition: threadwork.h:147
Threads::WorkManager::freethreads_
ObjectSet< WorkThread > freethreads_
Definition: threadwork.h:141
Threads::WorkManager::addWork
bool addWork(TypeSet< Work > &, int queueid=-1, bool firstinline=false)
Threads::Work::tf_
TaskFunction tf_
Definition: threadwork.h:208
Threads::WorkManager::getWork
const Work * getWork(CallBacker *) const
task.h
Threads::WorkManager::twm
static Threads::WorkManager & twm()
uistring.h
ObjectSet< WorkThread >
Threads::WorkManager::threads_
ObjectSet< WorkThread > threads_
Definition: threadwork.h:139
BufferStringSet
Set of BufferString objects.
Definition: bufstringset.h:26
Threads::WorkManager::workloadcond_
ConditionVar & workloadcond_
Definition: threadwork.h:150
Threads::WorkManager::reduceWorkload
void reduceWorkload(int queueidx)
mExpClass
#define mExpClass(module)
Definition: commondefs.h:177
Threads::WorkManager::removeQueue
void removeQueue(int queueid, bool finishall)
Threads::Work::execute
bool execute()
Definition: threadwork.h:250
CallBackFunction
void(CallBacker::*)(CallBacker *) CallBackFunction
Definition: callback.h:38
mDynamicCastGet
#define mDynamicCastGet(typ, out, in)
Definition: commondefs.h:148
Threads::WorkManager::queuetypes_
TypeSet< QueueType > queuetypes_
Definition: threadwork.h:146
genc.h
Threads::WorkManager::cDefaultQueueID
static int cDefaultQueueID()
Definition: threadwork.h:61
callback.h
CallBacker
Inherit from this class to be able to send and/or receive CallBacks.
Definition: callback.h:185
Threads::WorkManager::freeid_
int freeid_
Definition: threadwork.h:152
Threads::WorkManager::reportFinishedAndAskForMore
int reportFinishedAndAskForMore(WorkThread *, int oldqueueid)
Threads::WorkManager::nrFreeThreads
int nrFreeThreads() const
Valid right now, may change any time.
Threads::Work::isOK
bool isOK() const
Definition: threadwork.h:246
mODTextTranslationClass
#define mODTextTranslationClass(clss)
Definition: uistring.h:40
Threads::WorkManager::emptyQueue
void emptyQueue(int queueid, bool finishall)
Threads::Work::Work
Work()
Definition: threadwork.h:224
Threads::Work::stf_
StaticTaskFunction stf_
Definition: threadwork.h:209
Threads::WorkManager::queueworkload_
TypeSet< int > queueworkload_
Definition: threadwork.h:145
Threads::WorkManager::isidle
Notifier< WorkManager > isidle
Definition: threadwork.h:118
Threads::WorkManager::callbacks_
TypeSet< CallBack > callbacks_
Definition: threadwork.h:137
Threads::WorkManager::workload_
TypeSet< Work > workload_
Definition: threadwork.h:135
CallBack
CallBacks object-oriented (object + method).
Definition: callback.h:62
Threads::WorkManager::SingleThread
@ SingleThread
Definition: threadwork.h:48
Threads::Work::takeover_
bool takeover_
Definition: threadwork.h:210
Threads::WorkManager::isWorkThread
bool isWorkThread() const
Task
Generalization of something (e.g. a computation) that needs to be done in multiple steps.
Definition: task.h:28
Threads::WorkManager
Takes work and puts it on a queue for execution either in parallel, singlethread or manual.
Definition: threadwork.h:41
Threads::WorkManager::queueSize
int queueSize(int queueid) const
Threads::ConditionVar
Is an object that faciliates many threads to wait for something to happen.
Definition: thread.h:110
Threads::WorkManager::uiMessage
uiString uiMessage(CallBacker *) const
Notifier
Class to help setup a callback handling.
Definition: notify.h:126
Threads::WorkManager::getWorkExitStatus
bool getWorkExitStatus(CallBacker *) const
Threads::WorkManager::queueids_
TypeSet< int > queueids_
Definition: threadwork.h:144
Threads::WorkManager::workqueueid_
TypeSet< int > workqueueid_
Definition: threadwork.h:136
Threads::WorkManager::queueSizeNoLock
int queueSizeNoLock(int queueid) const
Threads::WorkManager::removeWork
bool removeWork(const Work &)
Threads::WorkManager::executeQueue
bool executeQueue(int queueid)
Threads::WorkManager::nrThreads
int nrThreads() const
Definition: threadwork.h:113
StaticTaskFunction
bool(* StaticTaskFunction)()
Definition: threadwork.h:24
Threads::WorkManager::isShuttingDown
Notifier< WorkManager > isShuttingDown
Definition: threadwork.h:122
TaskFunction
bool(CallBacker::* TaskFunction)()
Definition: threadwork.h:25
bufstringset.h
Threads::Work::errMsg
uiString errMsg() const
Definition: threadwork.h:201
uiString
String that is able to hold international (UTF-8) strings for the user interface.
Definition: uistring.h:121
uiString::emptyString
static const uiString & emptyString()
Definition: uistring.h:300
Threads::Work::cbf_
CallBackFunction cbf_
Definition: threadwork.h:207
Threads::Work::operator==
bool operator==(const Work &) const
Threads::WorkManager::setQueueName
void setQueueName(int queueid, const char *)
Threads::Work::msg_
uiString msg_
Definition: threadwork.h:212
Threads::WorkManager::WorkManager
WorkManager(int nrthreads=-1)
Threads::WorkManager::executeWork
bool executeWork(Work *, int sz, int queueid=-1, bool firstinline=false)
Returns when finished with all.
Threads::WorkManager::threadids_
ObjectSet< const void > threadids_
Definition: threadwork.h:140
Threads::WorkManager::queueisclosing_
BoolTypeSet queueisclosing_
Definition: threadwork.h:148
Threads::Work
The abstraction of something that can be done. It can be an ordinary CallBack, a static function (mus...
Definition: threadwork.h:189
Threads
interface to threads that should be portable.
Definition: atomic.h:23
objectset.h
Threads::WorkManager::QueueType
QueueType
Definition: threadwork.h:48
Threads::WorkManager::twmid_
const int twmid_
Only for debugging.
Definition: threadwork.h:153
Threads::WorkManager::addQueue
int addQueue(QueueType type, const char *name)
Threads::Work::destroy
void destroy()
mSTFN
#define mSTFN(clss, fn)
Definition: threadwork.h:218
Threads::WorkManager::~WorkManager
~WorkManager()
TypeSet
Sets of (small) copyable elements.
Definition: commontypes.h:29

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