1 /* Copyright (c) 2009, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 
27 #include "thread_utils.h"
28 
29 #include "mdl.h"
30 
31 using thread::Notification;
32 using thread::Thread;
33 
34 namespace {
35 
36 const int counter_start_value= 42;
37 
38 class NotificationThread : public Thread
39 {
40 public:
NotificationThread(Notification * start_notification,Notification * end_notfication,int * counter)41   NotificationThread(Notification *start_notification,
42                       Notification *end_notfication,
43                       int *counter)
44     : m_start_notification(start_notification),
45       m_end_notification(end_notfication),
46       m_counter(counter)
47   {
48   }
49 
run()50   virtual void run()
51   {
52     // Verify counter, increment it, notify the main thread.
53     EXPECT_EQ(counter_start_value, *m_counter);
54     (*m_counter)+= 1;
55     m_start_notification->notify();
56 
57     // Wait for notification from other thread.
58     m_end_notification->wait_for_notification();
59     EXPECT_EQ(counter_start_value, *m_counter);
60 
61     // Set counter again before returning from thread.
62     (*m_counter)+= 1;
63   }
64 
65 private:
66   Notification *m_start_notification;
67   Notification *m_end_notification;
68   int          *m_counter;
69 
70   NotificationThread(const NotificationThread&); // Not copyable.
71   void operator=(const NotificationThread&);      // Not assignable.
72 };
73 
74 
75 /*
76   A basic, single-threaded test of Notification.
77  */
TEST(Notification,Notify)78 TEST(Notification, Notify)
79 {
80   Notification notification;
81   EXPECT_FALSE(notification.has_been_notified());
82   notification.notify();
83   EXPECT_TRUE(notification.has_been_notified());
84 }
85 
86 /*
87   Starts a thread, and verifies that the notification/synchronization
88   mechanism works.
89  */
TEST(NotificationThread,StartAndWait)90 TEST(NotificationThread, StartAndWait)
91 {
92   Notification start_notification;
93   Notification end_notfication;
94   int counter= counter_start_value;
95   NotificationThread
96     notification_thread(&start_notification, &end_notfication, &counter);
97   notification_thread.start();
98 
99   // Wait for the other thread to increment counter, and notify us.
100   start_notification.wait_for_notification();
101   EXPECT_EQ(counter_start_value + 1, counter);
102   EXPECT_TRUE(start_notification.has_been_notified());
103 
104   // Reset counter, and notify other thread.
105   counter= counter_start_value;
106   end_notfication.notify();
107   notification_thread.join();
108 
109   // We should see the final results of the thread we have joined.
110   EXPECT_EQ(counter_start_value + 1, counter);
111 }
112 
113 }  // namespace
114