Code analysis violations Bug in scheduler - race during with worker thread shutdown.
Showing the changes to src/vm/sys/file_system.cpp (1 of 18 files). Show all
Unified · Split
| @@ -0,0 +1,48 @@ | |||
| 1 | // | ||
| 2 | // Dis VM | ||
| 3 | // File: file_system.cpp | ||
| 4 | // Author: arr | ||
| 5 | // | ||
| 6 | |||
| 7 | #include <iostream> | ||
| 8 | #include <fstream> | ||
| 9 | #include <mutex> | ||
| 10 | #include <utils.h> | ||
| 11 | #include "sys_utils.h" | ||
| 12 | |||
| 13 | using namespace disvm::runtime; | ||
| 14 | |||
| 15 | namespace | ||
| 16 | { | ||
| 17 | std::mutex _create_file_stream_mutex; | ||
| 18 | } | ||
| 19 | |||
| 20 | std::unique_ptr<std::fstream> disvm::runtime::sys::create_file_stream(const char *path, std::ios::openmode mode, cfs_flags_t flags) | ||
| 21 | { | ||
| 22 | auto file = std::unique_ptr<std::fstream>{}; | ||
| 23 | if (flags == cfs_flags_t::none) | ||
| 24 | { | ||
| 25 | file = std::make_unique<std::fstream>(path, mode); | ||
| 26 | } | ||
| 27 | else | ||
| 28 | { | ||
| 29 | std::unique_lock<std::mutex> lock{ _create_file_stream_mutex, std::defer_lock }; | ||
| 30 | if (util::has_flag(flags, cfs_flags_t::atomic)) | ||
| 31 | lock.lock(); | ||
| 32 | |||
| 33 | if (util::has_flag(flags, cfs_flags_t::ensure_create)) | ||
| 34 | { | ||
| 35 | std::fstream file_exists{ path }; | ||
| 36 | if (file_exists) | ||
| 37 | return{}; | ||
| 38 | } | ||
| 39 | |||
| 40 | file = std::make_unique<std::fstream>(path, mode); | ||
| 41 | } | ||
| 42 | |||
| 43 | // Return null if file stream is not open. | ||
| 44 | if (!file->is_open()) | ||
| 45 | return{}; | ||
| 46 | |||
| 47 | return file; | ||
| 48 | } | ||