opennurbs_terminator.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2013 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(OPENNURBS_TERMINATOR_INC_)
18 #define OPENNURBS_TERMINATOR_INC_
19 
20 class ON_CLASS ON_Terminator
21 {
22 public:
23  ON_Terminator();
24  ~ON_Terminator();
25 
26  /*
27  Description:
28  Set the function that is called when a calculation wants
29  to determine if the calculation should stop or continue.
30  Parameters:
31  callback_function - [in]
32  * The function that is called when a calculation wants
33  to determine if the calculation should stop or continue.
34  * If this callback function returns true, the calculation will
35  terminate as soon as possible.
36  * If this callback function returns false, the calculation will
37  continue.
38  * The calculation thread calls in this callback function.
39  * The callback function should do something that is fast and simple.
40  Parameters passed to the callback function:
41  context - [in]
42  the value of callback_context.
43  Example:
44  bool bStopCalculation = false;
45 
46  bool StopCalculation( ON__UINT_PTR context )
47  {
48  if ( 0 != context )
49  {
50  const bool* p = (const bool*)context;
51  if ( *p )
52  return true; // terminate calculation as soon as possible
53  }
54  return false; // continue calculation
55  }
56 
57  ...
58 
59  ON_Terminator terminator;
60  terminator.SetTerminationQueryCallbackFunction(StopCalculation,(ON__UINT_PTR)(&bStopCalculation));
61  ...
62  Pass &terminator to a calculation and then set bStopCalculation = true to terminate it.
63  The calculation will generally be running in another thread, but can be in the same
64  thread if it is structured to pump windows messages or something similar.
65  */
66  void SetTerminationQueryCallbackFunction(
67  bool (*callback_function)(ON__UINT_PTR context),
68  ON__UINT_PTR callback_context
69  );
70 
71  ON__UINT_PTR CallbackFunction() const;
72  ON__UINT_PTR CallbackContext() const;
73 
74  void SetThreadId(
75  ON__UINT_PTR thread_id
76  );
77 
78  ON__UINT_PTR ThreadId() const;
79 
80  /*
81  Description:
82  Sets m_bTerminationRequested = true.
83  All future calls to TerminationRequested(this) will return true.
84  */
85  void RequestTermination();
86 
87  /*
88  Description:
89  If terminator is not nullptr, sets terminator->m_bTerminationRequested = true.
90  If terminator is not nullptr, all future calls to TerminationRequested(terminator)
91  will return true.
92  Parameters:
93  terminator - [in] (can be nullptr)
94  Remarks:
95  This is convenience function and is identical to the code
96 
97  if (nullptr != terminator)
98  terminator->RequestTermination();
99  */
100  static void RequestTermination(
101  ON_Terminator* terminator
102  );
103 
104  /*
105  Description:
106  A calculation calls ON_Terminator::TerminationRequested(terminator)
107  to determine if it should continue or quit.
108  Parameters:
109  terminator - [in]
110  A pointer to an ON_Terminator or null pointer.
111  Returns:
112  True if the calculation should terminate as soon as possible.
113  False if the calculation should continue.
114  Example:
115  void MyLongCalculation( ..., ON_Terminator* terminator, ...)
116  {
117  for ( i = 0; i < count; i++ )
118  {
119  if ( ON_Terminator::TerminationRequested(terminator) )
120  break;
121  ...
122  }
123  }
124  */
125  static bool TerminationRequested( ON_Terminator* terminator);
126 
127  /*
128  Description:
129  An expert user function to determine if a calculation should continue.
130  Parameters:
131  terminator - [in]
132  A pointer to an ON_Terminator or null pointer.
133  callback_delta - [in]
134  Minimum amount of time to delay between calls to the registered callback function
135  in clock() time units.
136  0 will omit making any call to the registered function.
137  ON_Terminator::TerminationRequested( terminator ) uses a value of
138  callback_delta = CLOCKS_PER_SEC/10 meaning a maximum
139  of 10 callbacks per second.
140  Returns:
141  True if the calculation should terminate as soon as possible.
142  False if the calculation should continue.
143  Remarks:
144  In general, call the ON_Terminator::TerminationRequested( terminator ).
145  */
146  static bool TerminationRequestedExpert(
147  ON_Terminator* terminator,
148  ON__UINT64 callback_delta
149  );
150 
151 private:
152  bool (*m_callback_function)(ON__UINT_PTR) = nullptr;
153  ON__UINT_PTR m_callback_context = 0;
154  ON__UINT64 m_previous_query_clock = 0;
155  bool m_bTerminationRequested = false;
156  bool m_reserved1[7];
157  ON__UINT_PTR m_thread_id = 0;
158  ON__UINT64 m_reserved2[2];
159 };
160 
161 
162 #endif
163 
Definition: opennurbs_terminator.h:20