pike.git / lib / modules / Thread.pmod

version» Context lines:

pike.git/lib/modules/Thread.pmod:108:    w_ptr++;    key=0; // Must free this one _before_ the signal...    r_cond::signal();    }   }            class Farm   { -  Mutex mutex = Mutex(); -  Condition ft_cond = Condition(); -  +     class Result    {    int ready;    mixed value;    function done_cb;       int status()    {    return ready;    }
pike.git/lib/modules/Thread.pmod:159:       void provide( mixed what )    {    ready = 1;    value = what;    if( done_cb )    done_cb( what, 0 );    }    }    -  class Handler +  static class Handler    {    Condition cond = Condition();    array job;    object thread;       float total_time;    int handled, max_time;       static int ready;   
pike.git/lib/modules/Thread.pmod:195:    object lock = mutex->lock();    free_threads += ({ this_object() });    lock = 0;    st = gethrtime()-st;    total_time += st/1000.0;    handled++;    job = 0;    if( st > max_time )    max_time = st;    ft_cond->broadcast(); +  } else { +  object lock = mutex->lock(); +  threads -= ({ this_object() }); +  free_threads -= ({ this_object() }); +  lock = 0; +  destruct(); +  return;    }    }    }       void run( array what, object|void resobj )    {    while(!ready) sleep(0.1);    job = ({ resobj, what });    cond->signal();    }
pike.git/lib/modules/Thread.pmod:228:    "\n ")):"")    +"\n\n");    }       void create()    {    thread = thread_create( handler );    }    }    +  static Mutex mutex = Mutex(); +  static Condition ft_cond = Condition(); +  static Queue job_queue = Queue(); +     static array(Handler) threads = ({}); -  array(Handler) free_threads = ({}); -  int max_num_threads = 20; +  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] });
pike.git/lib/modules/Thread.pmod:252:    lock = 0;    ft_cond->wait( );    mutex->lock();    }    }    object t = free_threads[0];    free_threads = free_threads[1..];    return t;    }    -  Queue job_queue = Queue(); +     -  void dispatcher() +  static void dispatcher()    {    while( array q = job_queue->read() )    aquire_thread()->run( q[1], q[0] );    }    -  object run( function f, mixed ... args ) +  static class ValueAdjuster    { -  object ro = Result(); -  job_queue->write( ({ 0, ({f, args }) }) ); -  return ro; -  } -  -  class ValueAdjuster -  { +     object r, r2;    mapping v;    int i;       void go(mixed vn, int err)    {    r->value[ i ] = vn;    if( err )    r->provide_error( err );    if( !--v->num_left )
pike.git/lib/modules/Thread.pmod:302:    mapping nl = ([ "num_left":sizeof( fun_args ) ]);    for( int i=0; i<sizeof( fun_args ); i++ )    {    Result r2 = Result();    r2->set_done_cb( ValueAdjuster( r, r2, i, nl )->go );    job_queue->write( ({ r2, fun_args[i] }) );    }    return r;    }    +  +  void run_multiple_async( array fun_args ) +  { +  for( int i=0; i<sizeof( fun_args ); i++ ) +  job_queue->write( ({ 0, fun_args[i] }) ); +  } +  +  +  object run( function f, mixed ... args ) +  { +  object ro = Result(); +  job_queue->write( ({ 0, ({f, args }) }) ); +  return ro; +  } +     void run_async( function f, mixed ... args )    {    job_queue->write( ({ 0, ({f, args }) }) );    }       int set_max_num_threads( int to )    {    int omnt = max_num_threads; -  +  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->broadcast( );    return omnt;    }       string debug_status()    {    string res = sprintf("Thread farm\n"    " Max threads = %d\n"    " Current threads = %d\n"    " Working threads = %d\n"