9
3

waiting_threads.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (C) 2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #ifndef _waiting_threads_h
  13. #define _waiting_threads_h
  14. #include <my_global.h>
  15. #include <my_sys.h>
  16. #include <lf.h>
  17. C_MODE_START
  18. typedef struct st_wt_resource_id WT_RESOURCE_ID;
  19. typedef struct st_wt_resource WT_RESOURCE;
  20. typedef struct st_wt_resource_type {
  21. my_bool (*compare)(const void *a, const void *b);
  22. const void *(*make_key)(const WT_RESOURCE_ID *id, uint *len); /* not used */
  23. } WT_RESOURCE_TYPE;
  24. struct st_wt_resource_id {
  25. ulonglong value;
  26. const WT_RESOURCE_TYPE *type;
  27. };
  28. /* the below differs from sizeof(WT_RESOURCE_ID) by the amount of padding */
  29. #define sizeof_WT_RESOURCE_ID (sizeof(ulonglong)+sizeof(void*))
  30. #define WT_WAIT_STATS 24
  31. #define WT_CYCLE_STATS 32
  32. extern ulonglong wt_wait_table[WT_WAIT_STATS];
  33. extern uint32 wt_wait_stats[WT_WAIT_STATS+1];
  34. extern uint32 wt_cycle_stats[2][WT_CYCLE_STATS+1];
  35. extern uint32 wt_success_stats;
  36. typedef struct st_wt_thd {
  37. /*
  38. XXX
  39. there's no protection (mutex) against concurrent access of the
  40. dynarray below. it is assumed that a caller will have it anyway
  41. (not to protect this array but to protect its own - caller's -
  42. data structures), and we'll get it for free. A caller needs to
  43. ensure that a blocker won't release a resource before a blocked
  44. thread starts waiting, which is usually done with a mutex.
  45. If the above assumption is wrong, we'll need to add a mutex here.
  46. */
  47. DYNAMIC_ARRAY my_resources;
  48. /*
  49. 'waiting_for' is modified under waiting_for->lock, and only by thd itself
  50. 'waiting_for' is read lock-free (using pinning protocol), but a thd object
  51. can read its own 'waiting_for' without any locks or tricks.
  52. */
  53. WT_RESOURCE *waiting_for;
  54. LF_PINS *pins;
  55. /* pointers to values */
  56. const ulong *timeout_short;
  57. const ulong *deadlock_search_depth_short;
  58. const ulong *timeout_long;
  59. const ulong *deadlock_search_depth_long;
  60. /*
  61. weight relates to the desirability of a transaction being killed if it's
  62. part of a deadlock. In a deadlock situation transactions with lower weights
  63. are killed first.
  64. Examples of using the weight to implement different selection strategies:
  65. 1. Latest
  66. Keep all weights equal.
  67. 2. Random
  68. Assight weights at random.
  69. (variant: modify a weight randomly before every lock request)
  70. 3. Youngest
  71. Set weight to -NOW()
  72. 4. Minimum locks
  73. count locks granted in your lock manager, store the value as a weight
  74. 5. Minimum work
  75. depends on the definition of "work". For example, store the number
  76. of rows modifies in this transaction (or a length of REDO log for a
  77. transaction) as a weight.
  78. It is only statistically relevant and is not protected by any locks.
  79. */
  80. ulong volatile weight;
  81. /*
  82. 'killed' is indirectly protected by waiting_for->lock because
  83. a killed thread needs to clear its 'waiting_for' and thus needs a lock.
  84. That is a thread needs an exclusive lock to read 'killed' reliably.
  85. But other threads may change 'killed' from 0 to 1, a shared
  86. lock is enough for that.
  87. */
  88. my_bool killed;
  89. #ifndef DBUG_OFF
  90. const char *name;
  91. #endif
  92. } WT_THD;
  93. #define WT_TIMEOUT ETIMEDOUT
  94. #define WT_OK 0
  95. #define WT_DEADLOCK -1
  96. #define WT_DEPTH_EXCEEDED -2
  97. #define WT_FREE_TO_GO -3
  98. void wt_init(void);
  99. void wt_end(void);
  100. void wt_thd_lazy_init(WT_THD *, const ulong *, const ulong *, const ulong *, const ulong *);
  101. void wt_thd_destroy(WT_THD *);
  102. int wt_thd_will_wait_for(WT_THD *, WT_THD *, const WT_RESOURCE_ID *);
  103. int wt_thd_cond_timedwait(WT_THD *, mysql_mutex_t *);
  104. void wt_thd_release(WT_THD *, const WT_RESOURCE_ID *);
  105. #define wt_thd_release_all(THD) wt_thd_release((THD), 0)
  106. my_bool wt_resource_id_memcmp(const void *, const void *);
  107. C_MODE_END
  108. #endif