MySQL 9.0.0
Source Code Documentation
thread_affinity.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 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 MYSQLROUTER_IO_THREAD_AFFINITY_H
27#define MYSQLROUTER_IO_THREAD_AFFINITY_H
28
29#include <bitset>
30#include <system_error>
31#include <thread>
32
33#if defined(__linux__)
34#include <sched.h> // CPU_SETSIZE
35#elif defined(__FreeBSD__)
36#include <sys/types.h> // must be before sys/cpuset.h for cpusetid_t
37
38#include <sys/cpuset.h> // CPU_SETSIZE
39#elif defined(_WIN32)
40#include <Windows.h>
41#endif
42
45
46/**
47 * manage thread affinity.
48 */
50 public:
51#if defined(__linux__) || defined(__FreeBSD__)
52 static constexpr const int max_cpus = CPU_SETSIZE;
53#elif defined(_WIN32)
54 static constexpr const int max_cpus = 64;
55#else
56 static constexpr const int max_cpus = 1024;
57#endif
58
59#if defined(_WIN32)
61#else
62 using native_handle_type = pthread_t;
63#endif
64
66 : thread_id_{thread_id} {}
67
68 // get current thread's native handle
69 static native_handle_type current_thread_handle() noexcept;
70
71 // get CPU affinity
72 stdx::expected<std::bitset<max_cpus>, std::error_code> affinity()
73 const noexcept;
74
75 // set CPU affinity
76 stdx::expected<void, std::error_code> affinity(
77 std::bitset<ThreadAffinity::max_cpus> cpus) const noexcept;
78
79 private:
81};
82
83#endif
manage thread affinity.
Definition: thread_affinity.h:49
ThreadAffinity(native_handle_type thread_id) noexcept
Definition: thread_affinity.h:65
pthread_t native_handle_type
Definition: thread_affinity.h:62
#define IO_COMPONENT_EXPORT
Definition: io_component_export.h:15
static my_thread_id thread_id
Definition: my_thr_init.cc:63
int native_handle_type
Definition: socket_constants.h:51
Definition: gcs_xcom_synode.h:64
Definition: bit.h:32
#define HANDLE
Definition: violite.h:159