Back to index

php5  5.3.10
zend_ts_hash.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Harald Radi <harald.radi@nme.at>                            |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: zend_ts_hash.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "zend.h"
00022 #include "zend_ts_hash.h"
00023 
00024 /* ts management functions */
00025 static void begin_read(TsHashTable *ht)
00026 {
00027 #ifdef ZTS
00028        tsrm_mutex_lock(ht->mx_reader);
00029        if ((++(ht->reader)) == 1) {
00030               tsrm_mutex_lock(ht->mx_writer);
00031        }
00032        tsrm_mutex_unlock(ht->mx_reader);
00033 #endif
00034 }
00035 
00036 static void end_read(TsHashTable *ht)
00037 {
00038 #ifdef ZTS
00039        tsrm_mutex_lock(ht->mx_reader);
00040        if ((--(ht->reader)) == 0) {
00041               tsrm_mutex_unlock(ht->mx_writer);
00042        }
00043        tsrm_mutex_unlock(ht->mx_reader);
00044 #endif
00045 }
00046 
00047 static void begin_write(TsHashTable *ht)
00048 {
00049 #ifdef ZTS
00050        tsrm_mutex_lock(ht->mx_writer);
00051 #endif
00052 }
00053 
00054 static void end_write(TsHashTable *ht)
00055 {
00056 #ifdef ZTS
00057        tsrm_mutex_unlock(ht->mx_writer);
00058 #endif
00059 }
00060 
00061 /* delegates */
00062 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)
00063 {
00064 #ifdef ZTS
00065        ht->mx_reader = tsrm_mutex_alloc();
00066        ht->mx_writer = tsrm_mutex_alloc();
00067        ht->reader = 0;
00068 #endif
00069        return _zend_hash_init(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent ZEND_FILE_LINE_RELAY_CC);
00070 }
00071 
00072 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)
00073 {
00074 #ifdef ZTS
00075        ht->mx_reader = tsrm_mutex_alloc();
00076        ht->mx_writer = tsrm_mutex_alloc();
00077        ht->reader = 0;
00078 #endif
00079        return _zend_hash_init_ex(TS_HASH(ht), nSize, pHashFunction, pDestructor, persistent, bApplyProtection ZEND_FILE_LINE_RELAY_CC);
00080 }
00081 
00082 ZEND_API void zend_ts_hash_destroy(TsHashTable *ht)
00083 {
00084        begin_write(ht);
00085        zend_hash_destroy(TS_HASH(ht));
00086        end_write(ht);
00087 
00088 #ifdef ZTS
00089        tsrm_mutex_free(ht->mx_reader);
00090        tsrm_mutex_free(ht->mx_writer);
00091 #endif
00092 }
00093 
00094 ZEND_API void zend_ts_hash_clean(TsHashTable *ht)
00095 {
00096        ht->reader = 0;
00097        begin_write(ht);
00098        zend_hash_clean(TS_HASH(ht));
00099        end_write(ht);
00100 }
00101 
00102 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)
00103 {
00104        int retval;
00105 
00106        begin_write(ht);
00107        retval = _zend_hash_add_or_update(TS_HASH(ht), arKey, nKeyLength, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
00108        end_write(ht);
00109 
00110        return retval;
00111 }
00112 
00113 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)
00114 {
00115        int retval;
00116 
00117        begin_write(ht);
00118        retval = _zend_hash_quick_add_or_update(TS_HASH(ht), arKey, nKeyLength, h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
00119        end_write(ht);
00120 
00121        return retval;
00122 }
00123 
00124 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)
00125 {
00126        int retval;
00127 
00128        begin_write(ht);
00129        retval = _zend_hash_index_update_or_next_insert(TS_HASH(ht), h, pData, nDataSize, pDest, flag ZEND_FILE_LINE_RELAY_CC);
00130        end_write(ht);
00131 
00132        return retval;
00133 }
00134 
00135 ZEND_API int zend_ts_hash_add_empty_element(TsHashTable *ht, char *arKey, uint nKeyLength)
00136 {
00137        int retval;
00138 
00139        begin_write(ht);
00140        retval = zend_hash_add_empty_element(TS_HASH(ht), arKey, nKeyLength);
00141        end_write(ht);
00142 
00143        return retval;
00144 }
00145 
00146 ZEND_API void zend_ts_hash_graceful_destroy(TsHashTable *ht)
00147 {
00148        begin_write(ht);
00149        zend_hash_graceful_destroy(TS_HASH(ht));
00150        end_write(ht);
00151 
00152 #ifdef ZTS
00153        tsrm_mutex_free(ht->mx_reader);
00154        tsrm_mutex_free(ht->mx_reader);
00155 #endif
00156 }
00157 
00158 ZEND_API void zend_ts_hash_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC)
00159 {
00160        begin_write(ht);
00161        zend_hash_apply(TS_HASH(ht), apply_func TSRMLS_CC);
00162        end_write(ht);
00163 }
00164 
00165 ZEND_API void zend_ts_hash_apply_with_argument(TsHashTable *ht, apply_func_arg_t apply_func, void *argument TSRMLS_DC)
00166 {
00167        begin_write(ht);
00168        zend_hash_apply_with_argument(TS_HASH(ht), apply_func, argument TSRMLS_CC);
00169        end_write(ht);
00170 }
00171 
00172 ZEND_API void zend_ts_hash_apply_with_arguments(TsHashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int num_args, ...)
00173 {
00174        va_list args;
00175 
00176        va_start(args, num_args);
00177        begin_write(ht);
00178        zend_hash_apply_with_arguments(TS_HASH(ht) TSRMLS_CC, apply_func, num_args, args);
00179        end_write(ht);
00180        va_end(args);
00181 }
00182 
00183 ZEND_API void zend_ts_hash_reverse_apply(TsHashTable *ht, apply_func_t apply_func TSRMLS_DC)
00184 {
00185        begin_write(ht);
00186        zend_hash_reverse_apply(TS_HASH(ht), apply_func TSRMLS_CC);
00187        end_write(ht);
00188 }
00189 
00190 ZEND_API int zend_ts_hash_del_key_or_index(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag)
00191 {
00192        int retval;
00193 
00194        begin_write(ht);
00195        retval = zend_hash_del_key_or_index(TS_HASH(ht), arKey, nKeyLength, h, flag);
00196        end_write(ht);
00197 
00198        return retval;
00199 }
00200 
00201 ZEND_API ulong zend_ts_get_hash_value(TsHashTable *ht, char *arKey, uint nKeyLength)
00202 {
00203        ulong retval;
00204 
00205        begin_read(ht);
00206        retval = zend_get_hash_value(arKey, nKeyLength);
00207        end_read(ht);
00208 
00209        return retval;
00210 }
00211 
00212 ZEND_API int zend_ts_hash_find(TsHashTable *ht, char *arKey, uint nKeyLength, void **pData)
00213 {
00214        int retval;
00215 
00216        begin_read(ht);
00217        retval = zend_hash_find(TS_HASH(ht), arKey, nKeyLength, pData);
00218        end_read(ht);
00219 
00220        return retval;
00221 }
00222 
00223 ZEND_API int zend_ts_hash_quick_find(TsHashTable *ht, char *arKey, uint nKeyLength, ulong h, void **pData)
00224 {
00225        int retval;
00226 
00227        begin_read(ht);
00228        retval = zend_hash_quick_find(TS_HASH(ht), arKey, nKeyLength, h, pData);
00229        end_read(ht);
00230 
00231        return retval;
00232 }
00233 
00234 ZEND_API int zend_ts_hash_index_find(TsHashTable *ht, ulong h, void **pData)
00235 {
00236        int retval;
00237 
00238        begin_read(ht);
00239        retval = zend_hash_index_find(TS_HASH(ht), h, pData);
00240        end_read(ht);
00241 
00242        return retval;
00243 }
00244 
00245 ZEND_API int zend_ts_hash_exists(TsHashTable *ht, char *arKey, uint nKeyLength)
00246 {
00247        int retval;
00248 
00249        begin_read(ht);
00250        retval = zend_hash_exists(TS_HASH(ht), arKey, nKeyLength);
00251        end_read(ht);
00252 
00253        return retval;
00254 }
00255 
00256 ZEND_API int zend_ts_hash_index_exists(TsHashTable *ht, ulong h)
00257 {
00258        int retval;
00259 
00260        begin_read(ht);
00261        retval = zend_hash_index_exists(TS_HASH(ht), h);
00262        end_read(ht);
00263 
00264        return retval;
00265 }
00266 
00267 ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size)
00268 {
00269        begin_read(source);
00270        begin_write(target);
00271        zend_hash_copy(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size);
00272        end_write(target);
00273        end_read(source);
00274 }
00275 
00276 ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size)
00277 {
00278        begin_read(source);
00279        zend_hash_copy(target, TS_HASH(source), pCopyConstructor, tmp, size);
00280        end_read(source);
00281 }
00282 
00283 ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, int overwrite)
00284 {
00285        begin_read(source);
00286        begin_write(target);
00287        zend_hash_merge(TS_HASH(target), TS_HASH(source), pCopyConstructor, tmp, size, overwrite);
00288        end_write(target);
00289        end_read(source);
00290 }
00291 
00292 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)
00293 {
00294        begin_read(source);
00295        begin_write(target);
00296        zend_hash_merge_ex(TS_HASH(target), TS_HASH(source), pCopyConstructor, size, pMergeSource, pParam);
00297        end_write(target);
00298        end_read(source);
00299 }
00300 
00301 ZEND_API int zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC)
00302 {
00303        int retval;
00304 
00305        begin_write(ht);
00306        retval = zend_hash_sort(TS_HASH(ht), sort_func, compare_func, renumber TSRMLS_CC);
00307        end_write(ht);
00308 
00309        return retval;
00310 }
00311 
00312 ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC)
00313 {
00314        int retval;
00315 
00316        begin_read(ht1);
00317        begin_read(ht2);
00318        retval = zend_hash_compare(TS_HASH(ht1), TS_HASH(ht2), compar, ordered TSRMLS_CC);
00319        end_read(ht2);
00320        end_read(ht1);
00321 
00322        return retval;
00323 }
00324 
00325 ZEND_API int zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC)
00326 {
00327        int retval;
00328 
00329        begin_read(ht);
00330        retval = zend_hash_minmax(TS_HASH(ht), compar, flag, pData TSRMLS_CC);
00331        end_read(ht);
00332 
00333        return retval;
00334 }
00335 
00336 ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht)
00337 {
00338        int retval;
00339 
00340        begin_read(ht);
00341        retval = zend_hash_num_elements(TS_HASH(ht));
00342        end_read(ht);
00343 
00344        return retval;
00345 }
00346 
00347 ZEND_API int zend_ts_hash_rehash(TsHashTable *ht)
00348 {
00349        int retval;
00350 
00351        begin_write(ht);
00352        retval = zend_hash_rehash(TS_HASH(ht));
00353        end_write(ht);
00354 
00355        return retval;
00356 }
00357 
00358 #if ZEND_DEBUG
00359 void zend_ts_hash_display_pListTail(TsHashTable *ht)
00360 {
00361        begin_read(ht);
00362        zend_hash_display_pListTail(TS_HASH(ht));
00363        end_read(ht);
00364 }
00365 
00366 void zend_ts_hash_display(TsHashTable *ht)
00367 {
00368        begin_read(ht);
00369        zend_hash_display(TS_HASH(ht));
00370        end_read(ht);
00371 }
00372 #endif
00373 
00374 /*
00375  * Local variables:
00376  * tab-width: 4
00377  * c-basic-offset: 4
00378  * indent-tabs-mode: t
00379  * End:
00380  */