forked from cppalliance/capy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecution_context.cpp
More file actions
148 lines (125 loc) · 2.59 KB
/
execution_context.cpp
File metadata and controls
148 lines (125 loc) · 2.59 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/capy
//
#include <boost/capy/ex/execution_context.hpp>
#include <boost/capy/ex/recycling_memory_resource.hpp>
#include <boost/capy/detail/except.hpp>
namespace boost {
namespace capy {
execution_context::
execution_context()
: frame_alloc_(get_recycling_memory_resource())
{
}
execution_context::
~execution_context()
{
shutdown();
destroy();
}
void
execution_context::
shutdown() noexcept
{
if(shutdown_)
return;
shutdown_ = true;
service* p = head_;
while(p)
{
p->shutdown();
p = p->next_;
}
}
void
execution_context::
destroy() noexcept
{
service* p = head_;
head_ = nullptr;
while(p)
{
service* next = p->next_;
delete p;
p = next;
}
}
execution_context::service*
execution_context::
find_impl(detail::type_index ti) const noexcept
{
auto p = head_;
while(p)
{
if(p->t0_ == ti || p->t1_ == ti)
break;
p = p->next_;
}
return p;
}
execution_context::service&
execution_context::
use_service_impl(factory& f)
{
std::unique_lock<std::mutex> lock(mutex_);
if(auto* p = find_impl(f.t0))
return *p;
lock.unlock();
// Create the service outside lock, enabling nested calls
service* sp = f.create(*this);
sp->t0_ = f.t0;
sp->t1_ = f.t1;
lock.lock();
if(auto* p = find_impl(f.t0))
{
delete sp;
return *p;
}
sp->next_ = head_;
head_ = sp;
return *sp;
}
execution_context::service&
execution_context::
make_service_impl(factory& f)
{
{
std::lock_guard<std::mutex> lock(mutex_);
if(find_impl(f.t0))
detail::throw_invalid_argument();
if(f.t0 != f.t1 && find_impl(f.t1))
detail::throw_invalid_argument();
}
// Unlocked to allow nested service creation from constructor
service* p = f.create(*this);
std::lock_guard<std::mutex> lock(mutex_);
if(find_impl(f.t0))
{
delete p;
detail::throw_invalid_argument();
}
p->t0_ = f.t0;
if(f.t0 != f.t1)
{
if(find_impl(f.t1))
{
delete p;
detail::throw_invalid_argument();
}
p->t1_ = f.t1;
}
else
{
p->t1_ = f.t0;
}
p->next_ = head_;
head_ = p;
return *p;
}
} // namespace capy
} // namespace boost