Remove io_context and just use io_service& directly. · simoncpp/cppcoro@a345276 · GitHub
Skip to content

Commit a345276

Browse files
committed
Remove io_context and just use io_service& directly.
Added io_work_scope RAII class to help with scoped lifetime of io_service event loop. Having io_context always increment the ref-count of active I/O operations whenever passed by-value to a function seems like unnecessary atomic operations when compared to selected usages of io_work_scope which generally only need creation for top-level operations.
1 parent 0a46465 commit a345276

12 files changed

Lines changed: 39 additions & 63 deletions

include/cppcoro/file.hpp

Lines changed: 2 additions & 2 deletions

include/cppcoro/io_service.hpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
namespace cppcoro
2020
{
21-
class io_context;
22-
2321
class io_service
2422
{
2523
public:
@@ -93,8 +91,6 @@ namespace cppcoro
9391

9492
void notify_work_finished() noexcept;
9593

96-
io_context get_context() noexcept;
97-
9894
#if CPPCORO_OS_WINNT
9995
detail::win32::handle_t native_iocp_handle() noexcept;
10096
#endif
@@ -149,17 +145,17 @@ namespace cppcoro
149145
std::experimental::coroutine_handle<> m_awaiter;
150146
};
151147

152-
class io_context
148+
class io_work_scope
153149
{
154150
public:
155151

156-
io_context(io_service& service) noexcept
152+
io_work_scope(io_service& service) noexcept
157153
: m_service(&service)
158154
{
159155
service.notify_work_started();
160156
}
161157

162-
io_context(const io_context& other) noexcept
158+
io_work_scope(const io_work_scope& other) noexcept
163159
: m_service(other.m_service)
164160
{
165161
if (m_service != nullptr)
@@ -168,51 +164,43 @@ namespace cppcoro
168164
}
169165
}
170166

171-
io_context(io_context&& other) noexcept
167+
io_work_scope(io_work_scope&& other) noexcept
172168
: m_service(other.m_service)
173169
{
174170
other.m_service = nullptr;
175171
}
176172

177-
~io_context()
173+
~io_work_scope()
178174
{
179175
if (m_service != nullptr)
180176
{
181177
m_service->notify_work_finished();
182178
}
183179
}
184180

185-
[[nodiscard]]
186-
io_service::schedule_operation schedule() noexcept
187-
{
188-
return m_service->schedule();
189-
}
190-
191-
void swap(io_context& other) noexcept
181+
void swap(io_work_scope& other) noexcept
192182
{
193183
std::swap(m_service, other.m_service);
194184
}
195185

196-
io_context& operator=(io_context other) noexcept
186+
io_work_scope& operator=(io_work_scope other) noexcept
197187
{
198188
swap(other);
199189
return *this;
200190
}
201191

202-
#if CPPCORO_OS_WINNT
203-
detail::win32::handle_t native_iocp_handle() noexcept
192+
io_service& service() noexcept
204193
{
205-
return m_service->native_iocp_handle();
194+
return *m_service;
206195
}
207-
#endif
208196

209197
private:
210198

211199
io_service* m_service;
212200

213201
};
214202

215-
inline void swap(io_context& a, io_context& b)
203+
inline void swap(io_work_scope& a, io_work_scope& b)
216204
{
217205
a.swap(b);
218206
}

include/cppcoro/read_only_file.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace cppcoro
1515
{
16-
class io_context;
17-
1816
class read_only_file : public readable_file
1917
{
2018
public:
@@ -44,7 +42,7 @@ namespace cppcoro
4442
/// If the file could not be opened for read.
4543
[[nodiscard]]
4644
static read_only_file open(
47-
io_context& ioContext,
45+
io_service& ioService,
4846
const std::experimental::filesystem::path& path,
4947
file_share_mode shareMode = file_share_mode::read,
5048
file_buffering_mode bufferingMode = file_buffering_mode::default_);

include/cppcoro/read_write_file.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
namespace cppcoro
1717
{
18-
class io_context;
19-
2018
class read_write_file : public readable_file, public writable_file
2119
{
2220
public:
@@ -50,7 +48,7 @@ namespace cppcoro
5048
/// If the file could not be opened for write.
5149
[[nodiscard]]
5250
static read_write_file open(
53-
io_context& ioContext,
51+
io_service& ioService,
5452
const std::experimental::filesystem::path& path,
5553
file_open_mode openMode = file_open_mode::create_or_open,
5654
file_share_mode shareMode = file_share_mode::none,

include/cppcoro/write_only_file.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
namespace cppcoro
1616
{
17-
class io_context;
18-
1917
class write_only_file : public writable_file
2018
{
2119
public:
@@ -49,7 +47,7 @@ namespace cppcoro
4947
/// If the file could not be opened for write.
5048
[[nodiscard]]
5149
static write_only_file open(
52-
io_context& ioContext,
50+
io_service& ioService,
5351
const std::experimental::filesystem::path& path,
5452
file_open_mode openMode = file_open_mode::create_or_open,
5553
file_share_mode shareMode = file_share_mode::none,

lib/file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cppcoro::file::file(detail::win32::safe_handle&& fileHandle) noexcept
4444

4545
cppcoro::detail::win32::safe_handle cppcoro::file::open(
4646
detail::win32::dword_t fileAccess,
47-
io_context& ioContext,
47+
io_service& ioService,
4848
const std::experimental::filesystem::path& path,
4949
file_open_mode openMode,
5050
file_share_mode shareMode,
@@ -130,7 +130,7 @@ cppcoro::detail::win32::safe_handle cppcoro::file::open(
130130
// Associate with the I/O service's completion port.
131131
const HANDLE result = ::CreateIoCompletionPort(
132132
fileHandle.handle(),
133-
ioContext.native_iocp_handle(),
133+
ioService.native_iocp_handle(),
134134
0,
135135
0);
136136
if (result == nullptr)

lib/io_service.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ void cppcoro::io_service::notify_work_finished() noexcept
158158
}
159159
}
160160

161-
cppcoro::io_context cppcoro::io_service::get_context() noexcept
162-
{
163-
return io_context{ *this };
164-
}
165-
166161
cppcoro::detail::win32::handle_t cppcoro::io_service::native_iocp_handle() noexcept
167162
{
168163
return m_iocpHandle.handle();

lib/read_only_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
# include <Windows.h>
1111

1212
cppcoro::read_only_file cppcoro::read_only_file::open(
13-
io_context& ioContext,
13+
io_service& ioService,
1414
const std::experimental::filesystem::path& path,
1515
file_share_mode shareMode,
1616
file_buffering_mode bufferingMode)
1717
{
1818
return read_only_file(file::open(
1919
GENERIC_READ,
20-
ioContext,
20+
ioService,
2121
path,
2222
file_open_mode::open_existing,
2323
shareMode,

lib/read_write_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
# include <Windows.h>
1111

1212
cppcoro::read_write_file cppcoro::read_write_file::open(
13-
io_context& ioContext,
13+
io_service& ioService,
1414
const std::experimental::filesystem::path& path,
1515
file_open_mode openMode,
1616
file_share_mode shareMode,
1717
file_buffering_mode bufferingMode)
1818
{
1919
return read_write_file(file::open(
2020
GENERIC_READ | GENERIC_WRITE,
21-
ioContext,
21+
ioService,
2222
path,
2323
openMode,
2424
shareMode,

lib/write_only_file.cpp

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)