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 #include "mutex_lock.h"
29 
30 namespace thread {
31 
32 namespace {
33 extern "C"
thread_start_routine(void * arg)34 void *thread_start_routine(void *arg)
35 {
36   Thread *start_arg= (Thread*) arg;
37   Thread::run_wrapper(start_arg);
38   return NULL;
39 }
40 
41 // We cannot use ASSERT_FALSE in constructors/destructors,
42 // so we add a local helper routine.
43 #define LOCAL_ASSERT_FALSE(arg) assert_false(arg, __LINE__)
assert_false(int arg,int line)44 void assert_false(int arg, int line)
45 {
46   ASSERT_FALSE(arg) << "failed with arg " << arg << " at line " << line;
47 }
48 
49 }  // namespace
50 
51 
start()52 int Thread::start()
53 {
54   const int retval=
55     my_thread_create(&m_thread_handle, NULL, thread_start_routine, this);
56   if (retval != 0)
57   {
58     ADD_FAILURE() << " could not start thread, errno: " << errno;
59     return retval;
60   }
61 
62   return retval;
63 }
64 
65 
join()66 void Thread::join()
67 {
68   const int failed= my_thread_join(&m_thread_handle, NULL);
69   if (failed)
70   {
71     ADD_FAILURE()
72       << " could not join thread id " << m_thread_handle.thread
73       << " failed: " << failed << " errno: " << errno
74       ;
75   }
76 }
77 
78 
run_wrapper(Thread * start_arg)79 void Thread::run_wrapper(Thread *start_arg)
80 {
81   const my_bool error= my_thread_init();
82   ASSERT_FALSE(error);
83   start_arg->run();
84   my_thread_end();
85 }
86 
87 
Notification()88 Notification::Notification() : m_notified(FALSE)
89 {
90   const int failed1= mysql_cond_init(0, &m_cond);
91   LOCAL_ASSERT_FALSE(failed1);
92   const int failed2= mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST);
93   LOCAL_ASSERT_FALSE(failed2);
94 }
95 
~Notification()96 Notification::~Notification()
97 {
98   mysql_mutex_destroy(&m_mutex);
99   mysql_cond_destroy(&m_cond);
100 }
101 
has_been_notified()102 bool Notification::has_been_notified()
103 {
104   Mutex_lock lock(&m_mutex);
105   return m_notified;
106 }
107 
wait_for_notification()108 void Notification::wait_for_notification()
109 {
110   Mutex_lock lock(&m_mutex);
111   while (!m_notified)
112   {
113     const int failed= mysql_cond_wait(&m_cond, &m_mutex);
114     ASSERT_FALSE(failed);
115   }
116 }
117 
notify()118 void Notification::notify()
119 {
120   Mutex_lock lock(&m_mutex);
121   m_notified= TRUE;
122   const int failed= mysql_cond_broadcast(&m_cond);
123   ASSERT_FALSE(failed);
124 }
125 
126 
127 }  // namespace thread
128