my_thread_local.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
  18. #ifndef MY_THREAD_LOCAL_INCLUDED
  19. #define MY_THREAD_LOCAL_INCLUDED
  20. #ifndef _WIN32
  21. #include <pthread.h>
  22. #endif
  23. struct _db_code_state_;
  24. typedef uint32 my_thread_id;
  25. C_MODE_START
  26. #ifdef _WIN32
  27. typedef DWORD thread_local_key_t;
  28. #else
  29. typedef pthread_key_t thread_local_key_t;
  30. #endif
  31. static inline int my_create_thread_local_key(thread_local_key_t *key,
  32. void (*destructor)(void *))
  33. {
  34. #ifdef _WIN32
  35. *key= TlsAlloc();
  36. return (*key == TLS_OUT_OF_INDEXES);
  37. #else
  38. return pthread_key_create(key, destructor);
  39. #endif
  40. }
  41. static inline int my_delete_thread_local_key(thread_local_key_t key)
  42. {
  43. #ifdef _WIN32
  44. return !TlsFree(key);
  45. #else
  46. return pthread_key_delete(key);
  47. #endif
  48. }
  49. static inline void* my_get_thread_local(thread_local_key_t key)
  50. {
  51. #ifdef _WIN32
  52. return TlsGetValue(key);
  53. #else
  54. return pthread_getspecific(key);
  55. #endif
  56. }
  57. static inline int my_set_thread_local(thread_local_key_t key,
  58. void *value)
  59. {
  60. #ifdef _WIN32
  61. return !TlsSetValue(key, value);
  62. #else
  63. return pthread_setspecific(key, value);
  64. #endif
  65. }
  66. /**
  67. Retrieve the MySQL thread-local storage variant of errno.
  68. */
  69. int my_errno();
  70. /**
  71. Set the MySQL thread-local storage variant of errno.
  72. */
  73. void set_my_errno(int my_errno);
  74. #ifdef _WIN32
  75. /*
  76. thr_winerr is used for returning the original OS error-code in Windows,
  77. my_osmaperr() returns EINVAL for all unknown Windows errors, hence we
  78. preserve the original Windows Error code in thr_winerr.
  79. */
  80. int thr_winerr();
  81. void set_thr_winerr(int winerr);
  82. #endif
  83. #ifndef DBUG_OFF
  84. /* Return pointer to DBUG for holding current state */
  85. struct _db_code_state_ **my_thread_var_dbug();
  86. my_thread_id my_thread_var_id();
  87. void set_my_thread_var_id(my_thread_id id);
  88. #endif
  89. C_MODE_END
  90. #endif // MY_THREAD_LOCAL_INCLUDED