MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
polyglot_garbage_collector.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, 2025, 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, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_GARBAGE_COLLECTOR_H_
27#define MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_GARBAGE_COLLECTOR_H_
29
30#include <chrono>
31#include <condition_variable>
32#include <memory>
33#include <mutex>
34#include <optional>
35#include <thread>
36
37namespace shcore {
38namespace polyglot {
39
40/**
41 * The Garbage Collection logic is determined by the occurrence of events such
42 * as:
43 *
44 * - Statements being executed
45 * - Language instances being disposed
46 * - Completion of predefined time lapse
47 *
48 * The trigger for GC operations is defined by the following optional
49 * configurations:
50 *
51 * - Minimal interval (seconds): if defined, the GC will NOT be executed in
52 * periods shorter than this interval.
53 * - Minimal executed statements: if defined, the GC will not be executed if
54 * less than the defined number of statements has been executed.
55 * - Minimal language instances: if defined, the GC will not be executed if less
56 * than the defined number of language instances have been terminated.
57 *
58 * Additionally, when the garbage collector may be stopped (i.e. when
59 * terminating the application), in such case, a GC operation will be triggered
60 * despite the state of the configurable options.
61 *
62 * This class keeps the Garbage Collection configuration and executes GC in a
63 * separate thread based on the configuration and reported events.
64 */
65class Garbage_collector final {
67
68 public:
69 using Time_point = std::chrono::time_point<std::chrono::system_clock>;
70 Garbage_collector() = default;
72
73 Garbage_collector(const Garbage_collector &other) = delete;
75
78
80
81 struct Config {
82 std::optional<size_t> interval;
83 std::optional<size_t> statements;
84 std::optional<size_t> languages;
85 };
86
87 void notify(Event event);
88 void start(Config &&config, poly_isolate isolate);
89 void stop();
90
91 private:
92 void run(poly_isolate isolate);
93 void set_state(State state, const std::string &error = "");
94 std::mutex m_mutex;
95 std::mutex m_state_mutex;
96 std::condition_variable m_condition;
97 std::unique_ptr<std::thread> m_thread;
98
100 std::string m_error;
102 bool m_terminated = false;
105
107};
108
109} // namespace polyglot
110} // namespace shcore
111
112#endif // MYSQLSHDK_SCRIPTING_POLYGLOT_LANGUAGES_POLYGLOT_GARBAGE_COLLECTOR_H_
The Garbage Collection logic is determined by the occurrence of events such as:
Definition: polyglot_garbage_collector.h:65
std::chrono::time_point< std::chrono::system_clock > Time_point
Definition: polyglot_garbage_collector.h:69
Garbage_collector & operator=(const Garbage_collector &other)=delete
void set_state(State state, const std::string &error="")
Definition: polyglot_garbage_collector.cc:80
Garbage_collector(const Garbage_collector &other)=delete
bool m_terminated
Definition: polyglot_garbage_collector.h:102
void run(poly_isolate isolate)
Definition: polyglot_garbage_collector.cc:86
std::mutex m_state_mutex
Definition: polyglot_garbage_collector.h:95
std::condition_variable m_condition
Definition: polyglot_garbage_collector.h:96
Garbage_collector & operator=(Garbage_collector &&other)=delete
Time_point m_last_gctime
Definition: polyglot_garbage_collector.h:106
Event
Definition: polyglot_garbage_collector.h:79
State
Definition: polyglot_garbage_collector.h:66
std::string m_error
Definition: polyglot_garbage_collector.h:100
void notify(Event event)
Definition: polyglot_garbage_collector.cc:55
std::unique_ptr< std::thread > m_thread
Definition: polyglot_garbage_collector.h:97
void start(Config &&config, poly_isolate isolate)
Definition: polyglot_garbage_collector.cc:41
size_t m_language_count
Definition: polyglot_garbage_collector.h:104
Garbage_collector(Garbage_collector &&other)=delete
State m_state
Definition: polyglot_garbage_collector.h:99
~Garbage_collector()
Definition: polyglot_garbage_collector.cc:35
size_t m_statement_count
Definition: polyglot_garbage_collector.h:103
void stop()
Definition: polyglot_garbage_collector.cc:47
std::mutex m_mutex
Definition: polyglot_garbage_collector.h:94
Config m_config
Definition: polyglot_garbage_collector.h:101
Definition: file_system_exceptions.h:34
required string event
Definition: replication_group_member_actions.proto:32
Definition: polyglot_garbage_collector.h:81
std::optional< size_t > interval
Definition: polyglot_garbage_collector.h:82
std::optional< size_t > statements
Definition: polyglot_garbage_collector.h:83
std::optional< size_t > languages
Definition: polyglot_garbage_collector.h:84