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