Skip to content

Commit b13d4f7

Browse files
committed
test: refine code
1 parent 1024a01 commit b13d4f7

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

include/coro/condition_variable.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace coro {
1818
// - std::mutex: Thread-safe for multithreaded use (default)
1919
// - dummy_mutex: No lock overhead for single-threaded use
2020
template <typename MUTEX = std::mutex>
21-
struct condition_variable {
21+
struct condition_variable_t {
2222
private:
2323
// Intrusive list node for waiting coroutines
2424
struct waiter_node {
@@ -28,14 +28,14 @@ struct condition_variable {
2828
};
2929

3030
public:
31-
condition_variable() : head_(nullptr), tail_(nullptr) {}
31+
condition_variable_t() : head_(nullptr), tail_(nullptr) {}
3232

3333
// Wait releases the mutex and suspends the coroutine
3434
// When resumed by notify, the mutex is NOT automatically re-acquired
3535
// You must manually re-acquire the lock after wait returns
3636
// Similar to Go's cond.Wait()
3737
struct wait_awaitable {
38-
condition_variable* cv_;
38+
condition_variable_t* cv_;
3939
void* mtx_{}; // Type-erased mutex pointer
4040
void (*unlock_fn_)(void*){}; // Function pointer to unlock
4141
waiter_node node_{};
@@ -178,7 +178,8 @@ struct condition_variable {
178178
};
179179

180180
// Type aliases for convenience
181-
using condition_variable_mt = condition_variable<std::mutex>;
182-
using condition_variable_st = condition_variable<dummy_mutex>;
181+
using condition_variable = condition_variable_t<std::mutex>;
182+
using condition_variable_mt = condition_variable_t<std::mutex>;
183+
using condition_variable_st = condition_variable_t<dummy_mutex>;
183184

184185
} // namespace coro

test/coro_condition_variable_mt.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void test_concurrent_notify() {
3535
auto exec = std::make_unique<executor_loop>();
3636
auto* exec_ptr = exec.get();
3737

38-
// Capture i by value properly
39-
static auto waiter = [&mtx, &cv, &wakeup_count, id = i]() -> async<void> {
38+
// Create waiter with proper parameter passing
39+
auto waiter = [](coro::mutex& mtx, coro::condition_variable& cv, std::atomic<int>& wakeup_count, int id) -> async<void> {
4040
LOG("Waiter %d: starting", id);
4141
co_await mtx.lock();
4242
LOG("Waiter %d: acquired lock, waiting", id);
@@ -49,7 +49,7 @@ void test_concurrent_notify() {
4949
LOG("Waiter %d: finished", id);
5050
};
5151

52-
waiter().detach(*exec_ptr);
52+
waiter(mtx, cv, wakeup_count, i).detach(*exec_ptr);
5353

5454
// Each executor runs in its own thread
5555
threads.emplace_back([exec_ptr]() {
@@ -126,7 +126,7 @@ void test_concurrent_wait() {
126126
for (int wid = 0; wid < waiters_per_thread; wid++) {
127127
int unique_id = tid * waiters_per_thread + wid;
128128

129-
static auto waiter = [&mtx, &cv, &ready_count, unique_id]() -> async<void> {
129+
auto waiter = [](coro::mutex& mtx, coro::condition_variable& cv, std::atomic<int>& ready_count, int unique_id) -> async<void> {
130130
co_await mtx.lock();
131131
LOG("Waiter %d: waiting", unique_id);
132132
co_await cv.wait(mtx);
@@ -136,7 +136,7 @@ void test_concurrent_wait() {
136136
mtx.unlock();
137137
};
138138

139-
waiter().detach(*exec_ptr);
139+
waiter(mtx, cv, ready_count, unique_id).detach(*exec_ptr);
140140
}
141141

142142
threads.emplace_back([exec_ptr]() {

test/coro_wait_group_mt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ async<void> wait_group_reuse_mt_test(executor& exec1, executor& exec2, std::thre
255255
}
256256

257257
// Test wait_group_st (single-threaded version) on multiple threads to verify it works
258-
async<void> wait_group_st_test(executor& exec1, executor& exec2, std::thread::id exec1_tid, std::thread::id exec2_tid) {
258+
async<void> wait_group_st_test() {
259259
LOG("=== Single-Thread WaitGroup Type Test ===");
260260

261261
// Note: wait_group_st is for single-threaded use, but we test it still works correctly
@@ -309,7 +309,7 @@ async<void> run_all_tests(executor& exec1, executor& exec2, std::thread::id exec
309309
co_await wait_group_reuse_mt_test(exec1, exec2, exec1_tid, exec2_tid);
310310
LOG("");
311311

312-
co_await wait_group_st_test(exec1, exec2, exec1_tid, exec2_tid);
312+
co_await wait_group_st_test();
313313
LOG("");
314314

315315
LOG("=== All Wait Group Multi-Thread Tests PASSED! ===");

0 commit comments

Comments
 (0)