Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 381 lines (307 sloc) 9.727 kb
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
8775a375 »
2012-01-01 - Year++
5 | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
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 |
f68c7ff2 »
2003-06-10 updating license information in the headers.
9 | available through the world-wide-web at the following url: |
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
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
333406bd »
2003-02-01 - Added some missing CVS $Id$ tags, headers and footers.
19 /* $Id$ */
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
20
76aaeebd »
2003-08-18 Improve tracking
21 #include "zend.h"
3e3ae4f6 »
2002-03-23 - Fix build without ZTS. If someone has a nicer fix let me know.
22 #include "zend_ts_hash.h"
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
23
24 /* ts management functions */
25 static void begin_read(TsHashTable *ht)
26 {
54c73d1c »
2003-01-30 fix non-zts build for wez
27 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
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);
54c73d1c »
2003-01-30 fix non-zts build for wez
33 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
34 }
35
36 static void end_read(TsHashTable *ht)
37 {
54c73d1c »
2003-01-30 fix non-zts build for wez
38 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
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);
54c73d1c »
2003-01-30 fix non-zts build for wez
44 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
45 }
46
47 static void begin_write(TsHashTable *ht)
48 {
54c73d1c »
2003-01-30 fix non-zts build for wez
49 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
50 tsrm_mutex_lock(ht->mx_writer);
54c73d1c »
2003-01-30 fix non-zts build for wez
51 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
52 }
53
54 static void end_write(TsHashTable *ht)
55 {
54c73d1c »
2003-01-30 fix non-zts build for wez
56 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
57 tsrm_mutex_unlock(ht->mx_writer);
54c73d1c »
2003-01-30 fix non-zts build for wez
58 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
59 }
60
61 /* delegates */
76aaeebd »
2003-08-18 Improve tracking
62 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
63 {
54c73d1c »
2003-01-30 fix non-zts build for wez
64 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
65 ht->mx_reader = tsrm_mutex_alloc();
66 ht->mx_writer = tsrm_mutex_alloc();
67 ht->reader = 0;
54c73d1c »
2003-01-30 fix non-zts build for wez
68 #endif
76aaeebd »
2003-08-18 Improve tracking
69 return _zend_hash_init(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent ZEND_FILE_LINE_RELAY_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
70 }
71
76aaeebd »
2003-08-18 Improve tracking
72 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
73 {
54c73d1c »
2003-01-30 fix non-zts build for wez
74 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
75 ht->mx_reader = tsrm_mutex_alloc();
76 ht->mx_writer = tsrm_mutex_alloc();
77 ht->reader = 0;
54c73d1c »
2003-01-30 fix non-zts build for wez
78 #endif
76aaeebd »
2003-08-18 Improve tracking
79 return _zend_hash_init_ex(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_RELAY_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
80 }
81
82 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht)
83 {
c8cfd3b9 »
2007-05-11 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
84 begin_write(ht);
85 zend_hash_destroy(TS_HASH(ht));
86 end_write(ht);
87
54c73d1c »
2003-01-30 fix non-zts build for wez
88 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
89 tsrm_mutex_free(ht->mx_reader);
c8d19858 »
2003-01-11 freed reader twice instead of writer and reader
90 tsrm_mutex_free(ht->mx_writer);
54c73d1c »
2003-01-30 fix non-zts build for wez
91 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
92 }
93
94 ZEND_API void zend_ts_hash_clean(TsHashTable *ht)
95 {
96 ht->reader = 0;
c8cfd3b9 »
2007-05-11 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
97 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
98 zend_hash_clean(TS_HASH(ht));
c8cfd3b9 »
2007-05-11 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
99 end_write(ht);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
100 }
101
0dba58c3 »
2003-08-18 Improve tracking
102 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
103 {
104 int retval;
105
106 begin_write(ht);
0dba58c3 »
2003-08-18 Improve tracking
107 retval = _zend_hash_add_or_update(TS_HASH(ht), arKey, nKeyLength, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
108 end_write(ht);
109
110 return retval;
111 }
112
0dba58c3 »
2003-08-18 Improve tracking
113 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
114 {
115 int retval;
116
117 begin_write(ht);
0dba58c3 »
2003-08-18 Improve tracking
118 retval = _zend_hash_quick_add_or_update(TS_HASH(ht), arKey, nKeyLength, h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
119 end_write(ht);
120
121 return retval;
122 }
123
0dba58c3 »
2003-08-18 Improve tracking
124 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
125 {
126 int retval;
127
128 begin_write(ht);
0dba58c3 »
2003-08-18 Improve tracking
129 retval = _zend_hash_index_update_or_next_insert(TS_HASH(ht), h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
130 end_write(ht);
131
132 return retval;
133 }
134
135 ZEND_API int zend_ts_hash_add_empty_element(TsHashTable *ht, char *arKey, uint nKeyLength)
136 {
137 int retval;
138
139 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
140 retval = zend_hash_add_empty_element(TS_HASH(ht), arKey, nKeyLength);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
141 end_write(ht);
142
143 return retval;
144 }
145
146 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht)
147 {
c8cfd3b9 »
2007-05-11 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
148 begin_write(ht);
149 zend_hash_graceful_destroy(TS_HASH(ht));
150 end_write(ht);
151
54c73d1c »
2003-01-30 fix non-zts build for wez
152 #ifdef ZTS
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
153 tsrm_mutex_free(ht->mx_reader);
154 tsrm_mutex_free(ht->mx_reader);
54c73d1c »
2003-01-30 fix non-zts build for wez
155 #endif
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
156 }
157
158 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC)
159 {
160 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
161 zend_hash_apply(TS_HASH(ht), apply_func TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
162 end_write(ht);
163 }
164
165 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *argument TSRMLS_DC)
166 {
167 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
168 zend_hash_apply_with_argument(TS_HASH(ht), apply_func, argument TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
169 end_write(ht);
170 }
171
0fbe6a0c »
2008-07-24 - MFH: Added TSRMLS_DC to apply_func_args_t and zend_hash_apply_with_…
172 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int num_args, ...)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
173 {
174 va_list args;
175
176 va_start(args, num_args);
177 begin_write(ht);
0fbe6a0c »
2008-07-24 - MFH: Added TSRMLS_DC to apply_func_args_t and zend_hash_apply_with_…
178 zend_hash_apply_with_arguments(TS_HASH(ht) TSRMLS_CC, apply_func, num_args, args);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
179 end_write(ht);
180 va_end(args);
181 }
182
183 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC)
184 {
185 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
186 zend_hash_reverse_apply(TS_HASH(ht), apply_func TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
187 end_write(ht);
188 }
189
190 ZEND_API int zend_ts_hash_del_key_or_index(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag)
191 {
192 int retval;
193
194 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
195 retval = zend_hash_del_key_or_index(TS_HASH(ht), arKey, nKeyLength, h, flag);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
196 end_write(ht);
197
198 return retval;
199 }
200
201 ZEND_API ulong zend_ts_get_hash_value(TsHashTable *ht, char *arKey, uint nKeyLength)
202 {
203 ulong retval;
204
205 begin_read(ht);
471947b1 »
2003-02-04 Reimplement PPP properties
206 retval = zend_get_hash_value(arKey, nKeyLength);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
207 end_read(ht);
208
209 return retval;
210 }
211
212 ZEND_API int zend_ts_hash_find(TsHashTable *ht, char *arKey, uint nKeyLength, void **pData)
213 {
214 int retval;
215
216 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
217 retval = zend_hash_find(TS_HASH(ht), arKey, nKeyLength, pData);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
218 end_read(ht);
219
220 return retval;
221 }
222
223 ZEND_API int zend_ts_hash_quick_find(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, void **pData)
224 {
225 int retval;
226
227 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
228 retval = zend_hash_quick_find(TS_HASH(ht), arKey, nKeyLength, h, pData);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
229 end_read(ht);
230
231 return retval;
232 }
233
234 ZEND_API int zend_ts_hash_index_find(TsHashTable *ht, ulong h, void **pData)
235 {
236 int retval;
237
238 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
239 retval = zend_hash_index_find(TS_HASH(ht), h, pData);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
240 end_read(ht);
241
242 return retval;
243 }
244
245 ZEND_API int zend_ts_hash_exists(TsHashTable *ht, char *arKey, uint nKeyLength)
246 {
247 int retval;
248
249 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
250 retval = zend_hash_exists(TS_HASH(ht), arKey, nKeyLength);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
251 end_read(ht);
252
253 return retval;
254 }
255
256 ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, ulong h)
257 {
258 int retval;
259
260 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
261 retval = zend_hash_index_exists(TS_HASH(ht), h);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
262 end_read(ht);
263
264 return retval;
265 }
266
267 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size)
268 {
269 begin_read(source);
270 begin_write(target);
ba364121 »
2002-06-10 added TS_HASH macro
271 zend_hash_copy(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
272 end_write(target);
273 end_read(source);
274 }
275
514e6333 »
2009-01-09 MFH: add zend_ts_hash_copy_to_hash()
276 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size)
277 {
278 begin_read(source);
279 zend_hash_copy(target, TS_HASH(source), pCopyConstructor, tmp, size);
280 end_read(source);
281 }
282
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
283 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite)
284 {
285 begin_read(source);
286 begin_write(target);
ba364121 »
2002-06-10 added TS_HASH macro
287 zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size, overwrite);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
288 end_write(target);
289 end_read(source);
290 }
291
471947b1 »
2003-02-04 Reimplement PPP properties
292 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)
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
293 {
294 begin_read(source);
295 begin_write(target);
471947b1 »
2003-02-04 Reimplement PPP properties
296 zend_hash_merge_ex(TS_HASH(target), TS_HASH(source), pCopyConstructor, size, pMergeSource, pParam);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
297 end_write(target);
298 end_read(source);
299 }
300
301 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC)
302 {
303 int retval;
304
305 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
306 retval = zend_hash_sort(TS_HASH(ht), sort_func, compare_func, renumber TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
307 end_write(ht);
308
309 return retval;
310 }
311
312 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC)
313 {
314 int retval;
315
316 begin_read(ht1);
317 begin_read(ht2);
ba364121 »
2002-06-10 added TS_HASH macro
318 retval = zend_hash_compare(TS_HASH(ht1), TS_HASH(ht2), compar, ordered TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
319 end_read(ht2);
320 end_read(ht1);
321
322 return retval;
323 }
324
325 ZEND_API int zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC)
326 {
327 int retval;
328
329 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
330 retval = zend_hash_minmax(TS_HASH(ht), compar, flag, pData TSRMLS_CC);
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
331 end_read(ht);
332
333 return retval;
334 }
335
336 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht)
337 {
338 int retval;
339
340 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
341 retval = zend_hash_num_elements(TS_HASH(ht));
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
342 end_read(ht);
343
344 return retval;
345 }
346
347 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht)
348 {
349 int retval;
350
351 begin_write(ht);
ba364121 »
2002-06-10 added TS_HASH macro
352 retval = zend_hash_rehash(TS_HASH(ht));
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
353 end_write(ht);
354
355 return retval;
356 }
357
358 #if ZEND_DEBUG
359 void zend_ts_hash_display_pListTail(TsHashTable *ht)
360 {
361 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
362 zend_hash_display_pListTail(TS_HASH(ht));
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
363 end_read(ht);
364 }
365
366 void zend_ts_hash_display(TsHashTable *ht)
367 {
368 begin_read(ht);
ba364121 »
2002-06-10 added TS_HASH macro
369 zend_hash_display(TS_HASH(ht));
d5e64b22 »
2002-03-20 added thread safe hashtable which allows concurrent
370 end_read(ht);
371 }
3e3ae4f6 »
2002-03-23 - Fix build without ZTS. If someone has a nicer fix let me know.
372 #endif
333406bd »
2003-02-01 - Added some missing CVS $Id$ tags, headers and footers.
373
374 /*
375 * Local variables:
376 * tab-width: 4
377 * c-basic-offset: 4
378 * indent-tabs-mode: t
379 * End:
380 */
Something went wrong with that request. Please try again.