From 6b7f5d858fb3a8f6aff8184c1fa05a4d10f74a38 Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Thu, 14 Sep 2023 15:24:20 +0200 Subject: [PATCH] Review code --- src/xtd.core/src/xtd/threading/monitor.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/xtd.core/src/xtd/threading/monitor.cpp b/src/xtd.core/src/xtd/threading/monitor.cpp index 941fe01877f4..72a7084c49a3 100644 --- a/src/xtd.core/src/xtd/threading/monitor.cpp +++ b/src/xtd.core/src/xtd/threading/monitor.cpp @@ -20,7 +20,11 @@ class monitor::critical_section { public: critical_section() : handle_(std::make_shared(native::critical_section::create())) {} critical_section(const critical_section&) = default; - critical_section& operator =(const critical_section&) = default; + critical_section& operator =(const critical_section& other) { + native::critical_section::destroy(*handle_); + handle_ = other.handle_; + return *this; + } critical_section(critical_section&&) = default; ~critical_section() {if (handle_.use_count() == 1) native::critical_section::destroy(*handle_);} @@ -52,7 +56,11 @@ class monitor::condition_variable { public: condition_variable() : handle_(std::make_shared(native::condition_variable::create())) {} condition_variable(const condition_variable&) = default; - condition_variable& operator =(const condition_variable&) = default; + condition_variable& operator =(const condition_variable& other) { + native::critical_section::destroy(*handle_); + handle_ = other.handle_; + return *this; + } condition_variable(condition_variable&&) = default; ~condition_variable() {if (handle_.use_count() == 1) native::condition_variable::destroy(*handle_);} @@ -98,17 +106,15 @@ void monitor::exit_ptr(object_ptr obj) { throw synchronization_lock_exception {csf_}; } - item saved; item* monitor_data = &get_static_data().monitor_items[obj.first]; + monitor_data->thread_id = thread::invalid_thread_id; + monitor_data->critical_section.leave(); + get_static_data().monitor_items_critical_section.leave(); + if (interlocked::decrement(monitor_data->used_counter) == 0) { - saved = get_static_data().monitor_items[obj.first]; if (obj.second) delete reinterpret_cast(obj.first); get_static_data().monitor_items.erase(obj.first); - monitor_data = &saved; } - monitor_data->thread_id = thread::invalid_thread_id; - monitor_data->critical_section.leave(); - get_static_data().monitor_items_critical_section.leave(); } intptr monitor::get_ustring_ptr(const ustring& str) {