MySQL 9.1.0
Source Code Documentation
event_queue.h
Go to the documentation of this file.
1#ifndef _EVENT_QUEUE_H_
2#define _EVENT_QUEUE_H_
3/* Copyright (c) 2004, 2024, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26/**
27
28 @addtogroup Event_Scheduler
29 @{
30
31 @file event_queue.h
32
33 Queue of events awaiting execution.
34*/
35
36#include <sys/types.h>
37#include <time.h>
38#include <atomic>
39#include <vector>
40
41#include "lex_string.h"
42#include "my_psi_config.h"
43#include "my_time.h"
49#include "priority_queue.h" // Priority_queue
50#include "sql/event_data_objects.h" // Event_queue_element
51#include "sql/event_parse_data.h" // Event_parse_data
52#include "sql/malloc_allocator.h" // IWYU pragma: keep
53
54class THD;
55
56#ifdef HAVE_PSI_INTERFACE
59#endif /* HAVE_PSI_INTERFACE */
60
62 /// Maps compare function to strict weak ordering required by Priority_queue.
64 return event_queue_element_compare_q(left, right) > 0;
65 }
66
67 /**
68 Compares the execute_at members of two Event_queue_element instances.
69 Used as compare operator for the prioritized queue when shifting
70 elements inside.
71
72 SYNOPSIS
73 event_queue_element_compare_q()
74 @param left First Event_queue_element object
75 @param right Second Event_queue_element object
76
77 @retval
78 -1 left->execute_at < right->execute_at
79 0 left->execute_at == right->execute_at
80 1 left->execute_at > right->execute_at
81
82 @remark
83 execute_at.second_part is not considered during comparison
84 */
86 Event_queue_element *right) {
88 return right->m_status != Event_parse_data::DISABLED;
89
90 if (right->m_status == Event_parse_data::DISABLED) return 1;
91
92 const my_time_t lhs = left->m_execute_at;
93 const my_time_t rhs = right->m_execute_at;
94 return (lhs < rhs ? -1 : (lhs > rhs ? 1 : 0));
95 }
96};
97
98/**
99 Queue of active events awaiting execution.
100*/
101
103 public:
104 Event_queue();
105 ~Event_queue();
106
107 bool init_queue();
108
109 /* Methods for queue management follow */
110
111 bool create_event(THD *thd, Event_queue_element *new_element, bool *created);
112
113 void update_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name,
114 Event_queue_element *new_element);
115
116 void drop_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name);
117
118 void drop_schema_events(LEX_CSTRING schema);
119
121
123 Event_queue_element_for_exec **event_name);
124
126
127 private:
128 void empty_queue();
129
130 void deinit_queue();
131 /* helper functions for working with mutexes & conditionals */
132 void lock_data(const char *func, uint line);
133
134 void unlock_data(const char *func, uint line);
135
136 void cond_wait(THD *thd, struct timespec *abstime,
137 const PSI_stage_info *stage, const char *src_func,
138 const char *src_file, uint src_line);
139
141
143 bool (*)(LEX_CSTRING, Event_basic *));
144
145 void dbug_dump_queue(time_t now);
146
147 /* LOCK_event_queue is the mutex which protects the access to the queue. */
150
151 /* The sorted queue with the Event_queue_element objects */
157
159
165 std::atomic<const char *> mutex_last_attempted_lock_in_func;
169};
170/**
171 @} (End of group Event_Scheduler)
172*/
173
174#endif /* _EVENT_QUEUE_H_ */
Definition: event_data_objects.h:80
@ DISABLED
Definition: event_parse_data.h:50
Definition: event_data_objects.h:55
Definition: event_data_objects.h:96
my_time_t m_execute_at
Definition: event_data_objects.h:103
int m_status
Definition: event_data_objects.h:99
Queue of active events awaiting execution.
Definition: event_queue.h:102
uint mutex_last_unlocked_at_line
Definition: event_queue.h:161
bool waiting_on_cond
Definition: event_queue.h:168
mysql_cond_t COND_queue_state
Definition: event_queue.h:149
bool mutex_queue_data_locked
Definition: event_queue.h:166
uint mutex_last_locked_at_line
Definition: event_queue.h:160
my_time_t next_activation_at
Definition: event_queue.h:158
std::atomic< const char * > mutex_last_attempted_lock_in_func
Definition: event_queue.h:165
const char * mutex_last_locked_in_func
Definition: event_queue.h:163
const char * mutex_last_unlocked_in_func
Definition: event_queue.h:164
std::atomic< uint > mutex_last_attempted_lock_at_line
Definition: event_queue.h:162
mysql_mutex_t LOCK_event_queue
Definition: event_queue.h:148
std::atomic< bool > mutex_queue_data_attempting_lock
Definition: event_queue.h:167
Priority_queue< Event_queue_element *, std::vector< Event_queue_element *, Malloc_allocator< Event_queue_element * > >, Event_queue_less > queue
Definition: event_queue.h:156
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
Implements a priority queue using a vector-based max-heap.
Definition: priority_queue.h:104
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
bool get_top_for_execution_if_time(THD *thd, Event_queue_element_for_exec **event_name)
Definition: event_queue.cc:531
void update_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name, Event_queue_element *new_element)
Definition: event_queue.cc:205
void lock_data(const char *func, uint line)
Definition: event_queue.cc:658
Event_queue()
Definition: event_queue.cc:76
void recalculate_activation_times(THD *thd)
Definition: event_queue.cc:376
void drop_matching_events(LEX_CSTRING pattern, bool(*)(LEX_CSTRING, Event_basic *))
Definition: event_queue.cc:277
void find_n_remove_event(LEX_CSTRING db, LEX_CSTRING name)
Definition: event_queue.cc:348
void cond_wait(THD *thd, struct timespec *abstime, const PSI_stage_info *stage, const char *src_func, const char *src_file, uint src_line)
Definition: event_queue.cc:705
void empty_queue()
Definition: event_queue.cc:476
PSI_cond_key key_COND_queue_state
Definition: events.cc:1030
~Event_queue()
Definition: event_queue.cc:94
void dbug_dump_queue(time_t now)
Definition: event_queue.cc:494
void unlock_data(const char *func, uint line)
Definition: event_queue.cc:684
void drop_schema_events(LEX_CSTRING schema)
Definition: event_queue.cc:328
bool init_queue()
Definition: event_queue.cc:116
PSI_mutex_key key_LOCK_event_queue
Definition: events.cc:1020
bool create_event(THD *thd, Event_queue_element *new_element, bool *created)
Adds an event to the queue.
Definition: event_queue.cc:169
void dump_internal_status()
Definition: event_queue.cc:745
void deinit_queue()
Definition: event_queue.cc:143
void drop_event(THD *thd, LEX_CSTRING dbname, LEX_CSTRING name)
Definition: event_queue.cc:246
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:44
unsigned int PSI_mutex_key
Instrumented mutex key.
Definition: psi_mutex_bits.h:52
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Interface for low level time utilities.
int64_t my_time_t
Portable time_t replacement.
Definition: my_time_t.h:32
Instrumentation helpers for conditions.
ABI for instrumented mutexes.
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2876
Performance schema instrumentation interface.
Instrumentation helpers for mutexes.
Performance schema instrumentation interface.
case opt name
Definition: sslopt-case.h:29
Definition: event_queue.h:61
int event_queue_element_compare_q(Event_queue_element *left, Event_queue_element *right)
Compares the execute_at members of two Event_queue_element instances.
Definition: event_queue.h:85
bool operator()(Event_queue_element *left, Event_queue_element *right)
Maps compare function to strict weak ordering required by Priority_queue.
Definition: event_queue.h:63
Definition: mysql_lex_string.h:40
Stage instrument information.
Definition: psi_stage_bits.h:74
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
Include file for Sun RPC to compile out of the box.