thread safety
[m6w6/ext-http] / php_http_persistent_handle.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id: http_persistent_handle_api.c 292841 2009-12-31 08:48:57Z mike $ */
14
15 #include "php_http.h"
16
17 #ifndef PHP_HTTP_DEBUG_PHANDLES
18 # define PHP_HTTP_DEBUG_PHANDLES 0
19 #endif
20 #if PHP_HTTP_DEBUG_PHANDLES
21 # undef inline
22 # define inline
23 #endif
24
25 static HashTable php_http_persistent_handles_hash;
26 #ifdef ZTS
27 # define LOCK() tsrm_mutex_lock(php_http_persistent_handles_lock)
28 # define UNLOCK() tsrm_mutex_unlock(php_http_persistent_handles_lock)
29 static MUTEX_T php_http_persistent_handles_lock;
30 #else
31 # define LOCK()
32 # define UNLOCK()
33 #endif
34
35 typedef struct php_http_persistent_handle_list {
36 HashTable free;
37 ulong used;
38 } php_http_persistent_handle_list_t;
39
40 typedef struct php_http_persistent_handle_provider {
41 php_http_persistent_handle_list_t list; /* "ident" => array(handles) entries */
42 php_http_resource_factory_t rf;
43 } php_http_persistent_handle_provider_t;
44
45 static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_init(php_http_persistent_handle_list_t *list)
46 {
47 int free_list;
48
49 if ((free_list = !list)) {
50 list = pemalloc(sizeof(php_http_persistent_handle_list_t), 1);
51 }
52
53 list->used = 0;
54
55 if (SUCCESS != zend_hash_init(&list->free, 0, NULL, NULL, 1)) {
56 if (free_list) {
57 pefree(list, 1);
58 }
59 list = NULL;
60 }
61
62 return list;
63 }
64
65 static inline void php_http_persistent_handle_list_dtor(php_http_persistent_handle_list_t *list, php_http_persistent_handle_provider_t *provider TSRMLS_DC)
66 {
67 HashPosition pos;
68 void **handle;
69
70 #if PHP_HTTP_DEBUG_PHANDLES
71 fprintf(stderr, "LSTDTOR: %p\n", list);
72 #endif
73 FOREACH_HASH_VAL(pos, &list->free, handle) {
74 #if PHP_HTTP_DEBUG_PHANDLES
75 fprintf(stderr, "DESTROY: %p\n", *handle);
76 #endif
77
78 provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC);
79 }
80 zend_hash_destroy(&list->free);
81 }
82
83 static inline void php_http_persistent_handle_list_free(php_http_persistent_handle_list_t **list, php_http_persistent_handle_provider_t *provider TSRMLS_DC)
84 {
85 php_http_persistent_handle_list_dtor(*list, provider TSRMLS_CC);
86 #if PHP_HTTP_DEBUG_PHANDLES
87 fprintf(stderr, "LSTFREE: %p\n", *list);
88 #endif
89 pefree(*list, 1);
90 *list = NULL;
91 }
92
93 static inline php_http_persistent_handle_list_t *php_http_persistent_handle_list_find(php_http_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len TSRMLS_DC)
94 {
95 php_http_persistent_handle_list_t **list, *new_list;
96
97 if (SUCCESS == zend_hash_find(&provider->list.free, ident_str, ident_len + 1, (void *) &list)) {
98 #if PHP_HTTP_DEBUG_PHANDLES
99 fprintf(stderr, "LSTFIND: %p\n", *list);
100 #endif
101 return *list;
102 }
103
104 if ((new_list = php_http_persistent_handle_list_init(NULL))) {
105 if (SUCCESS == zend_hash_add(&provider->list.free, ident_str, ident_len + 1, (void *) &new_list, sizeof(php_http_persistent_handle_list_t *), (void *) &list)) {
106 #if PHP_HTTP_DEBUG_PHANDLES
107 fprintf(stderr, "LSTFIND: %p (new)\n", *list);
108 #endif
109 return *list;
110 }
111 php_http_persistent_handle_list_free(&new_list, provider TSRMLS_CC);
112 }
113
114 return NULL;
115 }
116
117 static inline STATUS php_http_persistent_handle_do_acquire(php_http_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC)
118 {
119 ulong index;
120 void **handle_ptr;
121 php_http_persistent_handle_list_t *list;
122
123 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
124 zend_hash_internal_pointer_end(&list->free);
125 if (HASH_KEY_NON_EXISTANT != zend_hash_get_current_key(&list->free, NULL, &index, 0) && SUCCESS == zend_hash_get_current_data(&list->free, (void *) &handle_ptr)) {
126 *handle = *handle_ptr;
127 zend_hash_index_del(&list->free, index);
128 } else {
129 *handle = provider->rf.fops.ctor(provider->rf.data TSRMLS_CC);
130 }
131
132 if (*handle) {
133 ++provider->list.used;
134 ++list->used;
135 return SUCCESS;
136 }
137 } else {
138 *handle = NULL;
139 }
140
141 return FAILURE;
142 }
143
144 static inline STATUS php_http_persistent_handle_do_release(php_http_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC)
145 {
146 php_http_persistent_handle_list_t *list;
147
148 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
149 if (provider->list.used >= PHP_HTTP_G->persistent_handle.limit) {
150 provider->rf.fops.dtor(provider->rf.data, *handle TSRMLS_CC);
151 } else {
152 if (SUCCESS != zend_hash_next_index_insert(&list->free, (void *) handle, sizeof(void *), NULL)) {
153 return FAILURE;
154 }
155 }
156
157 *handle = NULL;
158 --provider->list.used;
159 --list->used;
160 return SUCCESS;
161 }
162
163 return FAILURE;
164 }
165
166 static inline STATUS php_http_persistent_handle_do_accrete(php_http_persistent_handle_provider_t *provider, const char *ident_str, size_t ident_len, void *old_handle, void **new_handle TSRMLS_DC)
167 {
168 php_http_persistent_handle_list_t *list;
169
170 if (provider->rf.fops.copy && (*new_handle = provider->rf.fops.copy(provider->rf.data, old_handle TSRMLS_CC))) {
171 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
172 ++list->used;
173 }
174 ++provider->list.used;
175 return SUCCESS;
176 }
177 return FAILURE;
178 }
179
180 static void php_http_persistent_handles_hash_dtor(void *p)
181 {
182 php_http_persistent_handle_provider_t *provider = (php_http_persistent_handle_provider_t *) p;
183 php_http_persistent_handle_list_t **list, *list_tmp;
184 HashPosition pos;
185 TSRMLS_FETCH();
186
187 FOREACH_HASH_VAL(pos, &provider->list.free, list) {
188 /* fix shutdown crash in PHP4 */
189 list_tmp = *list;
190 php_http_persistent_handle_list_free(&list_tmp, provider TSRMLS_CC);
191 }
192
193 zend_hash_destroy(&provider->list.free);
194 php_http_resource_factory_dtor(&provider->rf);
195 }
196
197 PHP_MINIT_FUNCTION(http_persistent_handle)
198 {
199 zend_hash_init(&php_http_persistent_handles_hash, 0, NULL, php_http_persistent_handles_hash_dtor, 1);
200 #ifdef ZTS
201 php_http_persistent_handles_lock = tsrm_mutex_alloc();
202 #endif
203 return SUCCESS;
204 }
205
206 PHP_MSHUTDOWN_FUNCTION(http_persistent_handle)
207 {
208 zend_hash_destroy(&php_http_persistent_handles_hash);
209 #ifdef ZTS
210 tsrm_mutex_free(php_http_persistent_handles_lock);
211 #endif
212 return SUCCESS;
213 }
214
215 PHP_HTTP_API STATUS php_http_persistent_handle_provide(const char *name_str, size_t name_len, php_http_resource_factory_ops_t *fops, void *data, void (*dtor)(void *))
216 {
217 STATUS status = FAILURE;
218 php_http_persistent_handle_provider_t provider;
219
220 LOCK();
221 if (php_http_persistent_handle_list_init(&provider.list)) {
222 if (php_http_resource_factory_init(&provider.rf, fops, data, dtor)) {
223 #if PHP_HTTP_DEBUG_PHANDLES
224 fprintf(stderr, "PROVIDE: %s\n", name_str);
225 #endif
226
227 if (SUCCESS == zend_hash_add(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider, sizeof(php_http_persistent_handle_provider_t), NULL)) {
228 status = SUCCESS;
229 } else {
230 php_http_resource_factory_dtor(&provider.rf);
231 }
232 }
233 }
234 UNLOCK();
235
236 return status;
237 }
238
239 PHP_HTTP_API php_http_persistent_handle_factory_t *php_http_persistent_handle_concede(php_http_persistent_handle_factory_t *a, const char *name_str, size_t name_len, const char *ident_str, size_t ident_len TSRMLS_DC)
240 {
241 STATUS status = FAILURE;
242 php_http_persistent_handle_factory_t *free_a = NULL;
243
244 if (!a) {
245 free_a = a = emalloc(sizeof(*a));
246 }
247 memset(a, 0, sizeof(*a));
248
249 LOCK();
250 status = zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &a->provider);
251 UNLOCK();
252
253 if (SUCCESS == status) {
254 a->ident.str = estrndup(ident_str, a->ident.len = ident_len);
255 if (free_a) {
256 a->free_on_abandon = 1;
257 }
258 } else {
259 if (free_a) {
260 efree(a);
261 }
262 a = NULL;
263 }
264
265 #if PHP_HTTP_DEBUG_PHANDLES
266 fprintf(stderr, "CONCETE: %p (%s) (%s)\n", a ? a->provider : NULL, name_str, ident_str);
267 #endif
268
269 return a;
270 }
271
272 PHP_HTTP_API void php_http_persistent_handle_abandon(php_http_persistent_handle_factory_t *a)
273 {
274 zend_bool f = a->free_on_abandon;
275
276 STR_FREE(a->ident.str);
277 memset(a, 0, sizeof(*a));
278 if (f) {
279 efree(a);
280 }
281 }
282
283 PHP_HTTP_API void *php_http_persistent_handle_acquire(php_http_persistent_handle_factory_t *a TSRMLS_DC)
284 {
285 void *handle = NULL;
286
287 LOCK();
288 php_http_persistent_handle_do_acquire(a->provider, a->ident.str, a->ident.len, &handle TSRMLS_CC);
289 UNLOCK();
290
291 return handle;
292 }
293
294 PHP_HTTP_API void *php_http_persistent_handle_accrete(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
295 {
296 void *new_handle;
297
298 LOCK();
299 php_http_persistent_handle_do_accrete(a->provider, a->ident.str, a->ident.len, handle, &new_handle TSRMLS_CC);
300 UNLOCK();
301
302 return new_handle;
303 }
304
305 PHP_HTTP_API void php_http_persistent_handle_release(php_http_persistent_handle_factory_t *a, void *handle TSRMLS_DC)
306 {
307 LOCK();
308 php_http_persistent_handle_do_release(a->provider, a->ident.str, a->ident.len, &handle TSRMLS_CC);
309 UNLOCK();
310 }
311
312 PHP_HTTP_API STATUS php_http_persistent_handle_acquire2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC)
313 {
314 STATUS status = FAILURE;
315 php_http_persistent_handle_provider_t *provider;
316
317 *handle = NULL;
318 LOCK();
319 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
320 status = php_http_persistent_handle_do_acquire(provider, ident_str, ident_len, handle TSRMLS_CC);
321 }
322 UNLOCK();
323
324 #if PHP_HTTP_DEBUG_PHANDLES
325 fprintf(stderr, "ACQUIRE: %p (%s)\n", *handle, name_str);
326 #endif
327
328 return status;
329 }
330
331 PHP_HTTP_API STATUS php_http_persistent_handle_release2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void **handle TSRMLS_DC)
332 {
333 STATUS status = FAILURE;
334 php_http_persistent_handle_provider_t *provider;
335 #if PHP_HTTP_DEBUG_PHANDLES
336 void *handle_tmp = *handle;
337 #endif
338
339 LOCK();
340 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
341 status = php_http_persistent_handle_do_release(provider, ident_str, ident_len, handle TSRMLS_CC);
342 }
343 UNLOCK();
344
345 #if PHP_HTTP_DEBUG_PHANDLES
346 fprintf(stderr, "RELEASE: %p (%s)\n", handle_tmp, name_str);
347 #endif
348
349 return status;
350 }
351
352 PHP_HTTP_API STATUS php_http_persistent_handle_accrete2(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len, void *old_handle, void **new_handle TSRMLS_DC)
353 {
354 STATUS status = FAILURE;
355 php_http_persistent_handle_provider_t *provider;
356
357 *new_handle = NULL;
358 LOCK();
359 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
360 status = php_http_persistent_handle_do_accrete(provider, ident_str, ident_len, old_handle, new_handle TSRMLS_CC);
361 }
362 UNLOCK();
363
364 #if PHP_HTTP_DEBUG_PHANDLES
365 fprintf(stderr, "ACCRETE: %p > %p (%s)\n", old_handle, *new_handle, name_str);
366 #endif
367
368 return status;
369 }
370
371 PHP_HTTP_API void php_http_persistent_handle_cleanup(const char *name_str, size_t name_len, const char *ident_str, size_t ident_len TSRMLS_DC)
372 {
373 php_http_persistent_handle_provider_t *provider;
374 php_http_persistent_handle_list_t *list, **listp;
375 HashPosition pos1, pos2;
376
377 LOCK();
378 if (name_str && name_len) {
379 if (SUCCESS == zend_hash_find(&php_http_persistent_handles_hash, name_str, name_len+1, (void *) &provider)) {
380 if (ident_str && ident_len) {
381 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
382 php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC);
383 php_http_persistent_handle_list_init(list);
384 }
385 } else {
386 FOREACH_HASH_VAL(pos1, &provider->list.free, listp) {
387 php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC);
388 php_http_persistent_handle_list_init(*listp);
389 }
390 }
391 }
392 } else {
393 FOREACH_HASH_VAL(pos1, &php_http_persistent_handles_hash, provider) {
394 if (ident_str && ident_len) {
395 if ((list = php_http_persistent_handle_list_find(provider, ident_str, ident_len TSRMLS_CC))) {
396 php_http_persistent_handle_list_dtor(list, provider TSRMLS_CC);
397 php_http_persistent_handle_list_init(list);
398 }
399 } else {
400 FOREACH_HASH_VAL(pos2, &provider->list.free, listp) {
401 php_http_persistent_handle_list_dtor(*listp, provider TSRMLS_CC);
402 php_http_persistent_handle_list_init(*listp);
403 }
404 }
405 }
406 }
407 UNLOCK();
408 }
409
410 PHP_HTTP_API HashTable *php_http_persistent_handle_statall(HashTable *ht TSRMLS_DC)
411 {
412 zval *zentry[2];
413 HashPosition pos1, pos2;
414 php_http_array_hashkey_t key1 = php_http_array_hashkey_init(0), key2 = php_http_array_hashkey_init(0);
415 php_http_persistent_handle_provider_t *provider;
416 php_http_persistent_handle_list_t **list;
417
418 LOCK();
419 if (zend_hash_num_elements(&php_http_persistent_handles_hash)) {
420 if (!ht) {
421 ALLOC_HASHTABLE(ht);
422 zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0);
423 }
424
425 FOREACH_HASH_KEYVAL(pos1, &php_http_persistent_handles_hash, key1, provider) {
426 MAKE_STD_ZVAL(zentry[0]);
427 array_init(zentry[0]);
428
429 FOREACH_HASH_KEYVAL(pos2, &provider->list.free, key2, list) {
430 MAKE_STD_ZVAL(zentry[1]);
431 array_init(zentry[1]);
432 add_assoc_long_ex(zentry[1], ZEND_STRS("used"), (*list)->used);
433 add_assoc_long_ex(zentry[1], ZEND_STRS("free"), zend_hash_num_elements(&(*list)->free));
434
435 /* use zend_hash_* not add_assoc_* (which is zend_symtable_*) as we want a string even for numbers */
436 zend_hash_add(Z_ARRVAL_P(zentry[0]), key2.str, key2.len, &zentry[1], sizeof(zval *), NULL);
437 }
438
439 zend_hash_add(ht, key1.str, key1.len, &zentry[0], sizeof(zval *), NULL);
440 }
441 } else if (ht) {
442 ht = NULL;
443 }
444 UNLOCK();
445
446 return ht;
447 }
448
449
450 /*
451 * Local variables:
452 * tab-width: 4
453 * c-basic-offset: 4
454 * End:
455 * vim600: noet sw=4 ts=4 fdm=marker
456 * vim<600: noet sw=4 ts=4
457 */
458