diff options
Diffstat (limited to 'sql/event_scheduler.h')
-rw-r--r-- | sql/event_scheduler.h | 212 |
1 files changed, 41 insertions, 171 deletions
diff --git a/sql/event_scheduler.h b/sql/event_scheduler.h index 5ae310bab2a..dffaf8c056c 100644 --- a/sql/event_scheduler.h +++ b/sql/event_scheduler.h @@ -16,235 +16,105 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -class Event_timed; +class Event_queue; +class Event_job_data; -class THD; -typedef bool * (*event_timed_identifier_comparator)(Event_timed*, Event_timed*); +void +pre_init_event_thread(THD* thd); -int -events_init(); +bool +post_init_event_thread(THD* thd); void -events_shutdown(); +deinit_event_thread(THD *thd); class Event_scheduler { public: - /* Return codes */ - enum enum_error_code - { - OP_OK= 0, - OP_NOT_RUNNING, - OP_CANT_KILL, - OP_CANT_INIT, - OP_DISABLED_EVENT, - OP_LOAD_ERROR, - OP_ALREADY_EXISTS - }; - - enum enum_state - { - UNINITIALIZED= 0, - INITIALIZED, - COMMENCING, - CANTSTART, - RUNNING, - SUSPENDED, - IN_SHUTDOWN - }; - - enum enum_suspend_or_resume - { - SUSPEND= 1, - RESUME= 2 - }; - - /* Singleton access */ - static Event_scheduler* - get_instance(); - - /* Methods for queue management follow */ - - enum enum_error_code - create_event(THD *thd, Event_timed *et, bool check_existence); - - enum enum_error_code - update_event(THD *thd, Event_timed *et, LEX_STRING *new_schema, - LEX_STRING *new_name); - - bool - drop_event(THD *thd, Event_timed *et); - - - int - drop_schema_events(THD *thd, LEX_STRING *schema); - - int - drop_user_events(THD *thd, LEX_STRING *definer, uint *dropped_num) - { DBUG_ASSERT(0); return 0;} - - uint - events_count(); + Event_scheduler():state(UNINITIALIZED){} + ~Event_scheduler(){} /* State changing methods follow */ bool start(); - enum enum_error_code - stop(); - bool - start_suspended(); + stop(); + /* + Need to be public because has to be called from the function + passed to pthread_create. + */ bool run(THD *thd); - enum enum_error_code - suspend_or_resume(enum enum_suspend_or_resume action); - - bool - init(); + void + init_scheduler(Event_queue *queue); void - destroy(); + deinit_scheduler(); - static void + void init_mutexes(); - - static void - destroy_mutexes(); void - report_error_during_start(); + deinit_mutexes(); /* Information retrieving methods follow */ - - enum enum_state - get_state(); - bool - initialized(); - - static int - dump_internal_status(THD *thd); + is_running(); - static bool - check_system_tables(THD *thd); + void + dump_internal_status(); private: - Event_timed * - find_event(Event_timed *etn, bool remove_from_q); - uint workers_count(); - bool - is_running_or_suspended(); /* helper functions */ bool - execute_top(THD *thd); + execute_top(THD *thd, Event_job_data *job_data); + /* helper functions for working with mutexes & conditionals */ void - clean_queue(THD *thd); - - void - stop_all_running_events(THD *thd); - - enum enum_error_code - load_named_event(THD *thd, Event_timed *etn, Event_timed **etn_new); - - int - load_events_from_db(THD *thd); + lock_data(const char *func, uint line); void - drop_matching_events(THD *thd, LEX_STRING *pattern, - bool (*)(Event_timed *,LEX_STRING *)); + unlock_data(const char *func, uint line); - bool - check_n_suspend_if_needed(THD *thd); + void + cond_wait(THD *thd, struct timespec *abstime, const char* msg, + const char *func, uint line); - bool - check_n_wait_for_non_empty_queue(THD *thd); + pthread_mutex_t LOCK_scheduler_state; - /* Singleton DP is used */ - Event_scheduler(); - - enum enum_cond_vars + enum enum_state { - COND_NONE= -1, - /* - COND_new_work is a conditional used to signal that there is a change - of the queue that should inform the executor thread that new event should - be executed sooner than previously expected, because of add/replace event. - */ - COND_new_work= 0, - /* - COND_started is a conditional used to synchronize the thread in which - ::start() was called and the spawned thread. ::start() spawns a new thread - and then waits on COND_started but also checks when awaken that `state` is - either RUNNING or CANTSTART. Then it returns back. - */ - COND_started_or_stopped, - /* - Conditional used for signalling from the scheduler thread back to the - thread that calls ::suspend() or ::resume. Synchronizing the calls. - */ - COND_suspend_or_resume, - /* Must be always last */ - COND_LAST + UNINITIALIZED = 0, + INITIALIZED, + RUNNING, + STOPPING }; - /* Singleton instance */ - static Event_scheduler singleton; - - /* This is the current status of the life-cycle of the manager. */ + /* This is the current status of the life-cycle of the scheduler. */ enum enum_state state; - /* Set to start the scheduler in suspended state */ - bool start_scheduler_suspended; - - /* - LOCK_scheduler_data is the mutex which protects the access to the - manager's queue as well as used when signalling COND_new_work, - COND_started and COND_shutdown. - */ - pthread_mutex_t LOCK_scheduler_data; - - /* - Holds the thread id of the executor thread or 0 if the executor is not - running. It is used by ::shutdown() to know which thread to kill with - kill_one_thread(). The latter wake ups a thread if it is waiting on a - conditional variable and sets thd->killed to non-zero. - */ - ulong thread_id; + THD *scheduler_thd; - pthread_cond_t cond_vars[COND_LAST]; - static const char * const cond_vars_names[COND_LAST]; + pthread_cond_t COND_state; - /* The MEM_ROOT of the object */ - MEM_ROOT scheduler_root; + Event_queue *queue; - /* The sorted queue with the Event_timed objects */ - QUEUE queue; - uint mutex_last_locked_at_line; uint mutex_last_unlocked_at_line; const char* mutex_last_locked_in_func; const char* mutex_last_unlocked_in_func; - enum enum_cond_vars cond_waiting_on; bool mutex_scheduler_data_locked; + bool waiting_on_cond; - /* helper functions for working with mutexes & conditionals */ - void - lock_data(const char *func, uint line); - - void - unlock_data(const char *func, uint line); - - int - cond_wait(enum enum_cond_vars, pthread_mutex_t *mutex); + ulonglong started_events; private: /* Prevent use of these */ |