MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
thread_utils-t.cc
1 /* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 // First include (the generated) my_config.h, to get correct platform defines.
17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 
20 #include "thread_utils.h"
21 
22 #include "mdl.h"
23 
24 using thread::Mutex_lock;
26 using thread::Thread;
27 
28 namespace {
29 
30 const int counter_start_value= 42;
31 
32 class NotificationThread : public Thread
33 {
34 public:
35  NotificationThread(Notification *start_notification,
36  Notification *end_notfication,
37  int *counter)
38  : m_start_notification(start_notification),
39  m_end_notification(end_notfication),
40  m_counter(counter)
41  {
42  }
43 
44  virtual void run()
45  {
46  // Verify counter, increment it, notify the main thread.
47  EXPECT_EQ(counter_start_value, *m_counter);
48  (*m_counter)+= 1;
49  m_start_notification->notify();
50 
51  // Wait for notification from other thread.
52  m_end_notification->wait_for_notification();
53  EXPECT_EQ(counter_start_value, *m_counter);
54 
55  // Set counter again before returning from thread.
56  (*m_counter)+= 1;
57  }
58 
59 private:
60  Notification *m_start_notification;
61  Notification *m_end_notification;
62  int *m_counter;
63 
64  NotificationThread(const NotificationThread&); // Not copyable.
65  void operator=(const NotificationThread&); // Not assignable.
66 };
67 
68 
69 /*
70  A basic, single-threaded test of Notification.
71  */
72 TEST(Notification, Notify)
73 {
74  Notification notification;
75  EXPECT_FALSE(notification.has_been_notified());
76  notification.notify();
77  EXPECT_TRUE(notification.has_been_notified());
78 }
79 
80 /*
81  Starts a thread, and verifies that the notification/synchronization
82  mechanism works.
83  */
84 TEST(NotificationThread, StartAndWait)
85 {
86  Notification start_notification;
87  Notification end_notfication;
88  int counter= counter_start_value;
89  NotificationThread
90  notification_thread(&start_notification, &end_notfication, &counter);
91  notification_thread.start();
92 
93  // Wait for the other thread to increment counter, and notify us.
94  start_notification.wait_for_notification();
95  EXPECT_EQ(counter_start_value + 1, counter);
96  EXPECT_TRUE(start_notification.has_been_notified());
97 
98  // Reset counter, and notify other thread.
99  counter= counter_start_value;
100  end_notfication.notify();
101  notification_thread.join();
102 
103  // We should see the final results of the thread we have joined.
104  EXPECT_EQ(counter_start_value + 1, counter);
105 }
106 
107 } // namespace