1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2009 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: zend_ts_hash.c 273191 2009-01-09 19:16:02Z tony2001 $ */
20 :
21 : #include "zend.h"
22 : #include "zend_ts_hash.h"
23 :
24 : /* ts management functions */
25 : static void begin_read(TsHashTable *ht) /* {{{ */
26 0 : {
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 0 : }
35 : /* }}} */
36 :
37 : static void end_read(TsHashTable *ht) /* {{{ */
38 0 : {
39 : #ifdef ZTS
40 : tsrm_mutex_lock(ht->mx_reader);
41 : if ((--(ht->reader)) == 0) {
42 : tsrm_mutex_unlock(ht->mx_writer);
43 : }
44 : tsrm_mutex_unlock(ht->mx_reader);
45 : #endif
46 0 : }
47 : /* }}} */
48 :
49 : static void begin_write(TsHashTable *ht) /* {{{ */
50 0 : {
51 : #ifdef ZTS
52 : tsrm_mutex_lock(ht->mx_writer);
53 : #endif
54 0 : }
55 : /* }}} */
56 :
57 : static void end_write(TsHashTable *ht) /* {{{ */
58 0 : {
59 : #ifdef ZTS
60 : tsrm_mutex_unlock(ht->mx_writer);
61 : #endif
62 0 : }
63 : /* }}} */
64 :
65 : /* delegates */
66 : ZEND_API int _zend_ts_hash_init(TsHashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC) /* {{{ */
67 0 : {
68 : #ifdef ZTS
69 : ht->mx_reader = tsrm_mutex_alloc();
70 : ht->mx_writer = tsrm_mutex_alloc();
71 : ht->reader = 0;
72 : #endif
73 0 : return _zend_hash_init(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent ZEND_FILE_LINE_RELAY_CC);
74 : }
75 : /* }}} */
76 :
77 : ZEND_API int _zend_ts_hash_init_ex(TsHashTable *ht, uint nSize, hash_func_t pHashFunction, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC) /* {{{ */
78 0 : {
79 : #ifdef ZTS
80 : ht->mx_reader = tsrm_mutex_alloc();
81 : ht->mx_writer = tsrm_mutex_alloc();
82 : ht->reader = 0;
83 : #endif
84 0 : return _zend_hash_init_ex(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_RELAY_CC);
85 : }
86 : /* }}} */
87 :
88 : ZEND_API void zend_ts_hash_destroy(TsHashTable *ht) /* {{{ */
89 0 : {
90 0 : begin_write(ht);
91 0 : zend_hash_destroy(TS_HASH(ht));
92 0 : end_write(ht);
93 :
94 : #ifdef ZTS
95 : tsrm_mutex_free(ht->mx_reader);
96 : tsrm_mutex_free(ht->mx_writer);
97 : #endif
98 0 : }
99 : /* }}} */
100 :
101 : ZEND_API void zend_ts_hash_clean(TsHashTable *ht) /* {{{ */
102 0 : {
103 0 : ht->reader = 0;
104 0 : begin_write(ht);
105 0 : zend_hash_clean(TS_HASH(ht));
106 0 : end_write(ht);
107 0 : }
108 : /* }}} */
109 :
110 : ZEND_API int _zend_ts_hash_add_or_update(TsHashTable *ht, char *arKey, uint nKeyLength, void *pData, uint nDataSize, void **pDest, int flag ZEND_FILE_LINE_DC) /* {{{ */
111 0 : {
112 : int retval;
113 :
114 0 : begin_write(ht);
115 0 : retval = _zend_hash_add_or_update(TS_HASH(ht), arKey, nKeyLength, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
116 0 : end_write(ht);
117 :
118 0 : return retval;
119 : }
120 : /* }}} */
121 :
122 : ZEND_API int _zend_ts_hash_quick_add_or_update(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, void *pData, uint nDataSize, void **pDest, int flag ZEND_FILE_LINE_DC) /* {{{ */
123 0 : {
124 : int retval;
125 :
126 0 : begin_write(ht);
127 0 : retval = _zend_hash_quick_add_or_update(TS_HASH(ht), arKey, nKeyLength, h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
128 0 : end_write(ht);
129 :
130 0 : return retval;
131 : }
132 : /* }}} */
133 :
134 : ZEND_API int _zend_ts_hash_index_update_or_next_insert(TsHashTable *ht, ulong h, void *pData, uint nDataSize, void **pDest, int flag ZEND_FILE_LINE_DC) /* {{{ */
135 0 : {
136 : int retval;
137 :
138 0 : begin_write(ht);
139 0 : retval = _zend_hash_index_update_or_next_insert(TS_HASH(ht), h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
140 0 : end_write(ht);
141 :
142 0 : return retval;
143 : }
144 : /* }}} */
145 :
146 : ZEND_API int zend_ts_hash_add_empty_element(TsHashTable *ht, char *arKey, uint nKeyLength) /* {{{ */
147 0 : {
148 : int retval;
149 :
150 0 : begin_write(ht);
151 0 : retval = zend_hash_add_empty_element(TS_HASH(ht), arKey, nKeyLength);
152 0 : end_write(ht);
153 :
154 0 : return retval;
155 : }
156 : /* }}} */
157 :
158 : ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht) /* {{{ */
159 0 : {
160 0 : begin_write(ht);
161 0 : zend_hash_graceful_destroy(TS_HASH(ht));
162 0 : end_write(ht);
163 :
164 : #ifdef ZTS
165 : tsrm_mutex_free(ht->mx_reader);
166 : tsrm_mutex_free(ht->mx_reader);
167 : #endif
168 0 : }
169 : /* }}} */
170 :
171 : ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC) /* {{{ */
172 0 : {
173 0 : begin_write(ht);
174 0 : zend_hash_apply(TS_HASH(ht), apply_func TSRMLS_CC);
175 0 : end_write(ht);
176 0 : }
177 : /* }}} */
178 :
179 : ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *argument TSRMLS_DC) /* {{{ */
180 0 : {
181 0 : begin_write(ht);
182 0 : zend_hash_apply_with_argument(TS_HASH(ht), apply_func, argument TSRMLS_CC);
183 0 : end_write(ht);
184 0 : }
185 : /* }}} */
186 :
187 : ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int num_args, ...) /* {{{ */
188 0 : {
189 : va_list args;
190 :
191 0 : va_start(args, num_args);
192 0 : begin_write(ht);
193 0 : zend_hash_apply_with_arguments(TS_HASH(ht) TSRMLS_CC, apply_func, num_args, args);
194 0 : end_write(ht);
195 0 : va_end(args);
196 0 : }
197 : /* }}} */
198 :
199 : ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC) /* {{{ */
200 0 : {
201 0 : begin_write(ht);
202 0 : zend_hash_reverse_apply(TS_HASH(ht), apply_func TSRMLS_CC);
203 0 : end_write(ht);
204 0 : }
205 : /* }}} */
206 :
207 : ZEND_API int zend_ts_hash_del_key_or_index(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag) /* {{{ */
208 0 : {
209 : int retval;
210 :
211 0 : begin_write(ht);
212 0 : retval = zend_hash_del_key_or_index(TS_HASH(ht), arKey, nKeyLength, h, flag);
213 0 : end_write(ht);
214 :
215 0 : return retval;
216 : }
217 : /* }}} */
218 :
219 : ZEND_API ulong zend_ts_get_hash_value(TsHashTable *ht, char *arKey, uint nKeyLength) /* {{{ */
220 0 : {
221 : ulong retval;
222 :
223 0 : begin_read(ht);
224 0 : retval = zend_get_hash_value(arKey, nKeyLength);
225 0 : end_read(ht);
226 :
227 0 : return retval;
228 : }
229 : /* }}} */
230 :
231 : ZEND_API int zend_ts_hash_find(TsHashTable *ht, char *arKey, uint nKeyLength, void **pData) /* {{{ */
232 0 : {
233 : int retval;
234 :
235 0 : begin_read(ht);
236 0 : retval = zend_hash_find(TS_HASH(ht), arKey, nKeyLength, pData);
237 0 : end_read(ht);
238 :
239 0 : return retval;
240 : }
241 : /* }}} */
242 :
243 : ZEND_API int zend_ts_hash_quick_find(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, void **pData) /* {{{ */
244 0 : {
245 : int retval;
246 :
247 0 : begin_read(ht);
248 0 : retval = zend_hash_quick_find(TS_HASH(ht), arKey, nKeyLength, h, pData);
249 0 : end_read(ht);
250 :
251 0 : return retval;
252 : }
253 : /* }}} */
254 :
255 : ZEND_API int zend_ts_hash_index_find(TsHashTable *ht, ulong h, void **pData) /* {{{ */
256 0 : {
257 : int retval;
258 :
259 0 : begin_read(ht);
260 0 : retval = zend_hash_index_find(TS_HASH(ht), h, pData);
261 0 : end_read(ht);
262 :
263 0 : return retval;
264 : }
265 : /* }}} */
266 :
267 : ZEND_API int zend_ts_hash_exists(TsHashTable *ht, char *arKey, uint nKeyLength) /* {{{ */
268 0 : {
269 : int retval;
270 :
271 0 : begin_read(ht);
272 0 : retval = zend_hash_exists(TS_HASH(ht), arKey, nKeyLength);
273 0 : end_read(ht);
274 :
275 0 : return retval;
276 : }
277 : /* }}} */
278 :
279 : ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, ulong h) /* {{{ */
280 0 : {
281 : int retval;
282 :
283 0 : begin_read(ht);
284 0 : retval = zend_hash_index_exists(TS_HASH(ht), h);
285 0 : end_read(ht);
286 :
287 0 : return retval;
288 : }
289 : /* }}} */
290 :
291 : ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size) /* {{{ */
292 0 : {
293 0 : begin_read(source);
294 0 : begin_write(target);
295 0 : zend_hash_copy(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size);
296 0 : end_write(target);
297 0 : end_read(source);
298 0 : }
299 : /* }}} */
300 :
301 : ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size) /* {{{ */
302 0 : {
303 0 : begin_read(source);
304 0 : zend_hash_copy(target, TS_HASH(source), pCopyConstructor, tmp, size);
305 0 : end_read(source);
306 0 : }
307 : /* }}} */
308 :
309 : ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite) /* {{{ */
310 0 : {
311 0 : begin_read(source);
312 0 : begin_write(target);
313 0 : zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size, overwrite);
314 0 : end_write(target);
315 0 : end_read(source);
316 0 : }
317 : /* }}} */
318 :
319 : ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, uint size, merge_checker_func_t pMergeSource, void *pParam) /* {{{ */
320 0 : {
321 0 : begin_read(source);
322 0 : begin_write(target);
323 0 : zend_hash_merge_ex(TS_HASH(target), TS_HASH(source), pCopyConstructor, size, pMergeSource, pParam);
324 0 : end_write(target);
325 0 : end_read(source);
326 0 : }
327 : /* }}} */
328 :
329 : ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC) /* {{{ */
330 0 : {
331 : int retval;
332 :
333 0 : begin_write(ht);
334 0 : retval = zend_hash_sort(TS_HASH(ht), sort_func, compare_func, renumber TSRMLS_CC);
335 0 : end_write(ht);
336 :
337 0 : return retval;
338 : }
339 : /* }}} */
340 :
341 : ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC) /* {{{ */
342 0 : {
343 : int retval;
344 :
345 0 : begin_read(ht1);
346 0 : begin_read(ht2);
347 0 : retval = zend_hash_compare(TS_HASH(ht1), TS_HASH(ht2), compar, ordered TSRMLS_CC);
348 0 : end_read(ht2);
349 0 : end_read(ht1);
350 :
351 0 : return retval;
352 : }
353 : /* }}} */
354 :
355 : ZEND_API int zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC) /* {{{ */
356 0 : {
357 : int retval;
358 :
359 0 : begin_read(ht);
360 0 : retval = zend_hash_minmax(TS_HASH(ht), compar, flag, pData TSRMLS_CC);
361 0 : end_read(ht);
362 :
363 0 : return retval;
364 : }
365 : /* }}} */
366 :
367 : ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht) /* {{{ */
368 0 : {
369 : int retval;
370 :
371 0 : begin_read(ht);
372 0 : retval = zend_hash_num_elements(TS_HASH(ht));
373 0 : end_read(ht);
374 :
375 0 : return retval;
376 : }
377 : /* }}} */
378 :
379 : ZEND_API int zend_ts_hash_rehash(TsHashTable *ht) /* {{{ */
380 0 : {
381 : int retval;
382 :
383 0 : begin_write(ht);
384 0 : retval = zend_hash_rehash(TS_HASH(ht));
385 0 : end_write(ht);
386 :
387 0 : return retval;
388 : }
389 : /* }}} */
390 :
391 : #if ZEND_DEBUG
392 : void zend_ts_hash_display_pListTail(TsHashTable *ht) /* {{{ */
393 : {
394 : begin_read(ht);
395 : zend_hash_display_pListTail(TS_HASH(ht));
396 : end_read(ht);
397 : }
398 : /* }}} */
399 :
400 : void zend_ts_hash_display(TsHashTable *ht) /* {{{ */
401 : {
402 : begin_read(ht);
403 : zend_hash_display(TS_HASH(ht));
404 : end_read(ht);
405 : }
406 : /* }}} */
407 : #endif
408 :
409 : /*
410 : * Local variables:
411 : * tab-width: 4
412 : * c-basic-offset: 4
413 : * indent-tabs-mode: t
414 : * End:
415 : */
|