[erlang-questions] A async c driver issues: The emulator will be halted during the execution of c driver code.

tate.zhou.cn tate.zhou.cn@REDACTED
Wed Apr 2 16:25:34 CEST 2008


Hi , guys:


I worte a c driver for parsing  xml pieces. The driver works , but when the c driver invoked , the erlang emulator will be halted until the  execution of  c driver  completed.  during the execution of the c driver code, other process of erlang get no chance to be execute , evenif the usetage of cpu is around 50% (My cpu is duo core 2 ).  

Does anybody have any idea about this issues?

Any infomation about how to write a async driver is appreciate.


List below is pieces of  my erl code and c dirver code:

The execution of ?INFO_MSG in test_async_normal will start  until test_async complete, although the execution time of  test_async  is more than 2 seconds.






erlang:
==========================================

test_async_newpid() ->
    
    
    PID = spawn(?MODULE, test_async_normal,[""]),
 
    PID2 = spawn(?MODULE, test_async,[""]),
    
    ?INFO_MSG("New Pid for port communication:",[PID]),
    
    
    ok.


test_async_normal(Data) ->
    
    
    timer:sleep(2* 1000),
    ?INFO_MSG("Hi , I am in new Pid:",[erlang:self()]),
                          
    ok.

test_async (Data) ->
    
    Port = open_port({spawn, vbp_xmlhandler}, [binary]),
    
    Bin =  erlang:list_to_binary("hello , i come from erlang: "), %load_xml_bin(),
    
    
    Port ! {self(), {command, Bin}},
        
    Ret_str = return_port_data(Port),
    
    
    ?INFO_MSG("Return from port : ~p~n",[Ret_str]),
    
    % ?INFO_MSG("Return from port : ~s",[Res]),
    
    port_close(Port),

    
        
ok.



return_port_data(Port) ->
    receive
 {Port, {data, Data}} ->
     Data
    end.    


====================================================












C driver code :

-----------------------------------------------------
static ErlDrvEntry vbp_xmlhandler_entry = {
    NULL,   /* init */
    vbp_xmlhandler_start, 
    vbp_xmlhandler_stop, 
    vbp_xmlhandler_async,   /* output */
    NULL,   /* ready_input */
    NULL,   /* ready_output */ 
    "vbp_xmlhandler",                 /* the name of the driver */
    NULL,   /* finish */
    NULL,   /* handle */
    NULL, 
    NULL,   /* timeout */
    NULL,   /* outputv */
    NULL,   /* ready_async */
    NULL,   /* flush */
    NULL,   /* call */
    NULL   /* event */
};

DRIVER_INIT(vbp_xmlhandler) /* must match name in driver_entry */
{
    return &vbp_xmlhandler_entry;
}



/*
//   async interface for handle command.
//
*/
static void vbp_xmlhandler_async(ErlDrvData handle, char *buff, int bufflen){
 iostream* stream;
    char* test_data;
 char *in_data;
 int len;
 int i ;
 int loop_count;
 expat_data* d = (expat_data*)handle;

 len = bufflen;


 stream = create_stream();

 // force a lot of cpu time to be consumed in current invocation.
 loop_count = 1000000 * 80; 
 for(i = 0;i <loop_count ;i++ ){
  in_data = malloc(20);

  free(in_data);
 }

 in_data = malloc(len + 1);
 memset(in_data,0,len +1);
 memcpy(in_data,buff,len);
 write_str_to_stream(stream,in_data);
 free(in_data);

 write_str_to_stream(stream," [append by c driver]");

 driver_output(d ->port, stream->buff, stream->index); 
 
 free_stream(stream);
}





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080402/65b7141a/attachment.htm>


More information about the erlang-questions mailing list