Seg3D  2.4
Seg3D is a free volume segmentation and processing tool developed by the NIH Center for Integrative Biomedical Computing at the University of Utah Scientific Computing and Imaging (SCI) Institute.
the_thread_interface.hxx
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2016 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 // File : the_thread_interface.hxx
30 // Author : Pavel A. Koshevoy
31 // Created : Fri Feb 16 09:20:00 MST 2007
32 // Copyright : (C) 2004-2008 University of Utah
33 // Description : An abstract thread class interface.
34 
35 #ifndef THE_THREAD_INTERFACE_HXX_
36 #define THE_THREAD_INTERFACE_HXX_
37 
38 // system includes:
39 #include <list>
40 
41 // local includes:
42 #include <Core/ITKCommon/ThreadUtils/the_transaction.hxx>
43 
44 // forward declarations:
46 class the_terminators_t;
47 class the_thread_pool_t;
49 
50 
51 //----------------------------------------------------------------
52 // the_thread_interface_t
53 //
54 // 1. the thread will not take ownership of the transactions.
55 // 2. the thread will take ownership of the mutex.
56 //
58 {
59  friend class the_thread_pool_t;
60 
61 protected:
62  // the destructor is protected on purpose,
63  // see delete_this for details:
64  virtual ~the_thread_interface_t();
65 
66 public:
67  // In order to avoid memory management problems with shared libraries,
68  // whoever provides this interface instance (via it's creator), has to
69  // provide a way to delete the instance as well. This will avoid
70  // issues with multiple-instances of C runtime libraries being
71  // used by the app and whatever libraries it links against that
72  // either use or provide this interface:
73  virtual void delete_this() = 0;
74 
75  // the thread will own the mutex passed down to it,
76  // it will delete the mutex when the thread is deleted:
78 
79  //----------------------------------------------------------------
80  // creator_t
81  //
82  typedef the_thread_interface_t *(*creator_t)();
83 
84  // specify a thread creation method:
85  static void set_creator(creator_t creator);
86 
87  // create a new instance of a thread:
88  static the_thread_interface_t * create();
89 
90  inline const unsigned int & id() const
91  { return id_; }
92 
93  // start the thread:
94  virtual void start() = 0;
95 
96  // wait for the thread to finish:
97  virtual void wait() = 0;
98 
99  // put the thread to sleep:
100  virtual void take_a_nap(const unsigned long & microseconds) = 0;
101 
102  // accessor to the transaction terminators:
103  virtual the_terminators_t & terminators() = 0;
104 
105  // give this thread an execution synchronization control:
106  void set_mutex(the_mutex_interface_t * mutex);
107 
108  // this controls whether the thread will voluntarily terminate
109  // once it runs out of transactions:
110  void set_idle_sleep_duration(bool enable, unsigned int microseconds = 10000);
111 
112  // schedule a transaction:
113  inline void add_transaction(the_transaction_t * transaction)
114  { push_back(transaction); }
115 
116  void push_back(the_transaction_t * transaction);
117  void push_front(the_transaction_t * transaction);
118 
119  // add transactions to the list:
120  void push_back(std::list<the_transaction_t *> & schedule);
121 
122  // NOTE: it is the responsibility of the caller to secure a mutex lock
123  // on this thread prior to checking whether the thread has any work left:
124  bool has_work() const;
125 
126  // schedule a transaction and start the thread:
127  void start(the_transaction_t * transaction);
128 
129  // abort the current transaction and clear pending transactions;
130  // transactionFinished will be emitted for the aborted transaction
131  // and the discarded pending transactions:
132  void stop();
133 
134  // accessors to the thread "stopped" flag:
135  inline const bool & stopped() const
136  { return stopped_; }
137 
138  inline void set_stopped(bool stopped)
139  { stopped_ = stopped; }
140 
141  // clear all pending transactions, do not abort the current transaction:
142  void flush();
143 
144  // this will call terminate_all for the terminators in this thread,
145  // but it will not stop the thread, so that new transactions may
146  // be scheduled while the old transactions are being terminated:
147  void terminate_transactions();
148 
149  // terminate the current transactions and schedule a new transaction:
150  void stop_and_go(the_transaction_t * transaction);
151  void stop_and_go(std::list<the_transaction_t *> & schedule);
152 
153  // flush the current transactions and schedule a new transaction:
154  void flush_and_go(the_transaction_t * transaction);
155  void flush_and_go(std::list<the_transaction_t *> & schedule);
156 
157  // execute the scheduled transactions, return true if all
158  // transactions had been executed successfully:
159  virtual bool work();
160 
161  // virtual: default transaction communication handlers:
162  void handle(the_transaction_t * transaction, the_transaction_t::state_t s);
163  void blab(const char * message) const;
164 
165  // transaction callback accessor:
166  inline void
167  set_thread_pool_cb(the_thread_pool_t * pool,
168  the_thread_pool_data_t * cb_data)
169  {
170  thread_pool_ = pool;
171  thread_pool_cb_data_ = cb_data;
172  }
173 
174  // mutex accessor:
175  inline the_mutex_interface_t * mutex()
176  { return mutex_; }
177 
178 protected:
179  // an abstract creator of threads:
180  static creator_t creator_;
181 
182  // FIXME: this may have been a bad idea:
183  unsigned int id_;
184 
185  // thread synchronization control:
186  the_mutex_interface_t * mutex_;
187 
188  // execution control flag:
189  bool stopped_;
190 
191  // these attributes control the thread behavior once all transactions
192  // have been processed. If sleep_when_idle_ flag is set to true, the
193  // thread will put itself to sleep instead of terminating:
194  bool sleep_when_idle_;
195  unsigned int sleep_microsec_;
196 
197  // currently executing transaction:
198  the_transaction_t * active_transaction_;
199 
200  // scheduled transactions:
201  std::list<the_transaction_t *> transactions_;
202 
203  // the transaction callback:
204  the_thread_pool_t * thread_pool_;
205  the_thread_pool_data_t * thread_pool_cb_data_;
206 };
207 
208 
209 #endif // THE_THREAD_INTERFACE_HXX_
Definition: the_thread_pool.hxx:85
Definition: the_transaction.hxx:55
Definition: the_transaction.hxx:181
Definition: the_mutex_interface.hxx:42
Definition: the_thread_pool.hxx:105
Definition: the_terminator.hxx:120
Definition: the_thread_interface.hxx:57