Skip to content

Commit

Permalink
Merge pull request krakjoe#53 from chibisuke/master
Browse files Browse the repository at this point in the history
adding anonymous access for StackableArray (see krakjoe#52)
  • Loading branch information
krakjoe committed Jan 20, 2013
2 parents 40fa6ce + ac1214b commit 5ab5f0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 4 additions & 4 deletions examples/StackableArray.php
Expand Up @@ -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
*/
Expand All @@ -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();
Expand All @@ -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);
?>
20 changes: 18 additions & 2 deletions src/handlers.c
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 5ab5f0f

Please sign in to comment.