xref: /aosp_15_r20/external/abseil-cpp/absl/synchronization/lifetime_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
16*9356374aSAndroid Build Coastguard Worker #include <thread>  // NOLINT(build/c++11), Abseil test
17*9356374aSAndroid Build Coastguard Worker #include <type_traits>
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
20*9356374aSAndroid Build Coastguard Worker #include "absl/base/const_init.h"
21*9356374aSAndroid Build Coastguard Worker #include "absl/base/thread_annotations.h"
22*9356374aSAndroid Build Coastguard Worker #include "absl/log/check.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/mutex.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/notification.h"
25*9356374aSAndroid Build Coastguard Worker 
26*9356374aSAndroid Build Coastguard Worker namespace {
27*9356374aSAndroid Build Coastguard Worker 
28*9356374aSAndroid Build Coastguard Worker // A two-threaded test which checks that Mutex, CondVar, and Notification have
29*9356374aSAndroid Build Coastguard Worker // correct basic functionality.  The intent is to establish that they
30*9356374aSAndroid Build Coastguard Worker // function correctly in various phases of construction and destruction.
31*9356374aSAndroid Build Coastguard Worker //
32*9356374aSAndroid Build Coastguard Worker // Thread one acquires a lock on 'mutex', wakes thread two via 'notification',
33*9356374aSAndroid Build Coastguard Worker // then waits for 'state' to be set, as signalled by 'condvar'.
34*9356374aSAndroid Build Coastguard Worker //
35*9356374aSAndroid Build Coastguard Worker // Thread two waits on 'notification', then sets 'state' inside the 'mutex',
36*9356374aSAndroid Build Coastguard Worker // signalling the change via 'condvar'.
37*9356374aSAndroid Build Coastguard Worker //
38*9356374aSAndroid Build Coastguard Worker // These tests use CHECK to validate invariants, rather than EXPECT or ASSERT
39*9356374aSAndroid Build Coastguard Worker // from gUnit, because we need to invoke them during global destructors, when
40*9356374aSAndroid Build Coastguard Worker // gUnit teardown would have already begun.
ThreadOne(absl::Mutex * mutex,absl::CondVar * condvar,absl::Notification * notification,bool * state)41*9356374aSAndroid Build Coastguard Worker void ThreadOne(absl::Mutex* mutex, absl::CondVar* condvar,
42*9356374aSAndroid Build Coastguard Worker                absl::Notification* notification, bool* state) {
43*9356374aSAndroid Build Coastguard Worker   // Test that the notification is in a valid initial state.
44*9356374aSAndroid Build Coastguard Worker   CHECK(!notification->HasBeenNotified()) << "invalid Notification";
45*9356374aSAndroid Build Coastguard Worker   CHECK(!*state) << "*state not initialized";
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker   {
48*9356374aSAndroid Build Coastguard Worker     absl::MutexLock lock(mutex);
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker     notification->Notify();
51*9356374aSAndroid Build Coastguard Worker     CHECK(notification->HasBeenNotified()) << "invalid Notification";
52*9356374aSAndroid Build Coastguard Worker 
53*9356374aSAndroid Build Coastguard Worker     while (*state == false) {
54*9356374aSAndroid Build Coastguard Worker       condvar->Wait(mutex);
55*9356374aSAndroid Build Coastguard Worker     }
56*9356374aSAndroid Build Coastguard Worker   }
57*9356374aSAndroid Build Coastguard Worker }
58*9356374aSAndroid Build Coastguard Worker 
ThreadTwo(absl::Mutex * mutex,absl::CondVar * condvar,absl::Notification * notification,bool * state)59*9356374aSAndroid Build Coastguard Worker void ThreadTwo(absl::Mutex* mutex, absl::CondVar* condvar,
60*9356374aSAndroid Build Coastguard Worker                absl::Notification* notification, bool* state) {
61*9356374aSAndroid Build Coastguard Worker   CHECK(!*state) << "*state not initialized";
62*9356374aSAndroid Build Coastguard Worker 
63*9356374aSAndroid Build Coastguard Worker   // Wake thread one
64*9356374aSAndroid Build Coastguard Worker   notification->WaitForNotification();
65*9356374aSAndroid Build Coastguard Worker   CHECK(notification->HasBeenNotified()) << "invalid Notification";
66*9356374aSAndroid Build Coastguard Worker   {
67*9356374aSAndroid Build Coastguard Worker     absl::MutexLock lock(mutex);
68*9356374aSAndroid Build Coastguard Worker     *state = true;
69*9356374aSAndroid Build Coastguard Worker     condvar->Signal();
70*9356374aSAndroid Build Coastguard Worker   }
71*9356374aSAndroid Build Coastguard Worker }
72*9356374aSAndroid Build Coastguard Worker 
73*9356374aSAndroid Build Coastguard Worker // Launch thread 1 and thread 2, and block on their completion.
74*9356374aSAndroid Build Coastguard Worker // If any of 'mutex', 'condvar', or 'notification' is nullptr, use a locally
75*9356374aSAndroid Build Coastguard Worker // constructed instance instead.
RunTests(absl::Mutex * mutex,absl::CondVar * condvar)76*9356374aSAndroid Build Coastguard Worker void RunTests(absl::Mutex* mutex, absl::CondVar* condvar) {
77*9356374aSAndroid Build Coastguard Worker   absl::Mutex default_mutex;
78*9356374aSAndroid Build Coastguard Worker   absl::CondVar default_condvar;
79*9356374aSAndroid Build Coastguard Worker   absl::Notification notification;
80*9356374aSAndroid Build Coastguard Worker   if (!mutex) {
81*9356374aSAndroid Build Coastguard Worker     mutex = &default_mutex;
82*9356374aSAndroid Build Coastguard Worker   }
83*9356374aSAndroid Build Coastguard Worker   if (!condvar) {
84*9356374aSAndroid Build Coastguard Worker     condvar = &default_condvar;
85*9356374aSAndroid Build Coastguard Worker   }
86*9356374aSAndroid Build Coastguard Worker   bool state = false;
87*9356374aSAndroid Build Coastguard Worker   std::thread thread_one(ThreadOne, mutex, condvar, &notification, &state);
88*9356374aSAndroid Build Coastguard Worker   std::thread thread_two(ThreadTwo, mutex, condvar, &notification, &state);
89*9356374aSAndroid Build Coastguard Worker   thread_one.join();
90*9356374aSAndroid Build Coastguard Worker   thread_two.join();
91*9356374aSAndroid Build Coastguard Worker }
92*9356374aSAndroid Build Coastguard Worker 
TestLocals()93*9356374aSAndroid Build Coastguard Worker void TestLocals() {
94*9356374aSAndroid Build Coastguard Worker   absl::Mutex mutex;
95*9356374aSAndroid Build Coastguard Worker   absl::CondVar condvar;
96*9356374aSAndroid Build Coastguard Worker   RunTests(&mutex, &condvar);
97*9356374aSAndroid Build Coastguard Worker }
98*9356374aSAndroid Build Coastguard Worker 
99*9356374aSAndroid Build Coastguard Worker // Normal kConstInit usage
100*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT absl::Mutex const_init_mutex(absl::kConstInit);
TestConstInitGlobal()101*9356374aSAndroid Build Coastguard Worker void TestConstInitGlobal() { RunTests(&const_init_mutex, nullptr); }
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker // Global variables during start and termination
104*9356374aSAndroid Build Coastguard Worker //
105*9356374aSAndroid Build Coastguard Worker // In a translation unit, static storage duration variables are initialized in
106*9356374aSAndroid Build Coastguard Worker // the order of their definitions, and destroyed in the reverse order of their
107*9356374aSAndroid Build Coastguard Worker // definitions.  We can use this to arrange for tests to be run on these objects
108*9356374aSAndroid Build Coastguard Worker // before they are created, and after they are destroyed.
109*9356374aSAndroid Build Coastguard Worker 
110*9356374aSAndroid Build Coastguard Worker using Function = void (*)();
111*9356374aSAndroid Build Coastguard Worker 
112*9356374aSAndroid Build Coastguard Worker class OnConstruction {
113*9356374aSAndroid Build Coastguard Worker  public:
OnConstruction(Function fn)114*9356374aSAndroid Build Coastguard Worker   explicit OnConstruction(Function fn) { fn(); }
115*9356374aSAndroid Build Coastguard Worker };
116*9356374aSAndroid Build Coastguard Worker 
117*9356374aSAndroid Build Coastguard Worker class OnDestruction {
118*9356374aSAndroid Build Coastguard Worker  public:
OnDestruction(Function fn)119*9356374aSAndroid Build Coastguard Worker   explicit OnDestruction(Function fn) : fn_(fn) {}
~OnDestruction()120*9356374aSAndroid Build Coastguard Worker   ~OnDestruction() { fn_(); }
121*9356374aSAndroid Build Coastguard Worker  private:
122*9356374aSAndroid Build Coastguard Worker   Function fn_;
123*9356374aSAndroid Build Coastguard Worker };
124*9356374aSAndroid Build Coastguard Worker 
125*9356374aSAndroid Build Coastguard Worker // These tests require that the compiler correctly supports C++11 constant
126*9356374aSAndroid Build Coastguard Worker // initialization... but MSVC has a known regression (since v19.10) till v19.25:
127*9356374aSAndroid Build Coastguard Worker // https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html
128*9356374aSAndroid Build Coastguard Worker #if defined(__clang__) || !(defined(_MSC_VER) && _MSC_VER < 1925)
129*9356374aSAndroid Build Coastguard Worker // kConstInit
130*9356374aSAndroid Build Coastguard Worker // Test early usage.  (Declaration comes first; definitions must appear after
131*9356374aSAndroid Build Coastguard Worker // the test runner.)
132*9356374aSAndroid Build Coastguard Worker extern absl::Mutex early_const_init_mutex;
133*9356374aSAndroid Build Coastguard Worker // (Normally I'd write this +[], to make the cast-to-function-pointer explicit,
134*9356374aSAndroid Build Coastguard Worker // but in some MSVC setups we support, lambdas provide conversion operators to
135*9356374aSAndroid Build Coastguard Worker // different flavors of function pointers, making this trick ambiguous.)
__anon517c212a0202null136*9356374aSAndroid Build Coastguard Worker OnConstruction test_early_const_init([] {
137*9356374aSAndroid Build Coastguard Worker   RunTests(&early_const_init_mutex, nullptr);
138*9356374aSAndroid Build Coastguard Worker });
139*9356374aSAndroid Build Coastguard Worker // This definition appears before test_early_const_init, but it should be
140*9356374aSAndroid Build Coastguard Worker // initialized first (due to constant initialization).  Test that the object
141*9356374aSAndroid Build Coastguard Worker // actually works when constructed this way.
142*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT absl::Mutex early_const_init_mutex(absl::kConstInit);
143*9356374aSAndroid Build Coastguard Worker 
144*9356374aSAndroid Build Coastguard Worker // Furthermore, test that the const-init c'tor doesn't stomp over the state of
145*9356374aSAndroid Build Coastguard Worker // a Mutex.  Really, this is a test that the platform under test correctly
146*9356374aSAndroid Build Coastguard Worker // supports C++11 constant initialization.  (The constant-initialization
147*9356374aSAndroid Build Coastguard Worker // constructors of globals "happen at link time"; memory is pre-initialized,
148*9356374aSAndroid Build Coastguard Worker // before the constructors of either grab_lock or check_still_locked are run.)
149*9356374aSAndroid Build Coastguard Worker extern absl::Mutex const_init_sanity_mutex;
__anon517c212a0302() 150*9356374aSAndroid Build Coastguard Worker OnConstruction grab_lock([]() ABSL_NO_THREAD_SAFETY_ANALYSIS {
151*9356374aSAndroid Build Coastguard Worker   const_init_sanity_mutex.Lock();
152*9356374aSAndroid Build Coastguard Worker });
153*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT absl::Mutex const_init_sanity_mutex(absl::kConstInit);
__anon517c212a0402() 154*9356374aSAndroid Build Coastguard Worker OnConstruction check_still_locked([]() ABSL_NO_THREAD_SAFETY_ANALYSIS {
155*9356374aSAndroid Build Coastguard Worker   const_init_sanity_mutex.AssertHeld();
156*9356374aSAndroid Build Coastguard Worker   const_init_sanity_mutex.Unlock();
157*9356374aSAndroid Build Coastguard Worker });
158*9356374aSAndroid Build Coastguard Worker #endif  // defined(__clang__) || !(defined(_MSC_VER) && _MSC_VER > 1900)
159*9356374aSAndroid Build Coastguard Worker 
160*9356374aSAndroid Build Coastguard Worker // Test shutdown usage.  (Declarations come first; definitions must appear after
161*9356374aSAndroid Build Coastguard Worker // the test runner.)
162*9356374aSAndroid Build Coastguard Worker extern absl::Mutex late_const_init_mutex;
163*9356374aSAndroid Build Coastguard Worker // OnDestruction is being used here as a global variable, even though it has a
164*9356374aSAndroid Build Coastguard Worker // non-trivial destructor.  This is against the style guide.  We're violating
165*9356374aSAndroid Build Coastguard Worker // that rule here to check that the exception we allow for kConstInit is safe.
166*9356374aSAndroid Build Coastguard Worker // NOLINTNEXTLINE
__anon517c212a0502null167*9356374aSAndroid Build Coastguard Worker OnDestruction test_late_const_init([] {
168*9356374aSAndroid Build Coastguard Worker   RunTests(&late_const_init_mutex, nullptr);
169*9356374aSAndroid Build Coastguard Worker });
170*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT absl::Mutex late_const_init_mutex(absl::kConstInit);
171*9356374aSAndroid Build Coastguard Worker 
172*9356374aSAndroid Build Coastguard Worker }  // namespace
173*9356374aSAndroid Build Coastguard Worker 
main()174*9356374aSAndroid Build Coastguard Worker int main() {
175*9356374aSAndroid Build Coastguard Worker   TestLocals();
176*9356374aSAndroid Build Coastguard Worker   TestConstInitGlobal();
177*9356374aSAndroid Build Coastguard Worker   // Explicitly call exit(0) here, to make it clear that we intend for the
178*9356374aSAndroid Build Coastguard Worker   // above global object destructors to run.
179*9356374aSAndroid Build Coastguard Worker   std::exit(0);
180*9356374aSAndroid Build Coastguard Worker }
181