Skip to content

Commit

Permalink
try fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed May 9, 2024
1 parent 1fddd97 commit 80d68f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ jobs:

- name: Build
run: |
CXX=${{ matrix.config.cxx }} meson setup build -Dbuildtype=${{ matrix.config.build_type }} -Db_sanitize=address
CXX=${{ matrix.config.cxx }} meson setup build -Dbuildtype=${{ matrix.config.build_type }}
meson compile -C build
# ./build/tests/gtests/all_gtests
./build/tests/test_latch
./build/tests/test_coredump
- name: Install
Expand Down
10 changes: 5 additions & 5 deletions tests/test_coredump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace uvio::net;

auto main() -> int {

auto server = [](Latch &start, Latch &finish) -> Task<> {
auto server = []() -> Task<> {
std::array<char, 64> buf{};

auto listener = TcpListener();
Expand All @@ -24,7 +24,7 @@ auto main() -> int {
// finish.count_down();
};

auto client = [](Latch &start, Latch &finish) -> Task<> {
auto client = []() -> Task<> {
std::array<char, 64> buf{};

// co_await start.arrive_and_wait();
Expand All @@ -40,9 +40,9 @@ auto main() -> int {
auto test = [&]() -> Task<> {
Latch start(3);
Latch finish(3);
spawn(server(start, finish));
spawn(client(start, finish));
co_await start.arrive_and_wait();
spawn(server());
spawn(client());
// co_await start.arrive_and_wait();
// co_await finish.arrive_and_wait();
co_return;
};
Expand Down
6 changes: 3 additions & 3 deletions tests/test_latch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ auto latch_test(Latch &latch, std::atomic<std::size_t> &sum) -> Task<> {
co_await latch.arrive_and_wait();
}

auto test(std::size_t n) -> Task<> {
auto test(std::ptrdiff_t n) -> Task<> {
assert(n >= 1);
Latch latch{static_cast<std::ptrdiff_t>(n)};
Latch latch{n};

std::atomic<std::size_t> sum = 0;
assert(latch.try_wait() == false);
Expand All @@ -23,7 +23,7 @@ auto test(std::size_t n) -> Task<> {
sum.fetch_add(1, std::memory_order::relaxed);
co_await latch.arrive_and_wait();
console.info("expected: {}, actual {}", n, sum.load());
assert(n == sum);
assert(n - sum == 0);
}

auto main() -> int {
Expand Down
8 changes: 5 additions & 3 deletions uvio/sync/latch.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// origin: https://github.com/8sileus/zedio/blob/main/zedio/sync/latch.hpp
// https://github.com/LEAVING-7/Coco/blob/main/include/coco/sync/latch.hpp
#pragma once

#include "uvio/macros.hpp"
Expand All @@ -12,7 +13,7 @@ class Latch {
struct LatchAwaiter {
std::coroutine_handle<> handle_;
Latch *latch_;
LatchAwaiter *next_{};
LatchAwaiter *next_;

LatchAwaiter(Latch *latch)
: latch_{latch} {}
Expand All @@ -30,7 +31,8 @@ class Latch {
std::memory_order::acq_rel,
std::memory_order::relaxed)) {
}
return !latch_->try_wait();
// return !latch_->try_wait();
return true;
}

auto await_resume() const noexcept {}
Expand All @@ -48,7 +50,7 @@ class Latch {
auto operator=(Latch &&) -> Latch & = delete;

auto count_down(std::ptrdiff_t update = 1) -> void {
auto odd = expected_.fetch_sub(update, std::memory_order::release);
auto odd = expected_.fetch_sub(update, std::memory_order::acquire);
if (odd == update) {
notify_all();
}
Expand Down

0 comments on commit 80d68f8

Please sign in to comment.