1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2017 Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Harald Radi <harald.radi@nme.at> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "zend.h"
22 #include "zend_ts_hash.h"
23
24 /* ts management functions */
begin_read(TsHashTable * ht)25 static void begin_read(TsHashTable *ht)
26 {
27 #ifdef ZTS
28 tsrm_mutex_lock(ht->mx_reader);
29 if ((++(ht->reader)) == 1) {
30 tsrm_mutex_lock(ht->mx_writer);
31 }
32 tsrm_mutex_unlock(ht->mx_reader);
33 #endif
34 }
35
end_read(TsHashTable * ht)36 static void end_read(TsHashTable *ht)
37 {
38 #ifdef ZTS
39 tsrm_mutex_lock(ht->mx_reader);
40 if ((--(ht->reader)) == 0) {
41 tsrm_mutex_unlock(ht->mx_writer);
42 }
43 tsrm_mutex_unlock(ht->mx_reader);
44 #endif
45 }
46
begin_write(TsHashTable * ht)47 static void begin_write(TsHashTable *ht)
48 {
49 #ifdef ZTS
50 tsrm_mutex_lock(ht->mx_writer);
51 #endif
52 }
53
end_write(TsHashTable * ht)54 static void end_write(TsHashTable *ht)
55 {
56 #ifdef ZTS
57 tsrm_mutex_unlock(ht->mx_writer);
58 #endif
59 }
60
61 /* delegates */
_zend_ts_hash_init(TsHashTable * ht,uint nSize,dtor_func_t pDestructor,zend_bool persistent ZEND_FILE_LINE_DC)62 ZEND_API void _zend_ts_hash_init(TsHashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
63 {
64 #ifdef ZTS
65 ht->mx_reader = tsrm_mutex_alloc();
66 ht->mx_writer = tsrm_mutex_alloc();
67 ht->reader = 0;
68 #endif
69 _zend_hash_init(TS_HASH(ht), nSize, pDestructor, persistent ZEND_FILE_LINE_RELAY_CC);
70 }
71
_zend_ts_hash_init_ex(TsHashTable * ht,uint nSize,dtor_func_t pDestructor,zend_bool persistent,zend_bool bApplyProtection ZEND_FILE_LINE_DC)72 ZEND_API void _zend_ts_hash_init_ex(TsHashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC)
73 {
74 #ifdef ZTS
75 ht->mx_reader = tsrm_mutex_alloc();
76 ht->mx_writer = tsrm_mutex_alloc();
77 ht->reader = 0;
78 #endif
79 _zend_hash_init_ex(TS_HASH(ht), nSize, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_RELAY_CC);
80 }
81
zend_ts_hash_destroy(TsHashTable * ht)82 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht)
83 {
84 begin_write(ht);
85 zend_hash_destroy(TS_HASH(ht));
86 end_write(ht);
87
88 #ifdef ZTS
89 tsrm_mutex_free(ht->mx_reader);
90 tsrm_mutex_free(ht->mx_writer);
91 #endif
92 }
93
zend_ts_hash_clean(TsHashTable * ht)94 ZEND_API void zend_ts_hash_clean(TsHashTable *ht)
95 {
96 ht->reader = 0;
97 begin_write(ht);
98 zend_hash_clean(TS_HASH(ht));
99 end_write(ht);
100 }
101
_zend_ts_hash_add_or_update(TsHashTable * ht,zend_string * key,zval * pData,int flag ZEND_FILE_LINE_DC)102 ZEND_API zval *_zend_ts_hash_add_or_update(TsHashTable *ht, zend_string *key, zval *pData, int flag ZEND_FILE_LINE_DC)
103 {
104 zval *retval;
105
106 begin_write(ht);
107 retval = _zend_hash_add_or_update(TS_HASH(ht), key, pData, flag ZEND_FILE_LINE_RELAY_CC);
108 end_write(ht);
109
110 return retval;
111 }
112
_zend_ts_hash_index_add_or_update(TsHashTable * ht,zend_ulong h,zval * pData,int flag ZEND_FILE_LINE_DC)113 ZEND_API zval *_zend_ts_hash_index_add_or_update(TsHashTable *ht, zend_ulong h, zval *pData, int flag ZEND_FILE_LINE_DC)
114 {
115 zval *retval;
116
117 begin_write(ht);
118 retval = _zend_hash_index_add_or_update(TS_HASH(ht), h, pData, flag ZEND_FILE_LINE_RELAY_CC);
119 end_write(ht);
120
121 return retval;
122 }
123
zend_ts_hash_add_empty_element(TsHashTable * ht,zend_string * key)124 ZEND_API zval *zend_ts_hash_add_empty_element(TsHashTable *ht, zend_string *key)
125 {
126 zval *retval;
127
128 begin_write(ht);
129 retval = zend_hash_add_empty_element(TS_HASH(ht), key);
130 end_write(ht);
131
132 return retval;
133 }
134
zend_ts_hash_graceful_destroy(TsHashTable * ht)135 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht)
136 {
137 begin_write(ht);
138 zend_hash_graceful_destroy(TS_HASH(ht));
139 end_write(ht);
140
141 #ifdef ZTS
142 tsrm_mutex_free(ht->mx_reader);
143 tsrm_mutex_free(ht->mx_writer);
144 #endif
145 }
146
zend_ts_hash_apply(TsHashTable * ht,apply_func_t apply_func)147 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func)
148 {
149 begin_write(ht);
150 zend_hash_apply(TS_HASH(ht), apply_func);
151 end_write(ht);
152 }
153
zend_ts_hash_apply_with_argument(TsHashTable * ht,apply_func_arg_t apply_func,void * argument)154 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *argument)
155 {
156 begin_write(ht);
157 zend_hash_apply_with_argument(TS_HASH(ht), apply_func, argument);
158 end_write(ht);
159 }
160
zend_ts_hash_apply_with_arguments(TsHashTable * ht,apply_func_args_t apply_func,int num_args,...)161 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht, apply_func_args_t apply_func, int num_args, ...)
162 {
163 va_list args;
164
165 va_start(args, num_args);
166 begin_write(ht);
167 zend_hash_apply_with_arguments(TS_HASH(ht), apply_func, num_args, args);
168 end_write(ht);
169 va_end(args);
170 }
171
zend_ts_hash_reverse_apply(TsHashTable * ht,apply_func_t apply_func)172 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func)
173 {
174 begin_write(ht);
175 zend_hash_reverse_apply(TS_HASH(ht), apply_func);
176 end_write(ht);
177 }
178
zend_ts_hash_del(TsHashTable * ht,zend_string * key)179 ZEND_API int zend_ts_hash_del(TsHashTable *ht, zend_string *key)
180 {
181 int retval;
182
183 begin_write(ht);
184 retval = zend_hash_del(TS_HASH(ht), key);
185 end_write(ht);
186
187 return retval;
188 }
189
zend_ts_hash_index_del(TsHashTable * ht,zend_ulong h)190 ZEND_API int zend_ts_hash_index_del(TsHashTable *ht, zend_ulong h)
191 {
192 int retval;
193
194 begin_write(ht);
195 retval = zend_hash_index_del(TS_HASH(ht), h);
196 end_write(ht);
197
198 return retval;
199 }
200
zend_ts_hash_find(TsHashTable * ht,zend_string * key)201 ZEND_API zval *zend_ts_hash_find(TsHashTable *ht, zend_string *key)
202 {
203 zval *retval;
204
205 begin_read(ht);
206 retval = zend_hash_find(TS_HASH(ht), key);
207 end_read(ht);
208
209 return retval;
210 }
211
zend_ts_hash_index_find(TsHashTable * ht,zend_ulong h)212 ZEND_API zval *zend_ts_hash_index_find(TsHashTable *ht, zend_ulong h)
213 {
214 zval *retval;
215
216 begin_read(ht);
217 retval = zend_hash_index_find(TS_HASH(ht), h);
218 end_read(ht);
219
220 return retval;
221 }
222
zend_ts_hash_exists(TsHashTable * ht,zend_string * key)223 ZEND_API int zend_ts_hash_exists(TsHashTable *ht, zend_string *key)
224 {
225 int retval;
226
227 begin_read(ht);
228 retval = zend_hash_exists(TS_HASH(ht), key);
229 end_read(ht);
230
231 return retval;
232 }
233
zend_ts_hash_index_exists(TsHashTable * ht,zend_ulong h)234 ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, zend_ulong h)
235 {
236 int retval;
237
238 begin_read(ht);
239 retval = zend_hash_index_exists(TS_HASH(ht), h);
240 end_read(ht);
241
242 return retval;
243 }
244
zend_ts_hash_copy(TsHashTable * target,TsHashTable * source,copy_ctor_func_t pCopyConstructor)245 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor)
246 {
247 begin_read(source);
248 begin_write(target);
249 zend_hash_copy(TS_HASH(target), TS_HASH(source), pCopyConstructor);
250 end_write(target);
251 end_read(source);
252 }
253
zend_ts_hash_copy_to_hash(HashTable * target,TsHashTable * source,copy_ctor_func_t pCopyConstructor)254 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor)
255 {
256 begin_read(source);
257 zend_hash_copy(target, TS_HASH(source), pCopyConstructor);
258 end_read(source);
259 }
260
zend_ts_hash_merge(TsHashTable * target,TsHashTable * source,copy_ctor_func_t pCopyConstructor,int overwrite)261 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite)
262 {
263 begin_read(source);
264 begin_write(target);
265 zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, overwrite);
266 end_write(target);
267 end_read(source);
268 }
269
zend_ts_hash_merge_ex(TsHashTable * target,TsHashTable * source,copy_ctor_func_t pCopyConstructor,merge_checker_func_t pMergeSource,void * pParam)270 ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam)
271 {
272 begin_read(source);
273 begin_write(target);
274 zend_hash_merge_ex(TS_HASH(target), TS_HASH(source), pCopyConstructor, pMergeSource, pParam);
275 end_write(target);
276 end_read(source);
277 }
278
zend_ts_hash_sort(TsHashTable * ht,sort_func_t sort_func,compare_func_t compare_func,int renumber)279 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber)
280 {
281 int retval;
282
283 begin_write(ht);
284 retval = zend_hash_sort_ex(TS_HASH(ht), sort_func, compare_func, renumber);
285 end_write(ht);
286
287 return retval;
288 }
289
zend_ts_hash_compare(TsHashTable * ht1,TsHashTable * ht2,compare_func_t compar,zend_bool ordered)290 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered)
291 {
292 int retval;
293
294 begin_read(ht1);
295 begin_read(ht2);
296 retval = zend_hash_compare(TS_HASH(ht1), TS_HASH(ht2), compar, ordered);
297 end_read(ht2);
298 end_read(ht1);
299
300 return retval;
301 }
302
zend_ts_hash_minmax(TsHashTable * ht,compare_func_t compar,int flag)303 ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag)
304 {
305 zval *retval;
306
307 begin_read(ht);
308 retval = zend_hash_minmax(TS_HASH(ht), compar, flag);
309 end_read(ht);
310
311 return retval;
312 }
313
zend_ts_hash_num_elements(TsHashTable * ht)314 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht)
315 {
316 int retval;
317
318 begin_read(ht);
319 retval = zend_hash_num_elements(TS_HASH(ht));
320 end_read(ht);
321
322 return retval;
323 }
324
zend_ts_hash_rehash(TsHashTable * ht)325 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht)
326 {
327 int retval;
328
329 begin_write(ht);
330 retval = zend_hash_rehash(TS_HASH(ht));
331 end_write(ht);
332
333 return retval;
334 }
335
zend_ts_hash_str_find(TsHashTable * ht,const char * key,size_t len)336 ZEND_API zval *zend_ts_hash_str_find(TsHashTable *ht, const char *key, size_t len)
337 {
338 zval *retval;
339
340 begin_read(ht);
341 retval = zend_hash_str_find(TS_HASH(ht), key, len);
342 end_read(ht);
343
344 return retval;
345 }
346
_zend_ts_hash_str_update(TsHashTable * ht,const char * key,size_t len,zval * pData ZEND_FILE_LINE_DC)347 ZEND_API zval *_zend_ts_hash_str_update(TsHashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC)
348 {
349 zval *retval;
350
351 begin_write(ht);
352 retval = zend_hash_str_update(TS_HASH(ht), key, len, pData);
353 end_write(ht);
354
355 return retval;
356 }
357
_zend_ts_hash_str_add(TsHashTable * ht,const char * key,size_t len,zval * pData ZEND_FILE_LINE_DC)358 ZEND_API zval *_zend_ts_hash_str_add(TsHashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC)
359 {
360 zval *retval;
361
362 begin_write(ht);
363 retval = zend_hash_str_add(TS_HASH(ht), key, len, pData);
364 end_write(ht);
365
366 return retval;
367 }
368
369 /*
370 * Local variables:
371 * tab-width: 4
372 * c-basic-offset: 4
373 * indent-tabs-mode: t
374 * End:
375 */
376