MySQL 8.0.40
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#ifdef _WIN32
60 HANDLE handle{INVALID_HANDLE_VALUE};
61#endif
62};
63
64#ifdef _WIN32
65#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE 0
66#define MYSQL_ROUTER_THREAD_CREATE_DETACHED 1
67typedef void *(__cdecl *my_start_routine)(void *);
68#else
69#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
70#define MYSQL_ROUTER_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
71typedef void *(*my_start_routine)(void *);
72#endif
73
74/**
75 * @brief MySQLRouterThread provides higher level interface to managing threads.
76 */
77class HARNESS_EXPORT MySQLRouterThread {
78 public:
79 using thread_function = void *(void *);
80
81 /**
82 * Allocates memory for thread of execution.
83 *
84 * @param thread_stack_size the memory size allocated to thread's stack
85 *
86 * @throw std::runtime_error if cannot adjust thread size
87 */
89 size_t thread_stack_size = mysql_harness::kDefaultStackSizeInKiloBytes);
90
91 /**
92 * Execute run_thread function in thread of execution.
93 *
94 * @param run_thread the pointer to the function that is executed in thread.
95 * It has to be non-member void*(void*) function
96 * @param args_ptr pointer to run_thread parameter
97 * @param detach true if thread is detached, false if thread is joinable
98 *
99 * @throw std::runtime_error if cannot create new thread of execution
100 */
101 void run(thread_function run_thread, void *args_ptr, bool detach = false);
102
103 /**
104 * Waits for a thread to finish its execution
105 */
106 void join();
107
108 /**
109 * Waits for a thread to finish its execution if thread is joinable and join
110 * wasn't called.
111 */
113
114 // disable copy assignment
116 explicit MySQLRouterThread(const MySQLRouterThread &) = delete;
117
118 private:
119 /** @brief handle to the thread */
121
122 /** @brief attribute of thread */
124
125 /** @brief true if thread is joinable but join wasn't called, false otherwise
126 */
127 bool should_join_ = false;
128};
129
130} // namespace mysql_harness
131
132#endif // end of MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
MySQLRouterThread provides higher level interface to managing threads.
Definition: mysql_router_thread.h:77
mysql_harness::mysql_router_thread_handle thread_handle_
handle to the thread
Definition: mysql_router_thread.h:120
MySQLRouterThread(const MySQLRouterThread &)=delete
void *(void *) thread_function
Definition: mysql_router_thread.h:79
mysql_harness::mysql_router_thread_attr_t thread_attr_
attribute of thread
Definition: mysql_router_thread.h:123
MySQLRouterThread & operator=(const MySQLRouterThread &)=delete
static void run(mysql_harness::PluginFuncEnv *)
Definition: io_plugin.cc:195
Definition: common.h:42
pthread_t mysql_router_thread_t
Definition: mysql_router_thread.h:53
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:71
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:57
mysql_router_thread_t thread
Definition: mysql_router_thread.h:58
#define HANDLE
Definition: violite.h:159