Jump to content


Photo

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


  • Please log in to reply
1 reply to this topic

#1 thinking

thinking
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 17 July 2006 - 01:09 PM

hiho@ll

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

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:
/* {{{ 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;
}
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

#2 ASDen

ASDen
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 20 August 2007 - 08:28 PM

For the Threading matter read this
http://www.phpfreaks...?topic=155604.0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users