Merge bitcoin/bitcoin#30537: kernel: Only setup kernel context globals once

93fb0e7897 kernel: Only setup kernel context globals once (TheCharlatan)

Pull request description:

  The globals setup by the function calls when creating a new kernel context only need to be setup once. Calling them multiple times may be wasteful and has no apparent benefit.

  Besides kernel users potentially creating multiple contexts, this change may also be useful for tests creating multiple setups.

ACKs for top commit:
  stickies-v:
    re-ACK 93fb0e7897
  maflcko:
    ACK 93fb0e7897 👝
  tdb3:
    re ACK 93fb0e7897

Tree-SHA512: c8418c23b34883b9b6af2b93c48760a931c246c9190fae372fb808f573408d332f53ca43b9c783eef561c4a6681e2fb63f215c939b40a87d597c0518dabea22a
This commit is contained in:
merge-script 2024-07-31 12:17:14 +01:00
commit c6b4718112
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1

View file

@ -8,15 +8,18 @@
#include <logging.h> #include <logging.h>
#include <random.h> #include <random.h>
#include <mutex>
#include <string> #include <string>
namespace kernel { namespace kernel {
Context::Context() Context::Context()
{ {
std::string sha256_algo = SHA256AutoDetect(); static std::once_flag globals_initialized{};
LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo); std::call_once(globals_initialized, []() {
RandomInit(); std::string sha256_algo = SHA256AutoDetect();
LogInfo("Using the '%s' SHA256 implementation\n", sha256_algo);
RandomInit();
});
} }