TSRM.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Thread Safe Resource Manager |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1999-2011, Andi Gutmans, Sascha Schumann, Zeev Suraski |
  6. | This source file is subject to the TSRM license, that is bundled |
  7. | with this package in the file LICENSE |
  8. +----------------------------------------------------------------------+
  9. | Authors: Zeev Suraski <zeev@php.net> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifndef TSRM_H
  13. #define TSRM_H
  14. #if !defined(__CYGWIN__) && defined(WIN32)
  15. # define TSRM_WIN32
  16. # include "tsrm_config.w32.h"
  17. #else
  18. # include <tsrm_config.h>
  19. #endif
  20. #include "main/php_stdint.h"
  21. #ifdef TSRM_WIN32
  22. # ifdef TSRM_EXPORTS
  23. # define TSRM_API __declspec(dllexport)
  24. # else
  25. # define TSRM_API __declspec(dllimport)
  26. # endif
  27. #elif defined(__GNUC__) && __GNUC__ >= 4
  28. # define TSRM_API __attribute__ ((visibility("default")))
  29. #else
  30. # define TSRM_API
  31. #endif
  32. typedef intptr_t tsrm_intptr_t;
  33. typedef uintptr_t tsrm_uintptr_t;
  34. /* Only compile multi-threading functions if we're in ZTS mode */
  35. #ifdef ZTS
  36. #ifdef TSRM_WIN32
  37. # ifndef TSRM_INCLUDE_FULL_WINDOWS_HEADERS
  38. # define WIN32_LEAN_AND_MEAN
  39. # endif
  40. # include <windows.h>
  41. # include <shellapi.h>
  42. #elif defined(GNUPTH)
  43. # include <pth.h>
  44. #elif defined(PTHREADS)
  45. # include <pthread.h>
  46. #elif defined(TSRM_ST)
  47. # include <st.h>
  48. #elif defined(BETHREADS)
  49. #include <kernel/OS.h>
  50. #include <TLS.h>
  51. #endif
  52. typedef int ts_rsrc_id;
  53. /* Define THREAD_T and MUTEX_T */
  54. #ifdef TSRM_WIN32
  55. # define THREAD_T DWORD
  56. # define MUTEX_T CRITICAL_SECTION *
  57. #elif defined(GNUPTH)
  58. # define THREAD_T pth_t
  59. # define MUTEX_T pth_mutex_t *
  60. #elif defined(PTHREADS)
  61. # define THREAD_T pthread_t
  62. # define MUTEX_T pthread_mutex_t *
  63. #elif defined(TSRM_ST)
  64. # define THREAD_T st_thread_t
  65. # define MUTEX_T st_mutex_t
  66. #endif
  67. #ifdef HAVE_SIGNAL_H
  68. #include <signal.h>
  69. #endif
  70. typedef void (*ts_allocate_ctor)(void *);
  71. typedef void (*ts_allocate_dtor)(void *);
  72. #define THREAD_HASH_OF(thr,ts) (unsigned long)thr%(unsigned long)ts
  73. #ifdef __cplusplus
  74. extern "C" {
  75. #endif
  76. /* startup/shutdown */
  77. TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
  78. TSRM_API void tsrm_shutdown(void);
  79. /* allocates a new thread-safe-resource id */
  80. TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
  81. /* fetches the requested resource for the current thread */
  82. TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
  83. #define ts_resource(id) ts_resource_ex(id, NULL)
  84. /* frees all resources allocated for the current thread */
  85. TSRM_API void ts_free_thread(void);
  86. /* frees all resources allocated for all threads except current */
  87. void ts_free_worker_threads(void);
  88. /* deallocates all occurrences of a given id */
  89. TSRM_API void ts_free_id(ts_rsrc_id id);
  90. /* Debug support */
  91. #define TSRM_ERROR_LEVEL_ERROR 1
  92. #define TSRM_ERROR_LEVEL_CORE 2
  93. #define TSRM_ERROR_LEVEL_INFO 3
  94. typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id);
  95. typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id);
  96. typedef void (*tsrm_shutdown_func_t)(void);
  97. TSRM_API int tsrm_error(int level, const char *format, ...);
  98. TSRM_API void tsrm_error_set(int level, char *debug_filename);
  99. /* utility functions */
  100. TSRM_API THREAD_T tsrm_thread_id(void);
  101. TSRM_API MUTEX_T tsrm_mutex_alloc(void);
  102. TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
  103. TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
  104. TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
  105. #ifdef HAVE_SIGPROCMASK
  106. TSRM_API int tsrm_sigmask(int how, const sigset_t *set, sigset_t *oldset);
  107. #endif
  108. TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
  109. TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
  110. TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);
  111. /* these 3 APIs should only be used by people that fully understand the threading model
  112. * used by PHP/Zend and the selected SAPI. */
  113. TSRM_API void *tsrm_new_interpreter_context(void);
  114. TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
  115. TSRM_API void tsrm_free_interpreter_context(void *context);
  116. TSRM_API void *tsrm_get_ls_cache(void);
  117. TSRM_API uint8_t tsrm_is_main_thread(void);
  118. TSRM_API const char *tsrm_api_name(void);
  119. #if defined(__cplusplus) && __cplusplus > 199711L
  120. # define TSRM_TLS thread_local
  121. #else
  122. # ifdef TSRM_WIN32
  123. # define TSRM_TLS __declspec(thread)
  124. # else
  125. # define TSRM_TLS __thread
  126. # endif
  127. #endif
  128. #define TSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)
  129. #define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)
  130. #define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
  131. #define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_get_ls_cache()
  132. #define TSRMG(id, type, element) (TSRMG_BULK(id, type)->element)
  133. #define TSRMG_BULK(id, type) ((type) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(id)])
  134. #define TSRMG_STATIC(id, type, element) (TSRMG_BULK_STATIC(id, type)->element)
  135. #define TSRMG_BULK_STATIC(id, type) ((type) (*((void ***) TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(id)])
  136. #define TSRMLS_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE;
  137. #define TSRMLS_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE = NULL;
  138. #define TSRMLS_CACHE_UPDATE() TSRMLS_CACHE = tsrm_get_ls_cache()
  139. #define TSRMLS_CACHE _tsrm_ls_cache
  140. /* BC only */
  141. #define TSRMLS_D void
  142. #define TSRMLS_DC
  143. #define TSRMLS_C
  144. #define TSRMLS_CC
  145. #define TSRMLS_FETCH()
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #else /* non ZTS */
  150. #define TSRMLS_FETCH()
  151. #define TSRMLS_FETCH_FROM_CTX(ctx)
  152. #define TSRMLS_SET_CTX(ctx)
  153. #define TSRMG_STATIC(id, type, element)
  154. #define TSRMLS_CACHE_EXTERN()
  155. #define TSRMLS_CACHE_DEFINE()
  156. #define TSRMLS_CACHE_UPDATE()
  157. #define TSRMLS_CACHE
  158. #define TSRM_TLS
  159. /* BC only */
  160. #define TSRMLS_D void
  161. #define TSRMLS_DC
  162. #define TSRMLS_C
  163. #define TSRMLS_CC
  164. #endif /* ZTS */
  165. #endif /* TSRM_H */
  166. /*
  167. * Local variables:
  168. * tab-width: 4
  169. * c-basic-offset: 4
  170. * End:
  171. * vim600: sw=4 ts=4 fdm=marker
  172. * vim<600: sw=4 ts=4
  173. */