diff --git a/examples/StackableArray.php b/examples/StackableArray.php index f5e03495..aa900c0c 100644 --- a/examples/StackableArray.php +++ b/examples/StackableArray.php @@ -24,8 +24,6 @@ * because these arrays are meant to provide efficiency using the ArrayAccess interface is unsuitable. */ class StackableArray extends Stackable { - public $counter = 0; - /* * Always think about caching these types of objects, don't waste the run method or your workers */ @@ -44,13 +42,13 @@ public function run(){ * then don't protect these instructions * you will incurr additional unecessary locking */ - $this->test[++$this->test->counter]=rand(0, 10); + $this->test[]=rand(0, 10); } } /* create the array here for passing */ $s = new StackableArray(); /* set a pointless value */ -$s[++$s->counter]="h"; +$s[]="h"; /* show it was set */ print_r($s); $ts = array(); @@ -61,6 +59,8 @@ public function run(){ /* we want all threads to complete */ foreach($ts as $t) $t->join(); + +$s[510] = "test"; /* show it was all set without corruption */ print_r($s); ?> diff --git a/src/handlers.c b/src/handlers.c index b9285dd2..7ce6a883 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -96,10 +96,25 @@ 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; if (member == NULL) { - zend_error(E_WARNING, "pthreads cannot write an anonymous index, identify the member explicitly"); - return; + null_member = 1; + pthreads_store_lock(object TSRMLS_CC); + MAKE_STD_ZVAL(member); + ZVAL_STRING(member, "__array_counter", 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); + } + 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); + pthreads_store_unlock(object TSRMLS_CC); } if (Z_TYPE_P(member) != IS_STRING) { @@ -110,6 +125,7 @@ void pthreads_write_property(PTHREADS_WRITE_PROPERTY_PASSTHRU_D) { ); INIT_PZVAL(mstring); convert_to_string(mstring); + if(null_member) FREE_ZVAL(member); member = mstring; #if PHP_VERSION_ID > 50399 key = NULL;