Skip to content

Commit

Permalink
[ut]fix some data race (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Feb 6, 2025
1 parent 0868878 commit 7e3d7b2
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 194 deletions.
4 changes: 2 additions & 2 deletions include/ylt/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ inline bool open_native_async_file(File &file, Executor &executor,
static_cast<asio::file_base::flags>(open_flags));
}
} catch (std::exception &ex) {
std::cout << "line " << __LINE__ << " coro_file open failed" << ex.what()
ELOG_INFO << "line " << __LINE__ << " coro_file open failed" << ex.what()
<< "\n";
return false;
}
Expand Down Expand Up @@ -357,7 +357,7 @@ class basic_seq_coro_file {
[this, flags, filepath]() mutable {
frw_seq_file_.open(filepath.data(), flags);
if (!frw_seq_file_.is_open()) {
std::cout << "line " << __LINE__ << " coro_file open failed "
ELOG_INFO << "line " << __LINE__ << " coro_file open failed "
<< filepath << "\n";
std::cerr << "Error: " << strerror(errno);
return false;
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/coro_rpc/impl/coro_rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class coro_rpc_client {
}
#ifdef YLT_ENABLE_SSL
if (!ssl_init_ret_) {
std::cout << "ssl_init_ret_: " << ssl_init_ret_ << std::endl;
ELOG_INFO << "ssl_init_ret_: " << ssl_init_ret_;
co_return errc::not_connected;
}
#endif
Expand Down
6 changes: 4 additions & 2 deletions include/ylt/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class appender {

template <bool sync = false, bool enable_console = false>
void write_record(record_t &record) {
std::lock_guard guard(get_mutex<sync>());
std::unique_lock guard(get_mutex<sync>());
if constexpr (sync == true) {
if (max_files_ > 0 && file_size_ > max_file_size_ &&
static_cast<size_t>(-1) != file_size_) {
Expand All @@ -204,6 +204,8 @@ class appender {
write_file(msg);

if constexpr (enable_console) {
guard.unlock();
std::unique_lock guard1(get_mutex<true>());
add_color(record.get_severity());
std::cout << time_str;
clean_color(record.get_severity());
Expand Down Expand Up @@ -405,7 +407,7 @@ class appender {
bool has_init_ = false;
std::string filename_;

bool enable_console_ = false;
std::atomic<bool> enable_console_ = false;
bool flush_every_time_;
size_t file_size_ = 0;
size_t max_file_size_ = 0;
Expand Down
5 changes: 5 additions & 0 deletions include/ylt/standalone/cinatra/cinatra_log_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <iostream>
#include <mutex>
namespace cinatra {
struct null_logger_t {
template <typename T>
Expand All @@ -14,6 +15,8 @@ struct cout_logger_t {
return *this;
}
~cout_logger_t() { std::cout << std::endl; }
std::unique_lock<std::mutex> lock_ = std::unique_lock{mtx_};
inline static std::mutex mtx_;
};
struct cerr_logger_t {
template <typename T>
Expand All @@ -22,6 +25,8 @@ struct cerr_logger_t {
return *this;
}
~cerr_logger_t() { std::cerr << std::endl; }
std::unique_lock<std::mutex> lock_ = std::unique_lock{mtx_};
inline static std::mutex mtx_;
};

constexpr inline cinatra::null_logger_t NULL_LOGGER;
Expand Down
8 changes: 8 additions & 0 deletions include/ylt/standalone/cinatra/session_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class session_manager {
}

void start_check_session_timer() {
std::lock_guard lock(timer_mtx_);
std::weak_ptr<asio::steady_timer> timer = check_session_timer_;
check_session_timer_->expires_after(check_session_duration_);
check_session_timer_->async_wait([this, timer](auto ec) {
Expand All @@ -79,6 +80,12 @@ class session_manager {

void set_check_session_duration(auto duration) {
check_session_duration_ = duration;
{
std::lock_guard lock(timer_mtx_);
std::error_code ec;
check_session_timer_->cancel(ec);
}

start_check_session_timer();
}

Expand All @@ -100,6 +107,7 @@ class session_manager {
// session_timeout_ should be no less than 0
std::size_t session_timeout_ = 86400;
std::atomic<bool> stop_timer_ = false;
std::mutex timer_mtx_;
std::shared_ptr<asio::steady_timer> check_session_timer_;
std::atomic<std::chrono::steady_clock::duration> check_session_duration_ = {
std::chrono::seconds(15)};
Expand Down
Loading

0 comments on commit 7e3d7b2

Please sign in to comment.