1 /* Copyright (c) 2010, 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 Street, 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 "my_thread.h"
28 
29 namespace my_thread_unittest {
30 
handle_thread(void * arg)31 extern "C" void *handle_thread(void *arg)
32 {
33   my_thread_exit(0);
34   return 0; // Avoid compiler warning
35 }
36 
37 
38 class ThreadTest : public ::testing::Test
39 {
40 };
41 
42 
43 // Join with running/already finished thread
TEST(Thread,CreateAndJoin)44 TEST(Thread, CreateAndJoin)
45 {
46   my_thread_handle thr;
47   my_thread_attr_t thr_attr;
48   my_thread_attr_init(&thr_attr);
49 #ifdef _WIN32
50   const HANDLE null_thr_handle= NULL;
51 #endif
52   int ret, tries=10;
53   while (tries)
54   {
55     ret= my_thread_create(&thr, &thr_attr, handle_thread, 0);
56     EXPECT_EQ(0, ret);
57 #ifdef _WIN32
58     EXPECT_NE(null_thr_handle, thr.handle);
59 #endif
60     ret = my_thread_join(&thr, NULL);
61     EXPECT_EQ(0, ret);
62     tries--;
63   }
64   my_thread_attr_destroy(&thr_attr);
65 }
66 
67 }  // namespace
68