Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73f75a3

Browse files
authored
Merge branch 'master' into disruptor
2 parents a44c689 + 59f2e77 commit 73f75a3

8 files changed

+66
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ namespace cppcoro::net
16081608
cancellation_token ct) noexcept;
16091609

16101610
[[nodiscard]]
1611-
Awaitable<void> disconnect() noexcep0t;
1611+
Awaitable<void> disconnect() noexcept;
16121612
[[nodiscard]]
16131613
Awaitable<void> disconnect(cancellation_token ct) noexcept;
16141614

include/cppcoro/generator.hpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ namespace cppcoro
3434
constexpr std::experimental::suspend_always final_suspend() const { return {}; }
3535

3636
template<
37-
typename U,
38-
typename = std::enable_if_t<std::is_same<U, T>::value>>
39-
std::experimental::suspend_always yield_value(U& value) noexcept
37+
typename U = T,
38+
std::enable_if_t<!std::is_rvalue_reference<U>::value, int> = 0>
39+
std::experimental::suspend_always yield_value(std::remove_reference_t<T>& value) noexcept
4040
{
4141
m_value = std::addressof(value);
4242
return {};
4343
}
4444

45-
std::experimental::suspend_always yield_value(T&& value) noexcept
45+
std::experimental::suspend_always yield_value(std::remove_reference_t<T>&& value) noexcept
4646
{
4747
m_value = std::addressof(value);
4848
return {};
@@ -61,7 +61,7 @@ namespace cppcoro
6161

6262
reference_type value() const noexcept
6363
{
64-
return *m_value;
64+
return static_cast<reference_type>(*m_value);
6565
}
6666

6767
// Don't allow any use of 'co_await' inside the generator coroutine.
@@ -93,10 +93,15 @@ namespace cppcoro
9393
using iterator_category = std::input_iterator_tag;
9494
// What type should we use for counting elements of a potentially infinite sequence?
9595
using difference_type = std::size_t;
96-
using value_type = std::remove_reference_t<T>;
97-
using reference = value_type&;
98-
using pointer = value_type*;
96+
using value_type = typename generator_promise<T>::value_type;
97+
using reference = typename generator_promise<T>::reference_type;
98+
using pointer = typename generator_promise<T>::pointer_type;
9999

100+
// Iterator needs to be default-constructible to satisfy the Range concept.
101+
generator_iterator() noexcept
102+
: m_coroutine(nullptr)
103+
{}
104+
100105
explicit generator_iterator(std::nullptr_t) noexcept
101106
: m_coroutine(nullptr)
102107
{}
@@ -126,11 +131,11 @@ namespace cppcoro
126131
return *this;
127132
}
128133

129-
// Don't support post-increment as that would require taking a
130-
// copy of the old value into the returned iterator as there
131-
// are no guarantees it's still going to be valid after the
132-
// increment is executed.
133-
generator_iterator operator++(int) = delete;
134+
// Need to provide post-increment operator to implement the 'Range' concept.
135+
void operator++(int)
136+
{
137+
(void)operator++();
138+
}
134139

135140
reference operator*() const noexcept
136141
{

lib/lightweight_manual_reset_event.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,8 @@ cppcoro::detail::lightweight_manual_reset_event::~lightweight_manual_reset_event
215215

216216
void cppcoro::detail::lightweight_manual_reset_event::set() noexcept
217217
{
218-
{
219-
std::lock_guard<std::mutex> lock(m_mutex);
220-
m_isSet = true;
221-
}
218+
std::lock_guard<std::mutex> lock(m_mutex);
219+
m_isSet = true;
222220
m_cv.notify_all();
223221
}
224222

lib/socket_helpers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cppcoro::net::detail::sockaddr_to_ip_endpoint(const sockaddr& address) noexcept
3030

3131
return ipv4_endpoint{
3232
ipv4_address{ addressBytes },
33-
ipv4Address.sin_port
33+
ntohs(ipv4Address.sin_port)
3434
};
3535
}
3636
else
@@ -42,7 +42,7 @@ cppcoro::net::detail::sockaddr_to_ip_endpoint(const sockaddr& address) noexcept
4242

