pike.git / lib / modules / Thread.pmod

version» Context lines:

pike.git/lib/modules/Thread.pmod:240: Inside #if constant(thread_create)
   case 'O':    return sprintf( "%t(%d)", this_object(), size() );    }    }   }            optional class Farm   { +  static Mutex mutex = Mutex(); +  static Condition ft_cond = Condition(); +  static Queue job_queue = Queue(); +     class Result    {    int ready;    mixed value;    function done_cb;       int status()    {    return ready;    }       mixed result()    {    return value;    }       mixed `()()    { -  while(!ready) ft_cond->wait(); +  object key = mutex->lock(); +  while(!ready) ft_cond->wait(key); +  key = 0;    if( ready < 0 ) throw( value );    return value;    }       void set_done_cb( function to )    {    if( ready )    to( value, ready<0 );    else    done_cb = to;
pike.git/lib/modules/Thread.pmod:302: Inside #if constant(thread_create)
   case 't':    return "Thread.Farm().Result";    case 'O':    return sprintf( "%t(%d %O)", this_object(), ready, value );    }    }    }       static class Handler    { +  Mutex job_mutex = Mutex();    Condition cond = Condition();    array(object|array(function|array)) job;    object thread;       float total_time;    int handled, max_time;       static int ready;       void handler()    {    array(object|array(function|array)) q; -  +  object key = job_mutex->lock(); +  ready = 1;    while( 1 )    { -  ready = 1; -  cond->wait(); +  cond->wait(key);    if( q = job )    {    mixed res, err;    int st = gethrtime();    if( err = catch(res = q[1][0]( @q[1][1] )) && q[0])    ([object]q[0])->provide_error( err );    else if( q[0] )    ([object]q[0])->provide( res );    object lock = mutex->lock();    free_threads += ({ this_object() });
pike.git/lib/modules/Thread.pmod:350: Inside #if constant(thread_create)
   lock = 0;    destruct();    return;    }    }    }       void run( array(function|array) what, object|void resobj )    {    while(!ready) sleep(0.1); +  object key = job_mutex->lock();    job = ({ resobj, what });    cond->signal(); -  +  key = 0;    }       string debug_status()    {    return ("Thread:\n"    " Handled works: "+handled+"\n"+    (handled?    " Average time: "+((int)(total_time / handled))+"ms\n"    " Max time: "+sprintf("%2.2fms\n", max_time/1000.0):"")+    " Status: "+(job?"Working":"Idle" )+"\n"+
pike.git/lib/modules/Thread.pmod:388: Inside #if constant(thread_create)
   switch( f )    {    case 't':    return "Thread.Farm().Handler";    case 'O':    return sprintf( "%t(%f / %d, %d)", total_time, max_time,handled );    }    }    }    -  static Mutex mutex = Mutex(); -  static Condition ft_cond = Condition(); -  static Queue job_queue = Queue(); -  +     static array(Handler) threads = ({});    static array(Handler) free_threads = ({});    static int max_num_threads = 20;       static Handler aquire_thread()    {    object lock = mutex->lock();    while( !sizeof(free_threads) )    {    if( sizeof(threads) < max_num_threads )    {    threads += ({ Handler() });    free_threads += ({ threads[-1] });    } else { -  lock = 0; -  ft_cond->wait( ); -  mutex->lock(); +  ft_cond->wait(mutex);    }    }    object(Handler) t = free_threads[0];    free_threads = free_threads[1..];    return t;    }          static void dispatcher()    {
pike.git/lib/modules/Thread.pmod:483: Inside #if constant(thread_create)
   if( to <= 0 )    error("Illegal argument 1 to set_max_num_threads,"    "num_threads must be > 0\n");       max_num_threads = to;    while( sizeof( threads ) > max_num_threads )    {    object key = mutex->lock();    while( sizeof( free_threads ) )    free_threads[0]->cond->signal(); -  key = 0; +     if( sizeof( threads ) > max_num_threads) -  ft_cond->wait(); +  ft_cond->wait(key);    }    ft_cond->broadcast( );    return omnt;    }       string debug_status()    {    string res = sprintf("Thread farm\n"    " Max threads = %d\n"    " Current threads = %d\n"