Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 374 lines (301 sloc) 9.454 kb
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
4223aa4 MFH: Bump year.
Sebastian Bergmann authored
5 | Copyright (c) 1998-2007 Zend Technologies Ltd. (http://www.zend.com) |
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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 |
f68c7ff updating license information in the headers.
James Cox authored
9 | available through the world-wide-web at the following url: |
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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
333406b - Added some missing CVS $Id$ tags, headers and footers.
foobar authored
19 /* $Id$ */
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
20
76aaeeb @zsuraski Improve tracking
zsuraski authored
21 #include "zend.h"
3e3ae4f @andigutmans - Fix build without ZTS. If someone has a nicer fix let me know.
andigutmans authored
22 #include "zend_ts_hash.h"
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
23
24 /* ts management functions */
25 static void begin_read(TsHashTable *ht)
26 {
54c73d1 fix non-zts build for wez
Harald Radi authored
27 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
54c73d1 fix non-zts build for wez
Harald Radi authored
33 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
34 }
35
36 static void end_read(TsHashTable *ht)
37 {
54c73d1 fix non-zts build for wez
Harald Radi authored
38 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
54c73d1 fix non-zts build for wez
Harald Radi authored
44 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
45 }
46
47 static void begin_write(TsHashTable *ht)
48 {
54c73d1 fix non-zts build for wez
Harald Radi authored
49 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
50 tsrm_mutex_lock(ht->mx_writer);
54c73d1 fix non-zts build for wez
Harald Radi authored
51 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
52 }
53
54 static void end_write(TsHashTable *ht)
55 {
54c73d1 fix non-zts build for wez
Harald Radi authored
56 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
57 tsrm_mutex_unlock(ht->mx_writer);
54c73d1 fix non-zts build for wez
Harald Radi authored
58 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
59 }
60
61 /* delegates */
76aaeeb @zsuraski Improve tracking
zsuraski authored
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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
63 {
54c73d1 fix non-zts build for wez
Harald Radi authored
64 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
65 ht->mx_reader = tsrm_mutex_alloc();
66 ht->mx_writer = tsrm_mutex_alloc();
67 ht->reader = 0;
54c73d1 fix non-zts build for wez
Harald Radi authored
68 #endif
76aaeeb @zsuraski Improve tracking
zsuraski authored
69 return _zend_hash_init(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent ZEND_FILE_LINE_RELAY_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
70 }
71
76aaeeb @zsuraski Improve tracking
zsuraski authored
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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
73 {
54c73d1 fix non-zts build for wez
Harald Radi authored
74 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
75 ht->mx_reader = tsrm_mutex_alloc();
76 ht->mx_writer = tsrm_mutex_alloc();
77 ht->reader = 0;
54c73d1 fix non-zts build for wez
Harald Radi authored
78 #endif
76aaeeb @zsuraski Improve tracking
zsuraski authored
79 return _zend_hash_init_ex(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_RELAY_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
80 }
81
82 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht)
83 {
c8cfd3b @tony2001 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
tony2001 authored
84 begin_write(ht);
85 zend_hash_destroy(TS_HASH(ht));
86 end_write(ht);
87
54c73d1 fix non-zts build for wez
Harald Radi authored
88 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
89 tsrm_mutex_free(ht->mx_reader);
c8d1985 freed reader twice instead of writer and reader
Harald Radi authored
90 tsrm_mutex_free(ht->mx_writer);
54c73d1 fix non-zts build for wez
Harald Radi authored
91 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
92 }
93
94 ZEND_API void zend_ts_hash_clean(TsHashTable *ht)
95 {
96 ht->reader = 0;
c8cfd3b @tony2001 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
tony2001 authored
97 begin_write(ht);
ba36412 added TS_HASH macro
Harald Radi authored
98 zend_hash_clean(TS_HASH(ht));
c8cfd3b @tony2001 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
tony2001 authored
99 end_write(ht);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
100 }
101
0dba58c @zsuraski Improve tracking
zsuraski authored
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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
103 {
104 int retval;
105
106 begin_write(ht);
0dba58c @zsuraski Improve tracking
zsuraski authored
107 retval = _zend_hash_add_or_update(TS_HASH(ht), arKey, nKeyLength, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
108 end_write(ht);
109
110 return retval;
111 }
112
0dba58c @zsuraski Improve tracking
zsuraski authored
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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
114 {
115 int retval;
116
117 begin_write(ht);
0dba58c @zsuraski Improve tracking
zsuraski authored
118 retval = _zend_hash_quick_add_or_update(TS_HASH(ht), arKey, nKeyLength, h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
119 end_write(ht);
120
121 return retval;
122 }
123
0dba58c @zsuraski Improve tracking
zsuraski authored
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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
125 {
126 int retval;
127
128 begin_write(ht);
0dba58c @zsuraski Improve tracking
zsuraski authored
129 retval = _zend_hash_index_update_or_next_insert(TS_HASH(ht), h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
140 retval = zend_hash_add_empty_element(TS_HASH(ht), arKey, nKeyLength);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
141 end_write(ht);
142
143 return retval;
144 }
145
146 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht)
147 {
c8cfd3b @tony2001 MFH: fix #41134 (zend_ts_hash_clean not thread-safe)
tony2001 authored
148 begin_write(ht);
149 zend_hash_graceful_destroy(TS_HASH(ht));
150 end_write(ht);
151
54c73d1 fix non-zts build for wez
Harald Radi authored
152 #ifdef ZTS
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
153 tsrm_mutex_free(ht->mx_reader);
154 tsrm_mutex_free(ht->mx_reader);
54c73d1 fix non-zts build for wez
Harald Radi authored
155 #endif
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
156 }
157
158 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC)
159 {
160 begin_write(ht);
ba36412 added TS_HASH macro
Harald Radi authored
161 zend_hash_apply(TS_HASH(ht), apply_func TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
168 zend_hash_apply_with_argument(TS_HASH(ht), apply_func, argument TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
169 end_write(ht);
170 }
171
172 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht, apply_func_args_t apply_func, int num_args, ...)
173 {
174 va_list args;
175
176 va_start(args, num_args);
177 begin_write(ht);
ba36412 added TS_HASH macro
Harald Radi authored
178 zend_hash_apply_with_arguments(TS_HASH(ht), apply_func, num_args, args);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
186 zend_hash_reverse_apply(TS_HASH(ht), apply_func TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
195 retval = zend_hash_del_key_or_index(TS_HASH(ht), arKey, nKeyLength, h, flag);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
471947b @zsuraski Reimplement PPP properties
zsuraski authored
206 retval = zend_get_hash_value(arKey, nKeyLength);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
217 retval = zend_hash_find(TS_HASH(ht), arKey, nKeyLength, pData);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
228 retval = zend_hash_quick_find(TS_HASH(ht), arKey, nKeyLength, h, pData);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
239 retval = zend_hash_index_find(TS_HASH(ht), h, pData);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
250 retval = zend_hash_exists(TS_HASH(ht), arKey, nKeyLength);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
261 retval = zend_hash_index_exists(TS_HASH(ht), h);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
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);
ba36412 added TS_HASH macro
Harald Radi authored
271 zend_hash_copy(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
272 end_write(target);
273 end_read(source);
274 }
275
276 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite)
277 {
278 begin_read(source);
279 begin_write(target);
ba36412 added TS_HASH macro
Harald Radi authored
280 zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size, overwrite);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
281 end_write(target);
282 end_read(source);
283 }
284
471947b @zsuraski Reimplement PPP properties
zsuraski authored
285 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)
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
286 {
287 begin_read(source);
288 begin_write(target);
471947b @zsuraski Reimplement PPP properties
zsuraski authored
289 zend_hash_merge_ex(TS_HASH(target), TS_HASH(source), pCopyConstructor, size, pMergeSource, pParam);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
290 end_write(target);
291 end_read(source);
292 }
293
294 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC)
295 {
296 int retval;
297
298 begin_write(ht);
ba36412 added TS_HASH macro
Harald Radi authored
299 retval = zend_hash_sort(TS_HASH(ht), sort_func, compare_func, renumber TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
300 end_write(ht);
301
302 return retval;
303 }
304
305 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC)
306 {
307 int retval;
308
309 begin_read(ht1);
310 begin_read(ht2);
ba36412 added TS_HASH macro
Harald Radi authored
311 retval = zend_hash_compare(TS_HASH(ht1), TS_HASH(ht2), compar, ordered TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
312 end_read(ht2);
313 end_read(ht1);
314
315 return retval;
316 }
317
318 ZEND_API int zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC)
319 {
320 int retval;
321
322 begin_read(ht);
ba36412 added TS_HASH macro
Harald Radi authored
323 retval = zend_hash_minmax(TS_HASH(ht), compar, flag, pData TSRMLS_CC);
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
324 end_read(ht);
325
326 return retval;
327 }
328
329 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht)
330 {
331 int retval;
332
333 begin_read(ht);
ba36412 added TS_HASH macro
Harald Radi authored
334 retval = zend_hash_num_elements(TS_HASH(ht));
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
335 end_read(ht);
336
337 return retval;
338 }
339
340 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht)
341 {
342 int retval;
343
344 begin_write(ht);
ba36412 added TS_HASH macro
Harald Radi authored
345 retval = zend_hash_rehash(TS_HASH(ht));
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
346 end_write(ht);
347
348 return retval;
349 }
350
351 #if ZEND_DEBUG
352 void zend_ts_hash_display_pListTail(TsHashTable *ht)
353 {
354 begin_read(ht);
ba36412 added TS_HASH macro
Harald Radi authored
355 zend_hash_display_pListTail(TS_HASH(ht));
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
356 end_read(ht);
357 }
358
359 void zend_ts_hash_display(TsHashTable *ht)
360 {
361 begin_read(ht);
ba36412 added TS_HASH macro
Harald Radi authored
362 zend_hash_display(TS_HASH(ht));
d5e64b2 added thread safe hashtable which allows concurrent
Harald Radi authored
363 end_read(ht);
364 }
3e3ae4f @andigutmans - Fix build without ZTS. If someone has a nicer fix let me know.
andigutmans authored
365 #endif
333406b - Added some missing CVS $Id$ tags, headers and footers.
foobar authored
366
367 /*
368 * Local variables:
369 * tab-width: 4
370 * c-basic-offset: 4
371 * indent-tabs-mode: t
372 * End:
373 */
Something went wrong with that request. Please try again.