4343
return ipv6_endpoint{
4444
ipv6_address{ ipv6Address.sin6_addr.u.Byte },
45-
ipv6Address.sin6_port
45+
ntohs(ipv6Address.sin6_port)
4646
};
4747
}
4848
}
@@ -58,7 +58,7 @@ int cppcoro::net::detail::ip_endpoint_to_sockaddr(
5858
SOCKADDR_IN ipv4Address;
5959
ipv4Address.sin_family = AF_INET;
6060
std::memcpy(&ipv4Address.sin_addr, ipv4EndPoint.address().bytes(), 4);
61-
ipv4Address.sin_port = ipv4EndPoint.port();
61+
ipv4Address.sin_port = htons(ipv4EndPoint.port());
6262
std::memset(&ipv4Address.sin_zero, 0, sizeof(ipv4Address.sin_zero));
6363

6464
std::memcpy(&address.get(), &ipv4Address, sizeof(ipv4Address));
@@ -72,7 +72,7 @@ int cppcoro::net::detail::ip_endpoint_to_sockaddr(
7272
SOCKADDR_IN6 ipv6Address;
7373
ipv6Address.sin6_family = AF_INET6;
7474
std::memcpy(&ipv6Address.sin6_addr, ipv6EndPoint.address().bytes(), 16);
75-
ipv6Address.sin6_port = ipv6EndPoint.port();
75+
ipv6Address.sin6_port = htons(ipv6EndPoint.port());
7676
ipv6Address.sin6_flowinfo = 0;
7777
ipv6Address.sin6_scope_struct = SCOPEID_UNSPECIFIED_INIT;
7878

test/async_auto_reset_event_tests.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
#include <cppcoro/when_all.hpp>
1212
#include <cppcoro/when_all_ready.hpp>
1313
#include <cppcoro/on_scope_exit.hpp>
14-
15-
#if CPPCORO_OS_WINNT
16-
# include <cppcoro/io_service.hpp>
17-
# include "io_service_fixture.hpp"
18-
#endif
14+
#include <cppcoro/static_thread_pool.hpp>
1915

2016
#include <thread>
2117
#include <cassert>
@@ -91,10 +87,10 @@ TEST_CASE("multiple waiters")
9187
check()));
9288
}
9389

94-
#if CPPCORO_OS_WINNT
95-
96-
TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
90+
TEST_CASE("multi-threaded")
9791
{
92+
cppcoro::static_thread_pool tp{ 3 };
93+
9894
auto run = [&]() -> cppcoro::task<>
9995
{
10096
cppcoro::async_auto_reset_event event;
@@ -103,15 +99,15 @@ TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
10399

104100
auto startWaiter = [&]() -> cppcoro::task<>
105101
{
106-
co_await io_service().schedule();
102+
co_await tp.schedule();
107103
co_await event;
108104
++value;
109105
event.set();
110106
};
111107

112108
auto startSignaller = [&]() -> cppcoro::task<>
113109
{
114-
co_await io_service().schedule();
110+
co_await tp.schedule();
115111
value = 5;
116112
event.set();
117113
};
@@ -141,6 +137,4 @@ TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
141137
cppcoro::sync_wait(cppcoro::when_all(std::move(tasks)));
142138
}
143139

144-
#endif
145-
146140
TEST_SUITE_END();

test/generator_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ TEST_CASE("generator of reference returns by reference")
6060
CHECK(value == 2.0f);
6161
}
6262

