xref: /PHP-7.4/TSRM/TSRM.h (revision d63fdced)
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 "Zend/zend_config.w32.h"
19 #else
20 # include "main/php_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 #endif
56 
57 #if SIZEOF_SIZE_T == 4
58 # define TSRM_ALIGNED_SIZE(size) \
59 	(((size) + INT32_C(15)) & ~INT32_C(15))
60 #else
61 # define TSRM_ALIGNED_SIZE(size) \
62 	(((size) + INT64_C(15)) & ~INT64_C(15))
63 #endif
64 
65 typedef int ts_rsrc_id;
66 
67 /* Define THREAD_T and MUTEX_T */
68 #ifdef TSRM_WIN32
69 # define THREAD_T DWORD
70 # define MUTEX_T CRITICAL_SECTION *
71 #elif defined(GNUPTH)
72 # define THREAD_T pth_t
73 # define MUTEX_T pth_mutex_t *
74 #elif defined(PTHREADS)
75 # define THREAD_T pthread_t
76 # define MUTEX_T pthread_mutex_t *
77 #elif defined(TSRM_ST)
78 # define THREAD_T st_thread_t
79 # define MUTEX_T st_mutex_t
80 #endif
81 
82 #include <signal.h>
83 
84 typedef void (*ts_allocate_ctor)(void *);
85 typedef void (*ts_allocate_dtor)(void *);
86 
87 #define THREAD_HASH_OF(thr,ts)  (unsigned long)thr%(unsigned long)ts
88 
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92 
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 
97 /* environ lock API */
98 TSRM_API void tsrm_env_lock();
99 TSRM_API void tsrm_env_unlock();
100 
101 /* allocates a new thread-safe-resource id */
102 TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
103 
104 /* Fast resource in reserved (pre-allocated) space */
105 TSRM_API void tsrm_reserve(size_t size);
106 TSRM_API ts_rsrc_id ts_allocate_fast_id(ts_rsrc_id *rsrc_id, size_t *offset, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor);
107 
108 /* fetches the requested resource for the current thread */
109 TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id);
110 #define ts_resource(id)			ts_resource_ex(id, NULL)
111 
112 /* frees all resources allocated for the current thread */
113 TSRM_API void ts_free_thread(void);
114 
115 /* deallocates all occurrences of a given id */
116 TSRM_API void ts_free_id(ts_rsrc_id id);
117 
118 
119 /* Debug support */
120 #define TSRM_ERROR_LEVEL_ERROR	1
121 #define TSRM_ERROR_LEVEL_CORE	2
122 #define TSRM_ERROR_LEVEL_INFO	3
123 
124 typedef void (*tsrm_thread_begin_func_t)(THREAD_T thread_id);
125 typedef void (*tsrm_thread_end_func_t)(THREAD_T thread_id);
126 typedef void (*tsrm_shutdown_func_t)(void);
127 
128 
129 TSRM_API int tsrm_error(int level, const char *format, ...);
130 TSRM_API void tsrm_error_set(int level, char *debug_filename);
131 
132 /* utility functions */
133 TSRM_API THREAD_T tsrm_thread_id(void);
134 TSRM_API MUTEX_T tsrm_mutex_alloc(void);
135 TSRM_API void tsrm_mutex_free(MUTEX_T mutexp);
136 TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp);
137 TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp);
138 #ifdef HAVE_SIGPROCMASK
139 TSRM_API int tsrm_sigmask(int how, const sigset_t *set, sigset_t *oldset);
140 #endif
141 
142 TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler);
143 TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler);
144 TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler);
145 
146 /* these 3 APIs should only be used by people that fully understand the threading model
147  * used by PHP/Zend and the selected SAPI. */
148 TSRM_API void *tsrm_new_interpreter_context(void);
149 TSRM_API void *tsrm_set_interpreter_context(void *new_ctx);
150 TSRM_API void tsrm_free_interpreter_context(void *context);
151 
152 TSRM_API void *tsrm_get_ls_cache(void);
153 TSRM_API uint8_t tsrm_is_main_thread(void);
154 TSRM_API uint8_t tsrm_is_shutdown(void);
155 TSRM_API const char *tsrm_api_name(void);
156 
157 #ifdef TSRM_WIN32
158 # define TSRM_TLS __declspec(thread)
159 #else
160 # define TSRM_TLS __thread
161 #endif
162 
163 #define TSRM_SHUFFLE_RSRC_ID(rsrc_id)		((rsrc_id)+1)
164 #define TSRM_UNSHUFFLE_RSRC_ID(rsrc_id)		((rsrc_id)-1)
165 
166 #define TSRMLS_FETCH_FROM_CTX(ctx)	void ***tsrm_ls = (void ***) ctx
167 #define TSRMLS_SET_CTX(ctx)		ctx = (void ***) tsrm_get_ls_cache()
168 #define TSRMG(id, type, element)	(TSRMG_BULK(id, type)->element)
169 #define TSRMG_BULK(id, type)	((type) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(id)])
170 #define TSRMG_FAST(offset, type, element)	(TSRMG_FAST_BULK(offset, type)->element)
171 #define TSRMG_FAST_BULK(offset, type)	((type) (((char*) tsrm_get_ls_cache())+(offset)))
172 
173 #define TSRMG_STATIC(id, type, element)	(TSRMG_BULK_STATIC(id, type)->element)
174 #define TSRMG_BULK_STATIC(id, type)	((type) (*((void ***) TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(id)])
175 #define TSRMG_FAST_STATIC(offset, type, element)	(TSRMG_FAST_BULK_STATIC(offset, type)->element)
176 #define TSRMG_FAST_BULK_STATIC(offset, type)	((type) (((char*) TSRMLS_CACHE)+(offset)))
177 #define TSRMLS_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE;
178 #define TSRMLS_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE = NULL;
179 #define TSRMLS_CACHE_UPDATE() TSRMLS_CACHE = tsrm_get_ls_cache()
180 #define TSRMLS_CACHE _tsrm_ls_cache
181 
182 /* BC only */
183 #define TSRMLS_D void
184 #define TSRMLS_DC
185 #define TSRMLS_C
186 #define TSRMLS_CC
187 #define TSRMLS_FETCH()
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #else /* non ZTS */
194 
195 #define tsrm_env_lock()
196 #define tsrm_env_unlock()
197 
198 #define TSRMLS_FETCH()
199 #define TSRMLS_FETCH_FROM_CTX(ctx)
200 #define TSRMLS_SET_CTX(ctx)
201 
202 #define TSRMG_STATIC(id, type, element)
203 #define TSRMLS_CACHE_EXTERN()
204 #define TSRMLS_CACHE_DEFINE()
205 #define TSRMLS_CACHE_UPDATE()
206 #define TSRMLS_CACHE
207 
208 #define TSRM_TLS
209 
210 /* BC only */
211 #define TSRMLS_D	void
212 #define TSRMLS_DC
213 #define TSRMLS_C
214 #define TSRMLS_CC
215 
216 #endif /* ZTS */
217 
218 #endif /* TSRM_H */
219