Clone of PhatAC @ https://github.com/floaterxk/PhatAC

my_thread_local.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
  12. #ifndef MY_THREAD_LOCAL_INCLUDED
  13. #define MY_THREAD_LOCAL_INCLUDED
  14. #ifndef _WIN32
  15. #include <pthread.h>
  16. #endif
  17. struct _db_code_state_;
  18. typedef uint32 my_thread_id;
  19. C_MODE_START
  20. #ifdef _WIN32
  21. typedef DWORD thread_local_key_t;
  22. #else
  23. typedef pthread_key_t thread_local_key_t;
  24. #endif
  25. static inline int my_create_thread_local_key(thread_local_key_t *key,
  26. void (*destructor)(void *))
  27. {
  28. #ifdef _WIN32
  29. *key= TlsAlloc();
  30. return (*key == TLS_OUT_OF_INDEXES);
  31. #else
  32. return pthread_key_create(key, destructor);
  33. #endif
  34. }
  35. static inline int my_delete_thread_local_key(thread_local_key_t key)
  36. {
  37. #ifdef _WIN32
  38. return !TlsFree(key);
  39. #else
  40. return pthread_key_delete(key);
  41. #endif
  42. }
  43. static inline void* my_get_thread_local(thread_local_key_t key)
  44. {
  45. #ifdef _WIN32
  46. return TlsGetValue(key);
  47. #else
  48. return pthread_getspecific(key);
  49. #endif
  50. }
  51. static inline int my_set_thread_local(thread_local_key_t key,
  52. void *value)
  53. {
  54. #ifdef _WIN32
  55. return !TlsSetValue(key, value);
  56. #else
  57. return pthread_setspecific(key, value);
  58. #endif
  59. }
  60. /**
  61. Retrieve the MySQL thread-local storage variant of errno.
  62. */
  63. int my_errno();
  64. /**
  65. Set the MySQL thread-local storage variant of errno.
  66. */
  67. void set_my_errno(int my_errno);
  68. #ifdef _WIN32
  69. /*
  70. thr_winerr is used for returning the original OS error-code in Windows,
  71. my_osmaperr() returns EINVAL for all unknown Windows errors, hence we
  72. preserve the original Windows Error code in thr_winerr.
  73. */
  74. int thr_winerr();
  75. void set_thr_winerr(int winerr);
  76. #endif
  77. #ifndef DBUG_OFF
  78. /* Return pointer to DBUG for holding current state */
  79. struct _db_code_state_ **my_thread_var_dbug();
  80. my_thread_id my_thread_var_id();
  81. void set_my_thread_var_id(my_thread_id id);
  82. #endif
  83. C_MODE_END
  84. #endif // MY_THREAD_LOCAL_INCLUDED