service_thd_specifics.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef MYSQL_SERVICE_THD_SPECIFICS_INCLUDED
  2. /* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. /**
  14. @file
  15. THD specific for plugin(s)
  16. This API provides pthread_getspecific like functionality to plugin authors.
  17. This is a functional alternative to the declarative MYSQL_THDVAR
  18. A plugin should at init call thd_key_create that create a key that
  19. will have storage in each THD. The key should be used by all threads
  20. and can be used concurrently from all threads.
  21. A plugin should at deinit call thd_key_delete.
  22. Alternatively, a plugin can use thd_key_create_from_var(K,V) to create
  23. a key that corresponds to a named MYSQL_THDVAR variable.
  24. This API is also safe when using pool-of-threads in which case
  25. pthread_getspecific is not, because the actual OS thread may change.
  26. @note
  27. Normally one should prefer MYSQL_THDVAR declarative API.
  28. The benefits are:
  29. - It supports typed variables (int, char*, enum, etc), not only void*.
  30. - The memory allocated for MYSQL_THDVAR is free'd automatically
  31. (if PLUGIN_VAR_MEMALLOC is specified).
  32. - Continuous loading and unloading of the same plugin does not allocate
  33. memory for same variables over and over again.
  34. An example of using MYSQL_THDVAR for a thd local storage:
  35. MYSQL_THDVAR_STR(my_tls,
  36. PLUGIN_VAR_MEMALLOC | PLUGIN_VAR_NOSYSVAR | PLUGIN_VAR_NOCMDOPT,
  37. "thd local storage example", 0, 0, 0);
  38. */
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. typedef int MYSQL_THD_KEY_T;
  43. extern struct thd_specifics_service_st {
  44. int (*thd_key_create_func)(MYSQL_THD_KEY_T *key);
  45. void (*thd_key_delete_func)(MYSQL_THD_KEY_T *key);
  46. void *(*thd_getspecific_func)(MYSQL_THD thd, MYSQL_THD_KEY_T key);
  47. int (*thd_setspecific_func)(MYSQL_THD thd, MYSQL_THD_KEY_T key, void *value);
  48. } *thd_specifics_service;
  49. #define thd_key_create_from_var(K, V) do { *(K)= MYSQL_SYSVAR_NAME(V).offset; } while(0)
  50. #ifdef MYSQL_DYNAMIC_PLUGIN
  51. #define thd_key_create(K) (thd_specifics_service->thd_key_create_func(K))
  52. #define thd_key_delete(K) (thd_specifics_service->thd_key_delete_func(K))
  53. #define thd_getspecific(T, K) (thd_specifics_service->thd_getspecific_func(T, K))
  54. #define thd_setspecific(T, K, V) (thd_specifics_service->thd_setspecific_func(T, K, V))
  55. #else
  56. /**
  57. * create THD specific storage
  58. * @return 0 on success
  59. * else errno is returned
  60. */
  61. int thd_key_create(MYSQL_THD_KEY_T *key);
  62. /**
  63. * delete THD specific storage
  64. */
  65. void thd_key_delete(MYSQL_THD_KEY_T *key);
  66. /**
  67. * get/set thd specific storage
  68. * - first time this is called from a thread it will return 0
  69. * - this call is thread-safe in that different threads may call this
  70. * simultaneously if operating on different THDs.
  71. * - this call acquires no mutexes and is implemented as an array lookup
  72. */
  73. void* thd_getspecific(MYSQL_THD thd, MYSQL_THD_KEY_T key);
  74. int thd_setspecific(MYSQL_THD thd, MYSQL_THD_KEY_T key, void *value);
  75. #endif
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79. #define MYSQL_SERVICE_THD_SPECIFICS_INCLUDED
  80. #endif