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_terminator.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_terminator.hxx
30 // Author : Pavel A. Koshevoy
31 // Created : Sun Sep 24 18:05:00 MDT 2006
32 // Copyright : (C) 2004-2008 University of Utah
33 // Description : a thread terminator convenience class
34 
35 #ifndef THE_TERMINATOR_HXX_
36 #define THE_TERMINATOR_HXX_
37 
38 #if defined(USE_THE_TERMINATORS) || defined(USE_ITK_TERMINATORS)
39 #define WRAP(x) x
40 #else
41 #define WRAP(x)
42 #endif
43 
44 #include <Core/Utils/Log.h>
45 
46 // system includes:
47 #include <string>
48 #include <list>
49 
50 // local includes:
51 #include <Core/ITKCommon/ThreadUtils/the_thread_storage.hxx>
52 #include <Core/ITKCommon/the_utils.hxx>
53 
54 
55 //----------------------------------------------------------------
56 // the_terminator_t
57 //
59 {
60 public:
61  the_terminator_t(const char * id);
62  virtual ~the_terminator_t();
63 
64  // terminator id accessor:
65  inline const std::string & id() const
66  { return id_; }
67 
68  // read-only accessor to the termination request flag:
69  inline const bool & termination_requested() const
70  { return termination_requested_; }
71 
72  // this function may be called periodically from any time consuming
73  // function -- in case the user has decided to terminate its execution:
74  inline void terminate_on_request() const
75  {
76  // TODO: this code looks sketchy - is it actually needed?
77 
78  // if (termination_requested_ || should_terminate_immediately())
79  if (termination_requested_)
80  {
81  CORE_LOG_MESSAGE("Termination requested in the_terminator_t class");
82  throw_exception();
83  }
84  }
85 
86  // this is a no-op for simple terminators, itk terminators will
87  // override this to turn on the process AbortGenerateData flag:
88  virtual void terminate();
89 
90  // this function will throw an exception:
91  void throw_exception() const;
92 
93  // make sure there are no terminators left:
94  static bool verify_termination();
95 
96 protected:
97  // consult the thread regarding whether termination has been requested
98  // for all terminators in the current thread:
99  static bool should_terminate_immediately();
100 
101  // a list of active terminators per current thread:
102  static std::list<the_terminator_t *> & terminators();
103 
104  // add/remove a terminator to/from the list of active terminators
105  // in the current thread:
106  static void add(the_terminator_t * terminator);
107  static void del(the_terminator_t * terminator);
108 
109  // id of this terminator:
110  std::string id_;
111 
112  // flag indicating that termination was requested explicitly
113  // for this terminator:
114  bool termination_requested_;
115 };
116 
117 //----------------------------------------------------------------
118 // the_terminators_t
119 //
121 {
122 public:
123  virtual ~the_terminators_t();
124 
125  virtual void terminate();
126  virtual bool verify_termination();
127 
128  virtual void add(the_terminator_t * terminator);
129  virtual void del(the_terminator_t * terminator);
130 
131  // concurrent access controls:
132  virtual void lock() = 0;
133  virtual void unlock() = 0;
134 
135 private:
136  // the list of terminators:
137  std::list<the_terminator_t *> terminators_;
138 };
139 
140 
141 #endif // THE_TERMINATOR_HXX_
Definition: the_terminator.hxx:58
Definition: the_terminator.hxx:120