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