63+
TEST_CASE("generator of const type")
64+
{
65+
auto fib = []() -> generator<const std::uint64_t>
66+
{
67+
std::uint64_t a = 0, b = 1;
68+
while (true)
69+
{
70+
co_yield b;
71+
b += std::exchange(a, b);
72+
}
73+
};
74+
75+
std::uint64_t count = 0;
76+
for (auto i : fib())
77+
{
78+
if (i > 1'000'000) {
79+
break;
80+
}
81+
++count;
82+
}
83+
84+
// 30th fib number is 832'040
85+
CHECK(count == 30);
86+
}
87+
6388
TEST_CASE("generator doesn't start until its called")
6489
{
6590
bool reachedA = false;

test/single_consumer_async_auto_reset_event_tests.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
#include <cppcoro/when_all.hpp>
1212
#include <cppcoro/when_all_ready.hpp>
1313
#include <cppcoro/on_scope_exit.hpp>
14-
15-
#if CPPCORO_OS_WINNT
16-
# include <cppcoro/io_service.hpp>
17-
# include "io_service_fixture.hpp"
18-
#endif
14+
#include <cppcoro/static_thread_pool.hpp>
1915

2016
#include <thread>
2117
#include <cassert>
@@ -53,10 +49,10 @@ TEST_CASE("single waiter")
5349
cppcoro::sync_wait(cppcoro::when_all_ready(run(), check()));
5450
}
5551

56-
#if CPPCORO_OS_WINNT
57-
58-
TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
52+
TEST_CASE("multi-threaded")
5953
{
54+
cppcoro::static_thread_pool tp;
55+
6056
cppcoro::sync_wait([&]() -> cppcoro::task<>
6157
{
6258
cppcoro::single_consumer_async_auto_reset_event valueChangedEvent;
@@ -75,7 +71,7 @@ TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
7571

7672
auto modifier = [&](int count) -> cppcoro::task<int>
7773
{
78-
co_await io_service().schedule();
74+
co_await tp.schedule();
7975
for (int i = 0; i < count; ++i)
8076
{
8177
value.fetch_add(1, std::memory_order_relaxed);
@@ -94,6 +90,4 @@ TEST_CASE_FIXTURE(io_service_fixture_with_threads<3>, "multi-threaded")
9490
}());
9591
}
9692

97-
#endif
98-
9993
TEST_SUITE_END();

test/sync_wait_tests.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
#include <cppcoro/task.hpp>
1010
#include <cppcoro/shared_task.hpp>
1111
#include <cppcoro/on_scope_exit.hpp>
12-
13-
#if CPPCORO_OS_WINNT
14-
# include <cppcoro/io_service.hpp>
15-
# include "io_service_fixture.hpp"
16-
#endif
12+
#include <cppcoro/static_thread_pool.hpp>
1713

1814
#include <string>
1915
#include <type_traits>
@@ -55,20 +51,19 @@ TEST_CASE("sync_wait(shared_task<T>)")
5551
CHECK(cppcoro::sync_wait(makeTask()) == "foo");
5652
}
5753

58-
#if CPPCORO_OS_WINNT
59-
60-
TEST_CASE_FIXTURE(io_service_fixture_with_threads<1>, "multiple threads")
54+
TEST_CASE("multiple threads")
6155
{
6256
// We are creating a new task and starting it inside the sync_wait().
63-
// The task will reschedule itself for resumption on an I/O thread
57+
// The task will reschedule itself for resumption on a thread-pool thread
6458
// which will sometimes complete before this thread calls event.wait()
6559
// inside sync_wait(). Thus we're roughly testing the thread-safety of
6660
// sync_wait().
61+
cppcoro::static_thread_pool tp{ 1 };
6762

6863
int value = 0;
6964
auto createLazyTask = [&]() -> cppcoro::task<int>
7065
{
71-
co_await io_service().schedule();
66+
co_await tp.schedule();
7267
co_return value++;
7368
};
7469

@@ -78,6 +73,4 @@ TEST_CASE_FIXTURE(io_service_fixture_with_threads<1>, "multiple threads")
7873
}
7974
}
8075

81-
#endif
82-
8376
TEST_SUITE_END();

0 commit comments

Comments
 (0)