-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Injectable singleton class #1974
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8545b41
WIP
alyssawilk f5b6174
WIP
alyssawilk 4c22f0c
Merge branch 'abseil' into singleton
alyssawilk 7abd66c
WIP
alyssawilk 83a8806
Merge branch 'refs/heads/master' into abseil
alyssawilk 91f6bbb
Merge branch 'refs/heads/abseil' into singleton
alyssawilk 1f57850
status
alyssawilk c21aa9b
fix format
alyssawilk f60506a
Merge branch 'refs/heads/master' into abseil
alyssawilk 880829a
Merge branch 'abseil' into singleton
alyssawilk c568f7f
Merge branch 'master' into abseil
alyssawilk 23295fd
Merge branch 'abseil' into singleton
alyssawilk afd1e6d
splitting out singleton classes, adding unit tests
alyssawilk 5715662
abseil -> com_google_absl
alyssawilk 7428aa2
Merge branch 'refs/heads/master' into abseil
alyssawilk b88692e
Merge branch 'refs/heads/abseil' into singleton
alyssawilk 674c3e9
Moving singletons to source/common/singleton, adding detailed class c…
alyssawilk a156653
Merge branch 'refs/heads/master' into abseil
alyssawilk 1a8af58
Merge branch 'abseil' into singleton
alyssawilk fea701e
bowing to the cult of pedagogy ("how many spaces after a period" -> h…
alyssawilk 48d5b5f
Merge branch 'master' into abseil
alyssawilk e90ca87
Merge branch 'abseil' into singleton
alyssawilk cd31966
renaming abseil per declared workspace name (thanks jmillikin)
alyssawilk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include "absl/base/call_once.h" | ||
|
||
namespace Envoy { | ||
|
||
/** | ||
* ThreadSafeSingleton allows easy global cross-thread access to a non-const object. | ||
* | ||
* This singleton class should be used for singletons which must be globally | ||
* accessible but can not be marked as const. All functions in the singleton class | ||
* *must* be threadsafe. | ||
* | ||
* Note that there is heavy resistence in Envoy to adding this type of singleton | ||
* if data will persist with state changes across tests, as it becomes difficult | ||
* to write clean unit tests if a state change in one test will persist into | ||
* another test. Be wary of using it. A example of acceptable usage is OsSyscallsImpl, | ||
* where the functions are not strictly speaking const, but affect the OS rather than the | ||
* class itself. An example of unacceptable usage upstream would be for | ||
* globally accessible stat counters, it would have the aforementioned problem | ||
* where state "leaks" across tests. | ||
* | ||
* */ | ||
template <class T> class ThreadSafeSingleton { | ||
public: | ||
static T& get() { | ||
absl::call_once(ThreadSafeSingleton<T>::create_once_, &ThreadSafeSingleton<T>::Create); | ||
return *ThreadSafeSingleton<T>::instance_; | ||
} | ||
|
||
protected: | ||
template <typename A> friend class TestThreadsafeSingletonInjector; | ||
|
||
static void Create() { instance_ = new T(); } | ||
|
||
static absl::once_flag create_once_; | ||
static T* instance_; | ||
}; | ||
|
||
template <class T> absl::once_flag ThreadSafeSingleton<T>::create_once_; | ||
|
||
template <class T> T* ThreadSafeSingleton<T>::instance_ = nullptr; | ||
|
||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did occur to me that there is some truth-in-advertising issue with a
ThreadSafeSingleton
that isn't thread safe but forces you to be thread safe. Anyway, potatoes potatahs.