Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cancellable.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4619b4d147ece41a84f17ebf7c2016bbc949681f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once

#include <atomic>
#include <chrono>
#include <mutex>
#include <optional>
#include <string>

namespace base
{
// This is a helper thread-safe class which can be mixed in
// classes which represent some cancellable activities.
class Cancellable
{
public:
  enum class Status
  {
    Active,
    CancelCalled,
    DeadlineExceeded,
  };

  Cancellable() = default;

  virtual ~Cancellable() {}

  // Marks current activity as not cancelled.
  // Resets the deadline if it was present.
  virtual void Reset();

  // Marks current activity as cancelled.
  virtual void Cancel();

  // Sets a deadline after which the activity is cancelled.
  virtual void SetDeadline(std::chrono::steady_clock::time_point const & t);

  // Updates the status.
  // Returns true iff current activity has been cancelled (either by the call
  // to Cancel or by timeout).
  virtual bool IsCancelled() const;

  // Updates the status of current activity and returns its value.
  virtual Status CancellationStatus() const;

private:
  // Checks whether |m_deadline| has exceeded. Must be called with |m_mutex| locked.
  void CheckDeadline() const;

  mutable std::mutex m_mutex;

  mutable Status m_status = Status::Active;

  std::optional<std::chrono::steady_clock::time_point> m_deadline;
};

std::string DebugPrint(Cancellable::Status status);
}  // namespace base