MySQL 9.0.0
Source Code Documentation
mysql_router_thread.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
27#define MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
28
29#include "harness_export.h"
30
31#ifndef MYSQL_ABI_CHECK
32#if defined(_WIN32)
33#include <stdlib.h>
34#include <windows.h>
35#else
36#include <pthread.h> // IWYU pragma: export
37#include <sched.h> // IWYU pragma: export
38#include <stdexcept>
39#endif
40#endif /* MYSQL_ABI_CHECK */
41
42namespace mysql_harness {
43
44static const size_t kDefaultStackSizeInKiloBytes = 1024;
45
46#ifdef _WIN32
47typedef DWORD mysql_router_thread_t;
48typedef struct thread_attr {
49 DWORD dwStackSize;
50 int detachstate;
52#else
53typedef pthread_t mysql_router_thread_t;
54typedef pthread_attr_t mysql_router_thread_attr_t;
55#endif
56
59
62#ifdef _WIN32
63 HANDLE handle{INVALID_HANDLE_VALUE};
64#endif
65};
66
67#ifdef _WIN32
68#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE 0
69#define MYSQL_ROUTER_THREAD_CREATE_DETACHED 1
70typedef void *(__cdecl *my_start_routine)(void *);
71#else
72#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
73#define MYSQL_ROUTER_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
74typedef void *(*my_start_routine)(void *);
75#endif
76
77/**
78 * @brief MySQLRouterThread provides higher level interface to managing threads.
79 */
80class HARNESS_EXPORT MySQLRouterThread {
81 public:
82 using thread_function = void *(void *);
83
84 /**
85 * Allocates memory for thread of execution.
86 *
87 * @param thread_stack_size the memory size allocated to thread's stack
88 *
89 * @throw std::runtime_error if cannot adjust thread size
90 */
92 size_t thread_stack_size = mysql_harness::kDefaultStackSizeInKiloBytes);
93
94 /**
95 * Execute run_thread function in thread of execution.
96 *
97 * @param run_thread the pointer to the function that is executed in thread.
98 * It has to be non-member void*(void*) function
99 * @param args_ptr pointer to run_thread parameter
100 * @param detach true if thread is detached, false if thread is joinable
101 *
102 * @throw std::runtime_error if cannot create new thread of execution
103 */
104 void run(thread_function run_thread, void *args_ptr, bool detach = false);
105
106 /**
107 * Waits for a thread to finish its execution
108 */
109 void join();
110
111 /**
112 * Waits for a thread to finish its execution if thread is joinable and join
113 * wasn't called.
114 */
116
117 // disable copy assignment
119 explicit MySQLRouterThread(const MySQLRouterThread &) = delete;
120
121 private:
122 /** @brief handle to the thread */
124
125 /** @brief attribute of thread */
127
128 /** @brief true if thread is joinable but join wasn't called, false otherwise
129 */
130 bool should_join_ = false;
131};
132
133} // namespace mysql_harness
134
135#endif // end of MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
MySQLRouterThread provides higher level interface to managing threads.
Definition: mysql_router_thread.h:80
mysql_harness::mysql_router_thread_handle thread_handle_
handle to the thread
Definition: mysql_router_thread.h:123
MySQLRouterThread(const MySQLRouterThread &)=delete
void *(void *) thread_function
Definition: mysql_router_thread.h:82
mysql_harness::mysql_router_thread_attr_t thread_attr_
attribute of thread
Definition: mysql_router_thread.h:126
MySQLRouterThread & operator=(const MySQLRouterThread &)=delete
static void run(mysql_harness::PluginFuncEnv *)
Definition: io_plugin.cc:199
Definition: common.h:42
pthread_t mysql_router_thread_t
Definition: mysql_router_thread.h:53
static constexpr mysql_router_thread_t null_thread_initializer
Definition: mysql_router_thread.h:57
static const size_t kDefaultStackSizeInKiloBytes
Definition: mysql_router_thread.h:44
pthread_attr_t mysql_router_thread_attr_t
Definition: mysql_router_thread.h:54
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
void *(* my_start_routine)(void *)
Definition: mysql_router_thread.h:74
static int handle(int sql_errno, const char *sqlstate, const char *message, void *state)
Bridge function between the C++ API offered by this module and the C API of the parser service.
Definition: services.cc:64
Definition: mysql_router_thread.h:60
mysql_router_thread_t thread
Definition: mysql_router_thread.h:61
#define HANDLE
Definition: violite.h:159