diff --git a/CMakeLists.txt b/CMakeLists.txt index 498e6ec9..bdc45705 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,37 +55,36 @@ endif() set(LIBCORO_SOURCE_FILES include/coro/concepts/awaitable.hpp + include/coro/concepts/buffer.hpp + include/coro/concepts/executor.hpp include/coro/concepts/promise.hpp + include/coro/concepts/range_of.hpp include/coro/detail/void_value.hpp include/coro/attribute.hpp include/coro/coro.hpp + include/coro/event.hpp src/event.cpp include/coro/generator.hpp + include/coro/latch.hpp + include/coro/mutex.hpp src/mutex.cpp + include/coro/ring_buffer.hpp + include/coro/semaphore.hpp src/semaphore.cpp + include/coro/shared_mutex.hpp + include/coro/sync_wait.hpp src/sync_wait.cpp include/coro/task.hpp + include/coro/task_container.hpp + include/coro/thread_pool.hpp src/thread_pool.cpp include/coro/when_all.hpp ) if(LIBCORO_FEATURE_THREADING) list(APPEND LIBCORO_SOURCE_FILES - include/coro/concepts/buffer.hpp - include/coro/concepts/executor.hpp - include/coro/concepts/range_of.hpp - include/coro/detail/poll_info.hpp - include/coro/event.hpp src/event.cpp include/coro/fd.hpp include/coro/io_scheduler.hpp src/io_scheduler.cpp - include/coro/latch.hpp - include/coro/mutex.hpp src/mutex.cpp include/coro/poll.hpp - include/coro/ring_buffer.hpp - include/coro/semaphore.hpp src/semaphore.cpp - include/coro/shared_mutex.hpp - include/coro/sync_wait.hpp src/sync_wait.cpp - include/coro/task_container.hpp - include/coro/thread_pool.hpp src/thread_pool.cpp ) endif() diff --git a/README.md b/README.md index f9eac606..d6207d2d 100644 --- a/README.md +++ b/README.md @@ -849,10 +849,13 @@ int main() // Connect to the server. co_await client.connect(); + // Make sure the client socket can be written to. + co_await client.poll(coro::poll_op::write); + // Send the request data. client.send(std::string_view{"Hello from client."}); - // Wait for the response an receive it. + // Wait for the response and receive it. co_await client.poll(coro::poll_op::read); std::string response(256, '\0'); auto [recv_status, recv_bytes] = client.recv(response); @@ -914,6 +917,9 @@ int main() request.resize(recv_bytes.size()); std::cout << "server: " << request << "\n"; + // Make sure the client socket can be written to. + co_await client.poll(coro::poll_op::write); + auto response = "Hello from server " + std::to_string(requests); client.send(response); @@ -951,6 +957,9 @@ int main() // Send N requests on the same connection and wait for the server response to each one. for (size_t i = 1; i <= request_count; ++i) { + // Make sure the client socket can be written to. + co_await client.poll(coro::poll_op::write); + // Send the request data. auto request = "Hello from client " + std::to_string(i); client.send(request); diff --git a/include/coro/coro.hpp b/include/coro/coro.hpp index d04b2561..0d75b7ad 100644 --- a/include/coro/coro.hpp +++ b/include/coro/coro.hpp @@ -1,23 +1,14 @@ #pragma once #include "coro/concepts/awaitable.hpp" +#include "coro/concepts/buffer.hpp" +#include "coro/concepts/executor.hpp" #include "coro/concepts/promise.hpp" +#include "coro/concepts/range_of.hpp" #ifdef LIBCORO_FEATURE_THREADING - #include "coro/concepts/buffer.hpp" - #include "coro/concepts/executor.hpp" - #include "coro/concepts/range_of.hpp" - #include "coro/event.hpp" #include "coro/io_scheduler.hpp" - #include "coro/latch.hpp" - #include "coro/mutex.hpp" #include "coro/poll.hpp" - #include "coro/ring_buffer.hpp" - #include "coro/semaphore.hpp" - #include "coro/shared_mutex.hpp" - #include "coro/sync_wait.hpp" - #include "coro/task_container.hpp" - #include "coro/thread_pool.hpp" #endif #ifdef LIBCORO_FEATURE_NETWORKING @@ -36,6 +27,15 @@ #include "coro/net/udp_peer.hpp" #endif +#include "coro/event.hpp" #include "coro/generator.hpp" +#include "coro/latch.hpp" +#include "coro/mutex.hpp" +#include "coro/ring_buffer.hpp" +#include "coro/semaphore.hpp" +#include "coro/shared_mutex.hpp" +#include "coro/sync_wait.hpp" #include "coro/task.hpp" +#include "coro/task_container.hpp" +#include "coro/thread_pool.hpp" #include "coro/when_all.hpp" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index faeb5fe8..d9ecdf5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,10 +2,8 @@ cmake_minimum_required(VERSION 3.0) project(libcoro_test) set(LIBCORO_TEST_SOURCE_FILES - bench.cpp test_event.cpp test_generator.cpp - test_io_scheduler.cpp test_latch.cpp test_mutex.cpp test_ring_buffer.cpp @@ -28,6 +26,13 @@ if(LIBCORO_FEATURE_NETWORKING) ) endif() +if(LIBCORO_FEATURE_THREADING) + list(APPEND LIBCORO_TEST_SOURCE_FILES + bench.cpp + test_io_scheduler.cpp + ) +endif() + add_executable(${PROJECT_NAME} main.cpp ${LIBCORO_TEST_SOURCE_FILES}) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) @@ -45,4 +50,4 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") message(FATAL_ERROR "Clang is currently not supported.") endif() -add_test(NAME libcoro_tests COMMAND ${PROJECT_NAME}) \ No newline at end of file +add_test(NAME libcoro_tests COMMAND ${PROJECT_NAME}) diff --git a/test/test_ring_buffer.cpp b/test/test_ring_buffer.cpp index 27a73b2d..7f50cd02 100644 --- a/test/test_ring_buffer.cpp +++ b/test/test_ring_buffer.cpp @@ -127,9 +127,8 @@ TEST_CASE("ring_buffer producer consumer separate threads", "[ring_buffer]") coro::ring_buffer rb{}; // We'll use an io schedule so we can use yield_for on shutdown since its two threads. - coro::io_scheduler producer_tp{coro::io_scheduler::options{ - .execution_strategy = coro::io_scheduler::execution_strategy_t::process_tasks_inline}}; - coro::thread_pool consumer_tp{coro::thread_pool::options{.thread_count = 1}}; + coro::thread_pool producer_tp{coro::thread_pool::options{.thread_count = 1}}; + coro::thread_pool consumer_tp{coro::thread_pool::options{.thread_count = 1}}; auto make_producer_task = [&]() -> coro::task { @@ -143,7 +142,7 @@ TEST_CASE("ring_buffer producer consumer separate threads", "[ring_buffer]") while (!rb.empty()) { - co_await producer_tp.yield_for(std::chrono::milliseconds{10}); + co_await producer_tp.yield(); } rb.notify_waiters(); // Shut everything down. @@ -178,4 +177,4 @@ TEST_CASE("ring_buffer producer consumer separate threads", "[ring_buffer]") coro::sync_wait(coro::when_all(std::move(tasks))); REQUIRE(rb.empty()); -} \ No newline at end of file +} diff --git a/test/test_semaphore.cpp b/test/test_semaphore.cpp index a391d593..10945b60 100644 --- a/test/test_semaphore.cpp +++ b/test/test_semaphore.cpp @@ -176,7 +176,7 @@ TEST_CASE("semaphore ringbuffer many producers and consumers", "[semaphore]") coro::semaphore s{50, 0}; - coro::io_scheduler tp{}; // let er rip + coro::thread_pool tp{}; // let er rip auto make_consumer_task = [&](uint64_t id) -> coro::task { @@ -210,7 +210,7 @@ TEST_CASE("semaphore ringbuffer many producers and consumers", "[semaphore]") while (value.load(std::memory_order::relaxed) < iterations) { - co_await tp.yield_for(std::chrono::milliseconds{1}); + co_await tp.yield(); } std::cerr << "producer " << id << " exiting\n"; diff --git a/test/test_shared_mutex.cpp b/test/test_shared_mutex.cpp index c1063509..489578bf 100644 --- a/test/test_shared_mutex.cpp +++ b/test/test_shared_mutex.cpp @@ -80,6 +80,7 @@ TEST_CASE("mutex single waiter not locked shared", "[shared_mutex]") m.unlock(); } +#ifdef LIBCORO_FEATURE_THREADING TEST_CASE("mutex many shared and exclusive waiters interleaved", "[shared_mutex]") { auto tp = std::make_shared( @@ -163,3 +164,4 @@ TEST_CASE("mutex many shared and exclusive waiters interleaved", "[shared_mutex] coro::sync_wait(coro::when_all(make_shared_tasks_task(), make_exclusive_task())); } +#endif