aboutsummaryrefslogtreecommitdiffhomepage
path: root/absl/synchronization/lifetime_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/synchronization/lifetime_test.cc')
-rw-r--r--absl/synchronization/lifetime_test.cc41
1 files changed, 41 insertions, 0 deletions
diff --git a/absl/synchronization/lifetime_test.cc b/absl/synchronization/lifetime_test.cc
index 6aea60e..a3e2701 100644
--- a/absl/synchronization/lifetime_test.cc
+++ b/absl/synchronization/lifetime_test.cc
@@ -69,8 +69,22 @@ void ThreadTwo(absl::Mutex* mutex, absl::CondVar* condvar,
}
// Launch thread 1 and thread 2, and block on their completion.
+// If any of 'mutex', 'condvar', or 'notification' is nullptr, use a locally
+// constructed instance instead.
void RunTests(absl::Mutex* mutex, absl::CondVar* condvar,
absl::Notification* notification) {
+ absl::Mutex default_mutex;
+ absl::CondVar default_condvar;
+ absl::Notification default_notification;
+ if (!mutex) {
+ mutex = &default_mutex;
+ }
+ if (!condvar) {
+ condvar = &default_condvar;
+ }
+ if (!notification) {
+ notification = &default_notification;
+ }
bool state = false;
std::thread thread_one(ThreadOne, mutex, condvar, notification, &state);
std::thread thread_two(ThreadTwo, mutex, condvar, notification, &state);
@@ -85,6 +99,33 @@ void TestLocals() {
RunTests(&mutex, &condvar, &notification);
}
+// Global variables during start and termination
+//
+// In a translation unit, static storage duration variables are initialized in
+// the order of their definitions, and destroyed in the reverse order of their
+// definitions. We can use this to arrange for tests to be run on these objects
+// before they are created, and after they are destroyed.
+
+class ConstructorTestRunner {
+ public:
+ ConstructorTestRunner(absl::Mutex* mutex, absl::CondVar* condvar,
+ absl::Notification* notification) {
+ RunTests(mutex, condvar, notification);
+ }
+};
+
+class DestructorTestRunner {
+ public:
+ DestructorTestRunner(absl::Mutex* mutex, absl::CondVar* condvar,
+ absl::Notification* notification)
+ : mutex_(mutex), condvar_(condvar), notification_(notification) {}
+ ~DestructorTestRunner() { RunTests(mutex_, condvar_, notification_); }
+ private:
+ absl::Mutex* mutex_;
+ absl::CondVar* condvar_;
+ absl::Notification* notification_;
+};
+
} // namespace
int main() {