- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathTSRM.h
206 lines (164 loc) · 6.65 KB
/
TSRM.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
+----------------------------------------------------------------------+
| Thread Safe Resource Manager |
+----------------------------------------------------------------------+
| Copyright (c) 1999-2011, Andi Gutmans, Sascha Schumann, Zeev Suraski |
| This source file is subject to the TSRM license, that is bundled |
| with this package in the file LICENSE |
+----------------------------------------------------------------------+
| Authors: Zeev Suraski <zeev@php.net> |
+----------------------------------------------------------------------+
*/
#ifndefTSRM_H
#defineTSRM_H
#if !defined(__CYGWIN__) && defined(_WIN32)
# defineTSRM_WIN32
# include<Zend/zend_config.w32.h>
#else
# include<main/php_config.h>
#endif
#include<stdint.h>
#include<stdbool.h>
#ifdefTSRM_WIN32
# ifdefTSRM_EXPORTS
# defineTSRM_API __declspec(dllexport)
# else
# defineTSRM_API __declspec(dllimport)
# endif
#elif defined(__GNUC__) &&__GNUC__ >= 4
# defineTSRM_API __attribute__ ((visibility("default")))
#else
# defineTSRM_API
#endif
typedefintptr_ttsrm_intptr_t;
typedefuintptr_ttsrm_uintptr_t;
/* Only compile multi-threading functions if we're in ZTS mode */
#ifdefZTS
#ifdefTSRM_WIN32
# ifndefTSRM_INCLUDE_FULL_WINDOWS_HEADERS
# defineWIN32_LEAN_AND_MEAN
# endif
#else
# include<pthread.h>
#endif
#ifSIZEOF_SIZE_T==4
# defineTSRM_ALIGNED_SIZE(size) \
(((size) + INT32_C(15)) & ~INT32_C(15))
#else
# defineTSRM_ALIGNED_SIZE(size) \
(((size) + INT64_C(15)) & ~INT64_C(15))
#endif
typedefintts_rsrc_id;
/* Define THREAD_T and MUTEX_T */
#ifdefTSRM_WIN32
# defineTHREAD_T DWORD
# defineMUTEX_T CRITICAL_SECTION *
#else
# defineTHREAD_T pthread_t
# defineMUTEX_T pthread_mutex_t *
#endif
#include<signal.h>
typedefvoid (*ts_allocate_ctor)(void*);
typedefvoid (*ts_allocate_dtor)(void*);
#defineTHREAD_HASH_OF(thr,ts) (unsigned long)thr%(unsigned long)ts
#ifdef__cplusplus
extern"C" {
#endif
/* startup/shutdown */
TSRM_APIbooltsrm_startup(intexpected_threads, intexpected_resources, intdebug_level, constchar*debug_filename);
TSRM_APIvoidtsrm_shutdown(void);
/* environ lock API */
TSRM_APIvoidtsrm_env_lock(void);
TSRM_APIvoidtsrm_env_unlock(void);
/* allocates a new thread-safe-resource id */
TSRM_APIts_rsrc_idts_allocate_id(ts_rsrc_id*rsrc_id, size_tsize, ts_allocate_ctorctor, ts_allocate_dtordtor);
/* Fast resource in reserved (pre-allocated) space */
TSRM_APIvoidtsrm_reserve(size_tsize);
TSRM_APIts_rsrc_idts_allocate_fast_id(ts_rsrc_id*rsrc_id, size_t*offset, size_tsize, ts_allocate_ctorctor, ts_allocate_dtordtor);
/* fetches the requested resource for the current thread */
TSRM_APIvoid*ts_resource_ex(ts_rsrc_idid, THREAD_T*th_id);
#definets_resource(id) ts_resource_ex(id, NULL)
/* frees all resources allocated for the current thread */
TSRM_APIvoidts_free_thread(void);
/* deallocates all occurrences of a given id */
TSRM_APIvoidts_free_id(ts_rsrc_idid);
/* Runs a callback on all resources of the given id.
* The caller is responsible for ensuring the underlying resources don't data-race. */
TSRM_APIvoidts_apply_for_id(ts_rsrc_idid, void (*cb)(void*));
/* Debug support */
#defineTSRM_ERROR_LEVEL_ERROR 1
#defineTSRM_ERROR_LEVEL_CORE 2
#defineTSRM_ERROR_LEVEL_INFO 3
typedefvoid (*tsrm_thread_begin_func_t)(THREAD_Tthread_id);
typedefvoid (*tsrm_thread_end_func_t)(THREAD_Tthread_id);
typedefvoid (*tsrm_shutdown_func_t)(void);
TSRM_APIinttsrm_error(intlevel, constchar*format, ...);
TSRM_APIvoidtsrm_error_set(intlevel, constchar*debug_filename);
/* utility functions */
TSRM_APITHREAD_Ttsrm_thread_id(void);
TSRM_APIMUTEX_Ttsrm_mutex_alloc(void);
TSRM_APIvoidtsrm_mutex_free(MUTEX_Tmutexp);
TSRM_APIinttsrm_mutex_lock(MUTEX_Tmutexp);
TSRM_APIinttsrm_mutex_unlock(MUTEX_Tmutexp);
#ifdefHAVE_SIGPROCMASK
TSRM_APIinttsrm_sigmask(inthow, constsigset_t*set, sigset_t*oldset);
#endif
TSRM_APIvoid*tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_tnew_thread_begin_handler);
TSRM_APIvoid*tsrm_set_new_thread_end_handler(tsrm_thread_end_func_tnew_thread_end_handler);
TSRM_APIvoid*tsrm_set_shutdown_handler(tsrm_shutdown_func_tshutdown_handler);
TSRM_APIvoid*tsrm_get_ls_cache(void);
TSRM_APIsize_ttsrm_get_ls_cache_tcb_offset(void);
TSRM_APIbooltsrm_is_main_thread(void);
TSRM_APIbooltsrm_is_shutdown(void);
TSRM_APIconstchar*tsrm_api_name(void);
TSRM_APIbooltsrm_is_managed_thread(void);
#ifdefTSRM_WIN32
# defineTSRM_TLS __declspec(thread)
#else
# defineTSRM_TLS __thread
#endif
#ifndef__has_attribute
# define__has_attribute(x) 0
#endif
#if !__has_attribute(tls_model) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__MUSL__) || defined(__HAIKU__)
# defineTSRM_TLS_MODEL_ATTR
# defineTSRM_TLS_MODEL_DEFAULT
#elif__PIC__
# defineTSRM_TLS_MODEL_ATTR __attribute__((tls_model("initial-exec")))
# defineTSRM_TLS_MODEL_INITIAL_EXEC
#else
# defineTSRM_TLS_MODEL_ATTR __attribute__((tls_model("local-exec")))
# defineTSRM_TLS_MODEL_LOCAL_EXEC
#endif
#defineTSRM_SHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)+1)
#defineTSRM_UNSHUFFLE_RSRC_ID(rsrc_id) ((rsrc_id)-1)
#defineTSRMG(id, type, element) (TSRMG_BULK(id, type)->element)
#defineTSRMG_BULK(id, type) ((type) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(id)])
#defineTSRMG_FAST(offset, type, element) (TSRMG_FAST_BULK(offset, type)->element)
#defineTSRMG_FAST_BULK(offset, type) ((type) (((char*) tsrm_get_ls_cache())+(offset)))
#defineTSRMG_STATIC(id, type, element) (TSRMG_BULK_STATIC(id, type)->element)
#defineTSRMG_BULK_STATIC(id, type) ((type) (*((void ***) TSRMLS_CACHE))[TSRM_UNSHUFFLE_RSRC_ID(id)])
#defineTSRMG_FAST_STATIC(offset, type, element) (TSRMG_FAST_BULK_STATIC(offset, type)->element)
#defineTSRMG_FAST_BULK_STATIC(offset, type) ((type) (((char*) TSRMLS_CACHE)+(offset)))
#defineTSRMLS_MAIN_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR;
#defineTSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE TSRM_TLS_MODEL_ATTR = NULL;
#defineTSRMLS_CACHE_EXTERN() extern TSRM_TLS void *TSRMLS_CACHE;
#defineTSRMLS_CACHE_DEFINE() TSRM_TLS void *TSRMLS_CACHE = NULL;
#defineTSRMLS_CACHE_UPDATE() TSRMLS_CACHE = tsrm_get_ls_cache()
#defineTSRMLS_CACHE _tsrm_ls_cache
#ifdef__cplusplus
}
#endif
#else/* non ZTS */
#definetsrm_env_lock()
#definetsrm_env_unlock()
#defineTSRMG_STATIC(id, type, element)
#defineTSRMLS_MAIN_CACHE_EXTERN()
#defineTSRMLS_MAIN_CACHE_DEFINE()
#defineTSRMLS_CACHE_EXTERN()
#defineTSRMLS_CACHE_DEFINE()
#defineTSRMLS_CACHE_UPDATE()
#defineTSRMLS_CACHE
#defineTSRM_TLS
#endif/* ZTS */
#endif/* TSRM_H */