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_transaction.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_transaction.hxx
30 // Author : Pavel A. Koshevoy
31 // Created : Fri Feb 16 09:52:00 MST 2007
32 // Copyright : (C) 2004-2008 University of Utah
33 // Description : A thread transaction class.
34 
35 #ifndef THE_TRANSACTION_HXX_
36 #define THE_TRANSACTION_HXX_
37 
38 // system includes:
39 #include <iostream>
40 #include <string>
41 
42 // local includes:
43 #include <Core/ITKCommon/ThreadUtils/the_mutex_interface.hxx>
44 
45 // forward declarations:
48 
49 
50 //----------------------------------------------------------------
51 // the_transaction_t
52 //
53 // NOTE: the transaction will not take ownership of the mutex.
54 //
56 {
57 public:
59  virtual ~the_transaction_t();
60 
61  // execute the transaction:
62  virtual void execute(the_thread_interface_t * thread) = 0;
63 
64  // transaction execution state:
65  typedef enum {
66  PENDING_E,
67  SKIPPED_E,
68  STARTED_E,
69  ABORTED_E,
70  DONE_E
71  } state_t;
72 
73  inline state_t state() const
74  { return state_; }
75 
76  inline void set_state(const state_t & s)
77  { state_ = s; }
78 
79  inline bool done() const
80  { return state_ == DONE_E; }
81 
82  //----------------------------------------------------------------
83  // notify_cb_t
84  //
85  typedef void(*notify_cb_t)(void *, the_transaction_t *, state_t s);
86 
87  inline notify_cb_t notify_cb() const
88  { return notify_cb_; }
89 
90  inline void set_notify_cb(notify_cb_t cb, void * cb_data)
91  {
92  notify_cb_ = cb;
93  notify_cb_data_ = cb_data;
94  }
95 
96  //----------------------------------------------------------------
97  // status_cb_t
98  //
99  // NOTE: if status is NULL, it means the transaction is requesting
100  // a callback from the main thread. This could be used by the
101  // transaction for some GUI user interaction (such as finding replacement
102  // tiles for a mosaic, etc...)
103  //
104  typedef void(*status_cb_t)(void *, the_transaction_t *, const char * status);
105 
106  inline status_cb_t status_cb() const
107  { return status_cb_; }
108 
109  inline void set_status_cb(status_cb_t cb, void * cb_data)
110  {
111  status_cb_ = cb;
112  status_cb_data_ = cb_data;
113  }
114 
115  // notify the transaction about a change in it's state:
116  virtual void notify(the_transaction_handler_t * handler,
117  state_t s,
118  const char * message = NULL);
119 
120  // helper:
121  virtual void blab(the_transaction_handler_t * handler,
122  const char * message);
123 
124  // FIXME: this is a relic:
125 // inline static const the_text_t tr(const char * text)
126 // { return the_text_t(text); }
127 //
128 // inline static const the_text_t & tr(const the_text_t & text)
129 // { return text; }
130 
131  // if a transaction needs to have something executed in the main thread
132  // context it should call callback_request. This requires that a valid
133  // status callback is set for this transaction, and that the status
134  // callback will acknowledge the main thread callback request. This
135  // call will block until the callback is executed in the main thread,
136  // and the request is removed:
137  bool callback_request();
138 
139  // if a transaction needs to have something executed in the main thread
140  // context it should override this callback. This method will be executed
141  // in the main thread context. The default implementation will simply
142  // remove the request:
143  virtual void callback();
144 
145 protected:
146  // when requesting a callback from the main thread
147  // the status of request will be set to WAITING_E:
148  typedef enum {
149  NOTHING_E,
150  WAITING_E
151  } request_t;
152 
153  // synchronization control:
154  the_mutex_interface_t * mutex_;
155 
156  // status of callback request:
157  request_t request_;
158 
159  // current state of the transaction:
160  state_t state_;
161 
162 public:
163  // the callbacks:
164  notify_cb_t notify_cb_;
165  void * notify_cb_data_;
166 
167  status_cb_t status_cb_;
168  void * status_cb_data_;
169 };
170 
171 //----------------------------------------------------------------
172 // operator <<
173 //
174 extern std::ostream &
175 operator << (std::ostream & so, const the_transaction_t::state_t & state);
176 
177 
178 //----------------------------------------------------------------
179 // the_transaction_handler_t
180 //
182 {
183 public:
184  virtual ~the_transaction_handler_t() {}
185 
186  virtual void handle(the_transaction_t * transaction,
187  the_transaction_t::state_t s) = 0;
188  virtual void blab(const char * message) const = 0;
189 };
190 
191 
192 #endif // THE_TRANSACTION_HXX_
Definition: the_transaction.hxx:55
Definition: the_transaction.hxx:181
Definition: the_mutex_interface.hxx:42
Definition: the_thread_interface.hxx:57