TSRM.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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@zend.com> |
  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. #ifdef TSRM_WIN32
  21. # ifdef TSRM_EXPORTS
  22. # define TSRM_API __declspec(dllexport)
  23. # else
  24. # define TSRM_API __declspec(dllimport)
  25. # endif
  26. #elif defined(__GNUC__) && __GNUC__ >= 4
  27. # define TSRM_API __attribute__ ((visibility("default")))
  28. #else
  29. # define TSRM_API
  30. #endif
  31. #ifdef _WIN64
  32. typedef __int64 tsrm_intptr_t;
  33. typedef unsigned __int64 tsrm_uintptr_t;
  34. #else
  35. typedef long tsrm_intptr_t;
  36. typedef unsigned long tsrm_uintptr_t;
  37. #endif
  38. /* Only compile multi-threading functions if we're in ZTS mode */
  39. #ifdef ZTS
  40. #ifdef TSRM_WIN32
  41. # ifndef TSRM_INCLUDE_FULL_WINDOWS_HEADERS
  42. # define WIN32_LEAN_AND_MEAN
  43. # endif
  44. # include <windows.h>
  45. # include <shellapi.h>
  46. #elif defined(GNUPTH)
  47. # include <pth.h>
  48. #elif defined(PTHREADS)
  49. # include <pthread.h>
  50. #elif defined(TSRM_ST)
  51. # include <st.h>
  52. #elif defined(BETHREADS)
  53. #include <kernel/OS.h>
  54. #include <TLS.h>
  55. #endif
  56. typedef int ts_rsrc_id;
  57. /* Define THREAD_T and MUTEX_T */
  58. #ifdef TSRM_WIN32
  59. # define THREAD_T DWORD
  60. # define MUTEX_T CRITICAL_SECTION *
  61. #elif defined(GNUPTH)
  62. # define THREAD_T pth_t
  63. # define MUTEX_T pth_mutex_t *
  64. #elif defined(PTHREADS)
  65. # define THREAD_T pthread_t
  66. # define MUTEX_T pthread_mutex_t *
  67. #elif defined(NSAPI)
  68. # define THREAD_T SYS_THREAD
  69. # define MUTEX_T CRITICAL
  70. #elif defined(PI3WEB)
  71. # define THREAD_T PIThread *
  72. # define MUTEX_T PISync *
  73. #elif defined(TSRM_ST)
  74. # define THREAD_T st_thread_t
  75. # define MUTEX_T st_mutex_t
  76. #elif defined(BETHREADS)
  77. # define THREAD_T thread_id
  78. typedef struct {
  79. sem_id sem;
  80. int32 ben;
  81. } beos_ben;
  82. # define MUTEX_T beos_ben *
  83. #endif
  84. #ifdef HAVE_SIGNAL_H
  85. #include <signal.h>
  86. #endif
  87. typedef void (*ts_allocate_ctor)(void *, void ***);
  88. typedef void (*ts_allocate_dtor)(void *, void ***);
  89. #define THREAD_HASH_OF(thr,ts) (unsigned long)thr%(unsigned long)ts
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93. /* startup/shutdown */
  94. TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename);
  95. TSRM_API void tsrm_shutdown(void);
  96. /* allocates a new thread-safe-resource id */
  97. TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
  98. /* fetches the requested resource for the current thread */
  99. TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
  100. #define ts_resource(id) ts_resource_ex(id, NULL)
  101. /* frees all resources allocated for the current thread */
  102. TSRM_API void ts_free_thread(void);
  103. /* frees all resources allocated for all threads except current */
  104. void ts_free_worker_threads(void);
  105. /* deallocates all occurrences of a given id */
  106. TSRM_API void ts_free_id(ts_rsrc_id id);
  107. /* Debug support */
  108. #define TSRM_ERROR_LEVEL_ERROR 1
  109. #define TSRM_ERROR_LEVEL_CORE 2
  110. #define TSRM_ERROR_LEVEL_INFO 3
  111. typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id, void ***tsrm_ls);
  112. typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id, void ***tsrm_ls);
  113. TSRM_API int tsrm_error(int level, const char *format, ...);
  114. TSRM_API void tsrm_error_set(int level, char *debug_filename);
  115. /* utility functions */
  116. TSRM_API THREAD_T tsrm_thread_id(void);
  117. TSRM_API MUTEX_T tsrm_mutex_alloc(void);
  118. TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
  119. TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
  120. TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
  121. #ifdef HAVE_SIGPROCMASK
  122. TSRM_API int tsrm_sigmask(int how, const sigset_t *set, sigset_t *oldset);
  123. #endif
  124. TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
  125. TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
  126. /* these 3 APIs should only be used by people that fully understand the threading model
  127. * used by PHP/Zend and the selected SAPI. */
  128. TSRM_API void *tsrm_new_interpreter_context(void);
  129. TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
  130. TSRM_API void tsrm_free_interpreter_context(void *context);
  131. #define TSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)
  132. #define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)
  133. #define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)
  134. #define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx
  135. #define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls
  136. #define TSRMG(id, type, element) (((type) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
  137. #define TSRMLS_D void ***tsrm_ls
  138. #define TSRMLS_DC , TSRMLS_D
  139. #define TSRMLS_C tsrm_ls
  140. #define TSRMLS_CC , TSRMLS_C
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #else /* non ZTS */
  145. #define TSRMLS_FETCH()
  146. #define TSRMLS_FETCH_FROM_CTX(ctx)
  147. #define TSRMLS_SET_CTX(ctx)
  148. #define TSRMLS_D void
  149. #define TSRMLS_DC
  150. #define TSRMLS_C
  151. #define TSRMLS_CC
  152. #endif /* ZTS */
  153. #endif /* TSRM_H */