forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancellation_registration.cpp
More file actions
41 lines (36 loc) · 889 Bytes
/
Copy pathcancellation_registration.cpp
File metadata and controls
41 lines (36 loc) · 889 Bytes
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/cancellation_registration.hpp>
#include "cancellation_state.hpp"
#include <cassert>
cppcoro::cancellation_registration::~cancellation_registration()
{
if (m_state != nullptr)
{
m_state->deregister_callback(this);
m_state->release_token_ref();
}
}
void cppcoro::cancellation_registration::register_callback(cancellation_token&& token)
{
auto* state = token.m_state;
if (state != nullptr && state->can_be_cancelled())
{
m_state = state;
if (state->try_register_callback(this))
{
token.m_state = nullptr;
}
else
{
m_state = nullptr;
m_callback();
}
}
else
{
m_state = nullptr;
}
}