diff --git a/src/bk_download.cpp b/src/bk_download.cpp index 51e15a0d..cc72b8b9 100644 --- a/src/bk_download.cpp +++ b/src/bk_download.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,7 @@ bool check_downloaded(const std::string &dir) { return false; } +static std::mutex lock_files_mutex; static std::set lock_files; void BkDownload::switch_to_local_file() { @@ -188,6 +190,7 @@ bool BkDownload::download() { } bool BkDownload::lock_file() { + std::lock_guard lock(lock_files_mutex); if (lock_files.find(dir) != lock_files.end()) { LOG_WARN("failed to lock download path:`", dir); return false; @@ -197,6 +200,7 @@ bool BkDownload::lock_file() { } void BkDownload::unlock_file() { + std::lock_guard lock(lock_files_mutex); lock_files.erase(dir); } @@ -337,7 +341,7 @@ bool BkDownload::download_blob() { return true; } -void bk_download_proc(std::list &dl_list, uint64_t delay_sec, int &running) { +void bk_download_proc(std::list &dl_list, uint64_t delay_sec, volatile int &running) { auto tracer = overlaybd_otel::get_tracer("overlaybd"); auto span = tracer->StartSpan("background_download_process"); auto scope = tracer->WithActiveSpan(span); diff --git a/src/bk_download.h b/src/bk_download.h index d5962725..3176e902 100644 --- a/src/bk_download.h +++ b/src/bk_download.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "overlaybd/otel/tracer_common.h" @@ -45,7 +46,7 @@ class BkDownload { } BkDownload(ISwitchFile *sw_file, photon::fs::IFile *src_file, size_t file_size, const std::string &dir, const std::string &digest, const std::string &url, - int &running, int32_t limit_MB_ps, int32_t try_cnt, uint32_t bs) + volatile int &running, int32_t limit_MB_ps, int32_t try_cnt, uint32_t bs) : dir(dir), try_cnt(try_cnt), sw_file(sw_file), src_file(src_file), file_size(file_size), digest(digest), url(url), running(running), limit_MB_ps(limit_MB_ps), block_size(bs) { @@ -61,12 +62,12 @@ class BkDownload { size_t file_size; std::string digest; std::string url; - int &running; + volatile int &running; int32_t limit_MB_ps; uint32_t block_size; bool force_download = false; }; -void bk_download_proc(std::list &, uint64_t, int &); +void bk_download_proc(std::list &, uint64_t, volatile int &); } // namespace BKDL \ No newline at end of file diff --git a/src/image_file.h b/src/image_file.h index 86d44df2..2cbba76d 100644 --- a/src/image_file.h +++ b/src/image_file.h @@ -99,7 +99,7 @@ class ImageFile : public photon::fs::ForwardFile { int open_lower_layer(IFile *&file, ImageConfigNS::LayerConfig &layer, int index); std::string m_exception; - int m_status = 0; // 0: not started, 1: running, -1 exit + volatile int m_status = 0; // 0: not started, 1: running, -1 exit size_t size; uint64_t num_lbas;