Jump to content

Archived

This topic is now archived and is closed to further replies.

thinking

C forwarding zend PHP_FUNCTION parameters to a (php)thread?

Recommended Posts

hiho@ll

i was interested in creating threads in php (don't know for what, just for fun now ;-) )
what i want:
[code]
<?php
  function testfunc(){
        print("IT WORKS");
  }
  create_thread("testfunc");
?>
[/code]

explanation: the above code, creates a thread, the thread runs and prints "IT WORKS" (1)
the same stuff should work with objects and some parameters would be cool, to give the thread function (2)

the example (1) works, that's not my problem now
my problem is (2)
using objects, and parameters

the code i have:
[CODE]
/* {{{ PHP_MCLS_CREATESOCKET
*/
PHP_FUNCTION(create_thread)
{
    zval *functionname;

    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"z",&functionname)==FAILURE){
        RETURN_FALSE;
    }

    switch(Z_TYPE_P(functionname)){
        case IS_STRING:{
                      pthread_t mclsth;
                      pthread_create(&mclsth,NULL,thread_func,functionname->value.str.val);
                      pthread_detach(mclsth);
                      RETURN_TRUE;
                      }
        default: RETURN_FALSE;
    }
  RETURN_TRUE;
}
/* }}} */

void* thread_func(void *arg){
    char *funcname = (char*)arg;
    zval *func_name;
    zval *return_value;

    if(funcname == NULL){
        zend_error(E_ERROR,"MCLS: Function is null");
    }

    MAKE_STD_ZVAL(func_name);

    ZVAL_STRING(func_name, "testfunc", 1);

    ZEND_SET_SYMBOL(EG(active_symbol_table), "func_name", func_name);

    TSRMLS_FETCH();

    if(call_user_function_ex(CG(function_table), NULL, func_name, &return_value, 0, NULL, 0,&EG(symbol_table)) != FAILURE){
        return NULL;
    }

    zend_error(E_ERROR,"Couldn't call function %s",funcname);

    return NULL;
}
[/CODE]
this all works with my extension
but since i'm a noob in programming extensions i have no idea

how can i differentiate between
  create_thread("myfunc");
  OR
  create_thread(array($object,"myfunc"));

  and how can i make "forward" this to thread_func?

  i think i will need something like a wrapper structure, which holds my function call data and parameters
  but since i'm a zendnoob i have no idea, how the php memory management works
  and that's why i'm not sure how it works

to be more concrete:
Q1: what is missing for my C code to accept object methods or functions? (example code would be great)
Q2: what is missing for my C code to accept parameters for the threadfunction?
Q3: when, how and why do i have to free some zval vars?

[edit]
i forgot to mention that i'm running php-5.1.4
[/edit]

thx@ll

Share this post


Link to post
Share on other sites
For the Threading matter read this
[url=http://www.phpfreaks.com/forums/index.php?topic=155604.0]http://www.phpfreaks.com/forums/index.php?topic=155604.0[/url]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.