Skip to content

Commit

Permalink
work on consumption/leaks/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
krakjoe committed Jan 21, 2013
1 parent 223d087 commit ca72ff6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
1 change: 1 addition & 0 deletions examples/StackableArray.php
Expand Up @@ -58,6 +58,7 @@ public function run(){
while(@$i++ < $hammers){
$ts[]=new T($s);
}
printf("GOT %d T's\n", count($ts));
/* we want all threads to complete */
foreach($ts as $t)
$t->join();
Expand Down
43 changes: 25 additions & 18 deletions src/handlers.c
Expand Up @@ -96,24 +96,32 @@ zval * pthreads_read_dimension(PTHREADS_READ_DIMENSION_PASSTHRU_D) { return pthr
void pthreads_write_property(PTHREADS_WRITE_PROPERTY_PASSTHRU_D) {
PTHREAD pthreads = PTHREADS_FETCH_FROM(object);
zval *mstring = NULL;
zval *array_counter = NULL;
uint null_member = 0;
zend_bool nulled = 0;
zend_bool locked;

if (member == NULL && pthreads_lock_acquire(pthreads->store->lock, &locked TSRMLS_CC)) {
MAKE_STD_ZVAL(member);
ZVAL_STRING(member, "$", 0);
if(pthreads_store_isset(pthreads->store, Z_STRVAL_P(member), Z_STRLEN_P(member), 1 TSRMLS_CC)) {
pthreads_store_read(pthreads->store, Z_STRVAL_P(member), Z_STRLEN_P(member), &array_counter TSRMLS_CC);
} else {
MAKE_STD_ZVAL(array_counter);
ZVAL_LONG(array_counter, 0);
if (member == NULL) {
pthreads_lock_acquire(pthreads->store->lock, &locked TSRMLS_CC);
{
zval *pcounter;

MAKE_STD_ZVAL(member);

if(pthreads_store_isset(pthreads->store, "$\0", 2, 1 TSRMLS_CC)) {
pthreads_store_read(pthreads->store, "$\0", 2, &pcounter TSRMLS_CC);
} else {
MAKE_STD_ZVAL(pcounter);

ZVAL_LONG(pcounter, 0);
}

++Z_LVAL_P(pcounter);
pthreads_store_write(pthreads->store, "$\0", 2, &pcounter TSRMLS_CC);
ZVAL_LONG(member, Z_LVAL_P(pcounter)-1);

FREE_ZVAL(pcounter);

nulled = 1;
}
Z_LVAL_P(array_counter) ++;
pthreads_store_write(pthreads->store, Z_STRVAL_P(member), Z_STRLEN_P(member), &array_counter TSRMLS_CC);
ZVAL_LONG(member, Z_LVAL_P(array_counter)-1);
FREE_ZVAL(array_counter);
null_member = 1;
pthreads_lock_release(pthreads->store->lock, locked TSRMLS_CC);
}

Expand All @@ -125,7 +133,8 @@ void pthreads_write_property(PTHREADS_WRITE_PROPERTY_PASSTHRU_D) {
);
INIT_PZVAL(mstring);
convert_to_string(mstring);
if(null_member) FREE_ZVAL(member);
if(nulled)
FREE_ZVAL(member);
member = mstring;
#if PHP_VERSION_ID > 50399
key = NULL;
Expand All @@ -148,8 +157,6 @@ void pthreads_write_property(PTHREADS_WRITE_PROPERTY_PASSTHRU_D) {
"pthreads failed to write member %s::$%s",
Z_OBJCE_P(object)->name, Z_STRVAL_P(member)
);
} else {
zend_handlers->write_property(PTHREADS_WRITE_PROPERTY_PASSTHRU_C);
}
} break;

Expand Down
42 changes: 21 additions & 21 deletions src/store.c
Expand Up @@ -124,8 +124,8 @@ zend_bool pthreads_store_isset(pthreads_store store, char *key, int keyl, int ha
zend_bool locked = 0, isset = 0;
if (store) {
if (pthreads_lock_acquire(store->lock, &locked TSRMLS_CC)) {
pthreads_storage *storage;
if (zend_ts_hash_find(&store->table, key, keyl, (void**)&storage)==SUCCESS) {
pthreads_storage *storage = NULL;
if (zend_ts_hash_find(&store->table, key, keyl, (void**)&storage)==SUCCESS && storage) {
isset=((*storage)->exists);
}
pthreads_lock_release(store->lock, locked TSRMLS_CC);
Expand All @@ -143,7 +143,7 @@ int pthreads_store_read(pthreads_store store, char *key, int keyl, zval **read T
pthreads_storage *storage = NULL;
if (zend_ts_hash_find(&store->table, key, keyl, (void**)&storage)==SUCCESS && storage) {
MAKE_STD_ZVAL(*read);
if ((result = pthreads_store_convert(*storage, *read TSRMLS_CC))!=SUCCESS) {
if ((result = pthreads_store_convert((*storage), *read TSRMLS_CC))!=SUCCESS) {
FREE_ZVAL(*read);
} else Z_SET_REFCOUNT_PP(read, 0);
}
Expand All @@ -157,12 +157,12 @@ int pthreads_store_read(pthreads_store store, char *key, int keyl, zval **read T
int pthreads_store_write(pthreads_store store, char *key, int keyl, zval **write TSRMLS_DC) {
int result = FAILURE;
zend_bool locked;

if (store) {
pthreads_storage storage = pthreads_store_create(*write, 1 TSRMLS_CC);
if (storage) {
if (storage) {
if (pthreads_lock_acquire(store->lock, &locked TSRMLS_CC)) {
if (zend_ts_hash_update(&store->table, key, keyl, (void**) &storage, sizeof(pthreads_storage), NULL)==SUCCESS) {
if (zend_ts_hash_update(&store->table, key, keyl, (void**) &storage, sizeof(storage), NULL)==SUCCESS) {
if (zend_ts_hash_exists(&store->event, key, keyl)) {
pthreads_synchro *psync;
if (zend_ts_hash_find(&store->event, key, keyl, (void**)&psync)==SUCCESS) {
Expand Down Expand Up @@ -249,7 +249,7 @@ void pthreads_store_tohash(pthreads_store store, HashTable *hash TSRMLS_DC) {

zend_bool locked;
TsHashTable *safe = &store->table;

if (pthreads_lock_acquire(store->lock, &locked TSRMLS_CC)) {

HashTable *stored = TS_HASH(safe);
Expand All @@ -265,25 +265,25 @@ void pthreads_store_tohash(pthreads_store store, HashTable *hash TSRMLS_DC) {
ulong idx;

if (zend_hash_get_current_key_ex(stored, &name, &nlength, &idx, 0, &position)==HASH_KEY_IS_STRING) {
zval *pzval;

MAKE_STD_ZVAL(pzval);

if (pthreads_store_convert((*storage), pzval TSRMLS_CC)!=SUCCESS) {
ZVAL_NULL(pzval);
}

/* who knows why this happens ... no one */
if (name[nlength] != '\0') {
name[nlength]='\0';
if (name[0] != '$') { /* do not copy internal counter */
char *rename = estrndup(name, nlength);
{
zval *pzval;

MAKE_STD_ZVAL(pzval);

if (pthreads_store_convert((*storage), pzval TSRMLS_CC)!=SUCCESS) {
ZVAL_NULL(pzval);
}

zend_hash_update(hash, rename, nlength+1, &pzval, sizeof(zval), NULL);
}
efree(rename);
}

zend_hash_update(hash, name, nlength+1, &pzval, sizeof(zval), NULL);
}
}
pthreads_lock_release(store->lock, locked TSRMLS_CC);
}

} /* }}} */

/* {{{ free store storage for a thread */
Expand Down

0 comments on commit ca72ff6

Please sign in to comment.