Scarab  v2.4.7
Project 8 C++ Utility Library
cancelable.hh
Go to the documentation of this file.
1 /*
2  * cancelable.hh
3  *
4  * Created on: Jan 24, 2016
5  * Author: nsoblath
6  *
7  * Base class for thread-safe asynchronous cancellation
8  */
9 
10 #ifndef SCARAB_CANCELABLE_HH_
11 #define SCARAB_CANCELABLE_HH_
12 
13 #include "scarab_api.hh"
14 
15 #include <atomic>
16 
17 namespace scarab
18 {
20  {
21  public:
22  cancelable();
23  virtual ~cancelable();
24 
26  void cancel();
27 
29  void reset_cancel();
30 
32  bool is_canceled() const;
33 
34  private:
35  virtual void do_cancellation();
36  virtual void do_reset_cancellation();
37 
38  protected:
39  std::atomic< bool > f_canceled;
40  };
41 
42  inline void cancelable::cancel()
43  {
44  if( f_canceled.load() ) return;
45  f_canceled.store( true );
46  this->do_cancellation();
47  }
48 
50  {
51  if( ! f_canceled.load() ) return;
52  f_canceled.store( false );
53  this->do_reset_cancellation();
54  }
55 
56  inline bool cancelable::is_canceled() const
57  {
58  return f_canceled.load();
59  }
60 
61 } /* namespace scarab */
62 
63 #endif /* SCARAB_CANCELABLE_HH_ */
#define SCARAB_API
Definition: scarab_api.hh:24
bool is_canceled() const
check canceled state
Definition: cancelable.hh:56
void reset_cancel()
reset to non-canceled state
Definition: cancelable.hh:49
std::atomic< bool > f_canceled
Definition: cancelable.hh:39
void cancel()
asynchronous cancel function
Definition: cancelable.hh:42