MySQL 8.3.0
Source Code Documentation
mysql_router_thread.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
26#define MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
27
28#include "harness_export.h"
29
30#ifndef MYSQL_ABI_CHECK
31#if defined(_WIN32)
32#include <stdlib.h>
33#include <windows.h>
34#else
35#include <pthread.h> // IWYU pragma: export
36#include <sched.h> // IWYU pragma: export
37#include <stdexcept>
38#endif
39#endif /* MYSQL_ABI_CHECK */
40
41namespace mysql_harness {
42
43static const size_t kDefaultStackSizeInKiloBytes = 1024;
44
45#ifdef _WIN32
46typedef DWORD mysql_router_thread_t;
47typedef struct thread_attr {
48 DWORD dwStackSize;
49 int detachstate;
51#else
52typedef pthread_t mysql_router_thread_t;
53typedef pthread_attr_t mysql_router_thread_attr_t;
54#endif
55
58
61#ifdef _WIN32
62 HANDLE handle{INVALID_HANDLE_VALUE};
63#endif
64};
65
66#ifdef _WIN32
67#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE 0
68#define MYSQL_ROUTER_THREAD_CREATE_DETACHED 1
69typedef void *(__cdecl *my_start_routine)(void *);
70#else
71#define MYSQL_ROUTER_THREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
72#define MYSQL_ROUTER_THREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
73typedef void *(*my_start_routine)(void *);
74#endif
75
76/**
77 * @brief MySQLRouterThread provides higher level interface to managing threads.
78 */
79class HARNESS_EXPORT MySQLRouterThread {
80 public:
81 using thread_function = void *(void *);
82
83 /**
84 * Allocates memory for thread of execution.
85 *
86 * @param thread_stack_size the memory size allocated to thread's stack
87 *
88 * @throw std::runtime_error if cannot adjust thread size
89 */
91 size_t thread_stack_size = mysql_harness::kDefaultStackSizeInKiloBytes);
92
93 /**
94 * Execute run_thread function in thread of execution.
95 *
96 * @param run_thread the pointer to the function that is executed in thread.
97 * It has to be non-member void*(void*) function
98 * @param args_ptr pointer to run_thread parameter
99 * @param detach true if thread is detached, false if thread is joinable
100 *
101 * @throw std::runtime_error if cannot create new thread of execution
102 */
103 void run(thread_function run_thread, void *args_ptr, bool detach = false);
104
105 /**
106 * Waits for a thread to finish its execution
107 */
108 void join();
109
110 /**
111 * Waits for a thread to finish its execution if thread is joinable and join
112 * wasn't called.
113 */
115
116 // disable copy assignment
118 explicit MySQLRouterThread(const MySQLRouterThread &) = delete;
119
120 private:
121 /** @brief handle to the thread */
123
124 /** @brief attribute of thread */
126
127 /** @brief true if thread is joinable but join wasn't called, false otherwise
128 */
129 bool should_join_ = false;
130};
131
132} // namespace mysql_harness
133
134#endif // end of MYSQL_HARNESS_MYSQL_ROUTER_THREAD_INCLUDED
MySQLRouterThread provides higher level interface to managing threads.
Definition: mysql_router_thread.h:79
mysql_harness::mysql_router_thread_handle thread_handle_
handle to the thread
Definition: mysql_router_thread.h:122
MySQLRouterThread(const MySQLRouterThread &)=delete
void *(void *) thread_function
Definition: mysql_router_thread.h:81
mysql_harness::mysql_router_thread_attr_t thread_attr_
attribute of thread
Definition: mysql_router_thread.h:125
MySQLRouterThread & operator=(const MySQLRouterThread &)=delete
static void run(mysql_harness::PluginFuncEnv *)
Definition: io_plugin.cc:194
Definition: common.h:41
pthread_t mysql_router_thread_t
Definition: mysql_router_thread.h:52
static constexpr mysql_router_thread_t null_thread_initializer
Definition: mysql_router_thread.h:56
static const size_t kDefaultStackSizeInKiloBytes
Definition: mysql_router_thread.h:43
pthread_attr_t mysql_router_thread_attr_t
Definition: mysql_router_thread.h:53
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:150
void *(* my_start_routine)(void *)
Definition: mysql_router_thread.h:73
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:63
Definition: mysql_router_thread.h:59
mysql_router_thread_t thread
Definition: mysql_router_thread.h:60
#define HANDLE
Definition: violite.h:158