Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
751}753// If the queue is empty, the background thread may be waiting for work.754if (background_work_queue_.empty()) {755background_work_cv_.Signal();756}758background_work_queue_.emplace(background_work_function, background_work_arg);759background_work_mutex_.Unlock();760}762void WindowsEnv::BackgroundThreadMain() {763while (true) {764background_work_mutex_.Lock();766// Wait until there is work to be done.767while (background_work_queue_.empty()) {768background_work_cv_.Wait();769}771assert(!background_work_queue_.empty());772auto background_work_function = background_work_queue_.front().function;773void* background_work_arg = background_work_queue_.front().arg;774background_work_queue_.pop();776background_work_mutex_.Unlock();777background_work_function(background_work_arg);778}779}781// Wraps an Env instance whose destructor is never created.782//783// Intended usage:784// using PlatformSingletonEnv = SingletonEnv<PlatformEnv>;785// void ConfigurePosixEnv(int param) {786// PlatformSingletonEnv::AssertEnvNotInitialized();787// // set global configuration flags.788// }789// Env* Env::Default() {790// static PlatformSingletonEnv default_env;791// return default_env.env();792// }793template <typename EnvType>794class SingletonEnv {795public:796SingletonEnv() {797#if !defined(NDEBUG)798env_initialized_.store(true, std::memory_order_relaxed);799#endif // !defined(NDEBUG)800static_assert(sizeof(env_storage_) >= sizeof(EnvType),801"env_storage_ will not fit the Env");802static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);803static_assert(804offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,805"env_storage_ does not meet the Env's alignment needs");806static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,807"env_storage_ does not meet the Env's alignment needs");808new (env_storage_) EnvType();809}810~SingletonEnv() = default;812SingletonEnv(const SingletonEnv&) = delete;813SingletonEnv& operator=(const SingletonEnv&) = delete;815Env* env() { return reinterpret_cast<Env*>(&env_storage_); }817static void AssertEnvNotInitialized() {818#if !defined(NDEBUG)819assert(!env_initialized_.load(std::memory_order_relaxed));820#endif // !defined(NDEBUG)821}823private:824alignas(EnvType) char env_storage_[sizeof(EnvType)];825#if !defined(NDEBUG)826static std::atomic<bool> env_initialized_;827#endif // !defined(NDEBUG)828};830#if !defined(NDEBUG)831template <typename EnvType>832std::atomic<bool> SingletonEnv<EnvType>::env_initialized_;833#endif // !defined(NDEBUG)835using WindowsDefaultEnv = SingletonEnv<WindowsEnv>;837} // namespace839void EnvWindowsTestHelper::SetReadOnlyMMapLimit(int limit) {840WindowsDefaultEnv::AssertEnvNotInitialized();841g_mmap_limit = limit;842}844Env* Env::Default() {845static WindowsDefaultEnv env_container;846return env_container.env();847}849} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.