17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include "thread_utils.h"
53 void *thread_start_routine(
void *arg)
56 Thread::run_wrapper(start_arg);
62 #define LOCAL_ASSERT_FALSE(arg) assert_false(arg, __LINE__)
63 void assert_false(
int arg,
int line)
65 ASSERT_FALSE(arg) <<
"failed with arg " << arg <<
" at line " << line;
73 if (m_thread_handle != NULL)
74 CloseHandle(m_thread_handle);
81 Thread_start_arg start_arg(
this);
83 pthread_create(&m_thread_id, NULL, thread_start_routine, &start_arg);
86 ADD_FAILURE() <<
" could not start thread, errno: " << errno;
90 start_arg.m_thread_started.wait_for_notification();
92 m_thread_handle= OpenThread(SYNCHRONIZE, FALSE, m_thread_id);
93 if (m_thread_handle == NULL)
95 DWORD lasterror= GetLastError();
97 <<
" could not open thread id " << m_thread_id
98 <<
" GetLastError: " << lasterror
102 start_arg.m_thread_continue.notify();
103 start_arg.m_thread_running.wait_for_notification();
111 DWORD
ret= WaitForSingleObject(m_thread_handle, INFINITE);
112 if (ret != WAIT_OBJECT_0)
114 DWORD lasterror= GetLastError();
116 <<
" could not join thread id " << m_thread_id
117 <<
" handle " << m_thread_handle
118 <<
" GetLastError: " << lasterror
121 CloseHandle(m_thread_handle);
122 m_thread_handle= NULL;
127 const int failed= pthread_join(m_thread_id, NULL);
131 <<
" could not join thread id " << m_thread_id
132 <<
" failed: " << failed <<
" errno: " << errno
139 void Thread::run_wrapper(Thread_start_arg *start_arg)
141 const my_bool error= my_thread_init();
143 Thread *thread= start_arg->m_thread;
144 start_arg->m_thread_started.notify();
145 start_arg->m_thread_continue.wait_for_notification();
146 start_arg->m_thread_running.notify();
152 Mutex_lock::Mutex_lock(
mysql_mutex_t *mutex) : m_mutex(mutex)
158 Mutex_lock::~Mutex_lock()
161 LOCAL_ASSERT_FALSE(failed);
165 Notification::Notification() : m_notified(FALSE)
168 LOCAL_ASSERT_FALSE(failed1);
170 LOCAL_ASSERT_FALSE(failed2);
173 Notification::~Notification()
179 bool Notification::has_been_notified()
181 Mutex_lock lock(&m_mutex);
185 void Notification::wait_for_notification()
187 Mutex_lock lock(&m_mutex);
191 ASSERT_FALSE(failed);
195 void Notification::notify()
197 Mutex_lock lock(&m_mutex);
200 ASSERT_FALSE(failed);