You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.6 KiB

  1. #ifndef _EVENT_SCHEDULER_H_
  2. #define _EVENT_SCHEDULER_H_
  3. /* Copyright (C) 2004-2006 MySQL AB
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  15. class Event_timed;
  16. class THD;
  17. typedef bool * (*event_timed_identifier_comparator)(Event_timed*, Event_timed*);
  18. int
  19. events_init();
  20. void
  21. events_shutdown();
  22. class Event_scheduler
  23. {
  24. public:
  25. /* Return codes */
  26. enum enum_error_code
  27. {
  28. OP_OK= 0,
  29. OP_NOT_RUNNING,
  30. OP_CANT_KILL,
  31. OP_CANT_INIT,
  32. OP_DISABLED_EVENT,
  33. OP_LOAD_ERROR,
  34. OP_ALREADY_EXISTS
  35. };
  36. enum enum_state
  37. {
  38. UNINITIALIZED= 0,
  39. INITIALIZED,
  40. COMMENCING,
  41. CANTSTART,
  42. RUNNING,
  43. SUSPENDED,
  44. IN_SHUTDOWN
  45. };
  46. enum enum_suspend_or_resume
  47. {
  48. SUSPEND= 1,
  49. RESUME= 2
  50. };
  51. /* Singleton access */
  52. static Event_scheduler*
  53. get_instance();
  54. /* Methods for queue management follow */
  55. enum enum_error_code
  56. create_event(THD *thd, Event_timed *et, bool check_existence);
  57. enum enum_error_code
  58. update_event(THD *thd, Event_timed *et, LEX_STRING *new_schema,
  59. LEX_STRING *new_name);
  60. bool
  61. drop_event(THD *thd, Event_timed *et);
  62. int
  63. drop_schema_events(THD *thd, LEX_STRING *schema);
  64. int
  65. drop_user_events(THD *thd, LEX_STRING *definer, uint *dropped_num)
  66. { DBUG_ASSERT(0); return 0;}
  67. uint
  68. events_count();
  69. /* State changing methods follow */
  70. bool
  71. start();
  72. enum enum_error_code
  73. stop();
  74. bool
  75. start_suspended();
  76. bool
  77. run(THD *thd);
  78. enum enum_error_code
  79. suspend_or_resume(enum enum_suspend_or_resume action);
  80. bool
  81. init();
  82. void
  83. destroy();
  84. static void
  85. init_mutexes();
  86. static void
  87. destroy_mutexes();
  88. void
  89. report_error_during_start();
  90. /* Information retrieving methods follow */
  91. enum enum_state
  92. get_state();
  93. bool
  94. initialized();
  95. static int
  96. dump_internal_status(THD *thd);
  97. static bool
  98. check_system_tables(THD *thd);
  99. private:
  100. Event_timed *
  101. find_event(Event_timed *etn, bool remove_from_q);
  102. uint
  103. workers_count();
  104. bool
  105. is_running_or_suspended();
  106. /* helper functions */
  107. bool
  108. execute_top(THD *thd);
  109. void
  110. clean_queue(THD *thd);
  111. void
  112. stop_all_running_events(THD *thd);
  113. enum enum_error_code
  114. load_named_event(THD *thd, Event_timed *etn, Event_timed **etn_new);
  115. int
  116. load_events_from_db(THD *thd);
  117. void
  118. drop_matching_events(THD *thd, LEX_STRING *pattern,
  119. bool (*)(Event_timed *,LEX_STRING *));
  120. bool
  121. check_n_suspend_if_needed(THD *thd);
  122. bool
  123. check_n_wait_for_non_empty_queue(THD *thd);
  124. /* Singleton DP is used */
  125. Event_scheduler();
  126. enum enum_cond_vars
  127. {
  128. COND_NONE= -1,
  129. /*
  130. COND_new_work is a conditional used to signal that there is a change
  131. of the queue that should inform the executor thread that new event should
  132. be executed sooner than previously expected, because of add/replace event.
  133. */
  134. COND_new_work= 0,
  135. /*
  136. COND_started is a conditional used to synchronize the thread in which
  137. ::start() was called and the spawned thread. ::start() spawns a new thread
  138. and then waits on COND_started but also checks when awaken that `state` is
  139. either RUNNING or CANTSTART. Then it returns back.
  140. */
  141. COND_started_or_stopped,
  142. /*
  143. Conditional used for signalling from the scheduler thread back to the
  144. thread that calls ::suspend() or ::resume. Synchronizing the calls.
  145. */
  146. COND_suspend_or_resume,
  147. /* Must be always last */
  148. COND_LAST
  149. };
  150. /* Singleton instance */
  151. static Event_scheduler singleton;
  152. /* This is the current status of the life-cycle of the manager. */
  153. enum enum_state state;
  154. /* Set to start the scheduler in suspended state */
  155. bool start_scheduler_suspended;
  156. /*
  157. LOCK_scheduler_data is the mutex which protects the access to the
  158. manager's queue as well as used when signalling COND_new_work,
  159. COND_started and COND_shutdown.
  160. */
  161. pthread_mutex_t LOCK_scheduler_data;
  162. /*
  163. Holds the thread id of the executor thread or 0 if the executor is not
  164. running. It is used by ::shutdown() to know which thread to kill with
  165. kill_one_thread(). The latter wake ups a thread if it is waiting on a
  166. conditional variable and sets thd->killed to non-zero.
  167. */
  168. ulong thread_id;
  169. pthread_cond_t cond_vars[COND_LAST];
  170. static const char * const cond_vars_names[COND_LAST];
  171. /* The MEM_ROOT of the object */
  172. MEM_ROOT scheduler_root;
  173. /* The sorted queue with the Event_timed objects */
  174. QUEUE queue;
  175. uint mutex_last_locked_at_line;
  176. uint mutex_last_unlocked_at_line;
  177. const char* mutex_last_locked_in_func;
  178. const char* mutex_last_unlocked_in_func;
  179. enum enum_cond_vars cond_waiting_on;
  180. bool mutex_scheduler_data_locked;
  181. /* helper functions for working with mutexes & conditionals */
  182. void
  183. lock_data(const char *func, uint line);
  184. void
  185. unlock_data(const char *func, uint line);
  186. int
  187. cond_wait(enum enum_cond_vars, pthread_mutex_t *mutex);
  188. private:
  189. /* Prevent use of these */
  190. Event_scheduler(const Event_scheduler &);
  191. void operator=(Event_scheduler &);
  192. };
  193. #endif /* _EVENT_SCHEDULER_H_ */