Gabime/fwrite unlocked (#3276)

* Use locking fwrite_unlocked if possible

* Added compile definitions to header_only
This commit is contained in:
Gabi Melman
2024-12-01 14:16:52 +02:00
committed by GitHub
parent 951c5b9987
commit 1e6250e183
7 changed files with 79 additions and 12 deletions

View File

@@ -589,6 +589,18 @@ SPDLOG_INLINE bool fsync(FILE *fp) {
#endif
}
// Do non-locking fwrite if possible by the os or use the regular locking fwrite
// Return true on success.
SPDLOG_INLINE bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) {
#if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED)
return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes;
#elif defined(SPDLOG_FWRITE_UNLOCKED)
return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes;
#else
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
#endif
}
} // namespace os
} // namespace details
} // namespace spdlog