From 249bc514b2e7d0cb4015fea9aaec98efbd9ec962 Mon Sep 17 00:00:00 2001 From: Bright Chen Date: Mon, 7 Oct 2024 14:52:20 +0800 Subject: [PATCH] Make bthread semaphore and rwlock noncopyable (#2773) --- src/bthread/types.h | 15 ++++++++++++--- test/BUILD.bazel | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/bthread/types.h b/src/bthread/types.h index d177ea72b3..ff99bb1c1f 100644 --- a/src/bthread/types.h +++ b/src/bthread/types.h @@ -191,17 +191,26 @@ typedef struct bthread_cond_t { typedef struct { } bthread_condattr_t; -typedef struct { +typedef struct bthread_sem_t { +#if defined(__cplusplus) + bthread_sem_t() : butex(NULL), enable_csite(true) {} + DISALLOW_COPY_AND_ASSIGN(bthread_sem_t); +#endif unsigned* butex; bool enable_csite; } bthread_sem_t; -typedef struct { +typedef struct bthread_rwlock_t { +#if defined(__cplusplus) + bthread_rwlock_t() + : reader_count(0), reader_wait(0), wlock_flag(false), writer_csite{} {} + DISALLOW_COPY_AND_ASSIGN(bthread_rwlock_t); +#endif bthread_sem_t reader_sema; // Semaphore for readers to wait for completing writers. bthread_sem_t writer_sema; // Semaphore for writers to wait for completing readers. int reader_count; // Number of pending readers. int reader_wait; // Number of departing readers. - bool wlock_flag; // Flag used to indicate that a write lock has been hold. + bool wlock_flag; // Flag used to indicate that a write lock has been held. bthread_mutex_t write_queue_mutex; // Held if there are pending writers. bthread_contention_site_t writer_csite; } bthread_rwlock_t; diff --git a/test/BUILD.bazel b/test/BUILD.bazel index b8c3b3d236..7f49fe7102 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -139,6 +139,7 @@ TEST_BUTIL_SOURCES = [ #"popen_unittest.cpp", "bounded_queue_unittest.cc", "butil_unittest_main.cpp", + "scope_guard_unittest.cc", ] + select({ "@bazel_tools//tools/osx:darwin_x86_64": [], "//conditions:default": [