Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /base /lazy_instance_helpers.h
blob: 2a0b1dfd83cfd3be94b50e6de6d6348d3df83064 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:06[diff] [blame]1// Copyright 2018 The Chromium Authors
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lei Zhang6e174342021-04-21 03:57:20[diff] [blame]5#ifndef BASE_LAZY_INSTANCE_HELPERS_H_
6#define BASE_LAZY_INSTANCE_HELPERS_H_
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]7
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]8#include<atomic>
9#include<cstdint>
Peter Kasting134ef9af2024-12-28 02:30:09[diff] [blame]10
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]11#include"base/base_export.h"
Hans Wennborg7b533712020-06-22 20:52:27[diff] [blame]12#include"base/check.h"
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]13
14// Helper methods used by LazyInstance and a few other base APIs for thread-safe
15// lazy construction.
16
17namespacebase{
18namespaceinternal{
19
20// Our AtomicWord doubles as a spinlock, where a value of
21// kLazyInstanceStateCreating means the spinlock is being held for creation.
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]22constexpruintptr_t kLazyInstanceStateCreating=1;
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]23
24// Helper for GetOrCreateLazyPointer(). Checks if instance needs to be created.
25// If so returns true otherwise if another thread has beat us, waits for
26// instance to be created and returns false.
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]27BASE_EXPORTboolNeedsLazyInstance(std::atomic<uintptr_t>& state);
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]28
29// Helper for GetOrCreateLazyPointer(). After creating an instance, this is
30// called to register the dtor to be called at program exit and to update the
31// atomic state to hold the |new_instance|
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]32BASE_EXPORTvoidCompleteLazyInstance(std::atomic<uintptr_t>& state,
33uintptr_t new_instance,
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]34void(*destructor)(void*),
35void* destructor_arg);
36
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]37}// namespace internal
38
39namespace subtle{
40
41// If |state| is uninitialized (zero), constructs a value using
42// |creator_func(creator_arg)|, stores it into |state| and registers
43// |destructor(destructor_arg)| to be called when the current AtExitManager goes
44// out of scope. Then, returns the value stored in |state|. It is safe to have
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]45// concurrent calls to this function with the same |state|. |creator_func| may
46// return nullptr if it doesn't want to create an instance anymore (e.g. on
47// shutdown), it is from then on required to return nullptr to all callers (ref.
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]48// StaticMemorySingletonTraits). In that case, callers need to synchronize
49// before |creator_func| may return a non-null instance again (ref.
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]50// StaticMemorySingletonTraits::ResurectForTesting()).
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]51// Implementation note on |creator_func/creator_arg|. It makes for ugly adapters
52// but it avoids redundant template instantiations (e.g. saves 27KB in
53// chrome.dll) because linker is able to fold these for multiple Types but
54// couldn't with the more advanced CreatorFunc template type which in turn
55// improves code locality (and application startup) -- ref.
56// https://chromium-review.googlesource.com/c/chromium/src/+/530984/5/base/lazy_instance.h#140,
57// worsened by https://chromium-review.googlesource.com/c/chromium/src/+/868013
58// and caught then as https://crbug.com/804034.
59template<typenameType>
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]60Type*GetOrCreateLazyPointer(std::atomic<uintptr_t>& state,
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]61Type*(*creator_func)(void*),
62void* creator_arg,
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]63void(*destructor)(void*),
64void* destructor_arg){
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]65 DCHECK(creator_func);
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]66
67// If any bit in the created mask is true, the instance has already been
68// fully constructed.
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]69constexpruintptr_t kLazyInstanceCreatedMask=
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]70~internal::kLazyInstanceStateCreating;
71
72// We will hopefully have fast access when the instance is already created.
73// Since a thread sees |state| == 0 or kLazyInstanceStateCreating at most
74// once, the load is taken out of NeedsLazyInstance() as a fast-path. The load
75// has acquire memory ordering as a thread which sees |state| > creating needs
76// to acquire visibility over the associated data. Pairing Release_Store is in
77// CompleteLazyInstance().
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]78uintptr_t instance= state.load(std::memory_order_acquire);
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]79if(!(instance& kLazyInstanceCreatedMask)){
80if(internal::NeedsLazyInstance(state)){
81// This thread won the race and is now responsible for creating the
82// instance and storing it back into |state|.
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]83 instance=reinterpret_cast<uintptr_t>((*creator_func)(creator_arg));
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]84internal::CompleteLazyInstance(state, instance, destructor,
85 destructor_arg);
86}else{
87// This thread lost the race but now has visibility over the constructed
88// instance (NeedsLazyInstance() doesn't return until the constructing
89// thread releases the instance via CompleteLazyInstance()).
Benoit Lizeb82171a62022-06-17 13:37:58[diff] [blame]90 instance= state.load(std::memory_order_acquire);
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]91 DCHECK(instance& kLazyInstanceCreatedMask);
92}
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]93}
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]94returnreinterpret_cast<Type*>(instance);
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]95}
96
Gabriel Charetted55aad502018-01-26 18:07:51[diff] [blame]97}// namespace subtle
98
Gabriel Charettef297f012018-01-17 20:59:07[diff] [blame]99}// namespace base
100
Lei Zhang6e174342021-04-21 03:57:20[diff] [blame]101#endif// BASE_LAZY_INSTANCE_HELPERS_H_

[8]ページ先頭

©2009-2025 Movatter.jp