pike.git / lib / modules / Thread.pmod

version» Context lines:

pike.git/lib/modules/Thread.pmod:399: Inside #if constant(thread_create)
   }   }         //! A thread farm.   optional class Farm   {    protected Mutex mutex = Mutex();    protected Condition ft_cond = Condition();    protected Queue job_queue = Queue(); +  protected object dispatcher_thread; +  protected function(object, string:void) thread_name_cb; +  protected string thread_name_prefix;       //! An asynchronous result.    class Result    {    int ready;    mixed value;    function done_cb;       //! @returns    //! @int
pike.git/lib/modules/Thread.pmod:507: Inside #if constant(thread_create)
   Mutex job_mutex = Mutex();    Condition cond = Condition();    array(object|array(function|array)) job;    object thread;       float total_time;    int handled, max_time;       protected int ready;    +  void update_thread_name(int is_exiting) +  { +  if (thread_name_cb) { +  string th_name = +  !is_exiting && +  sprintf("%s Handler 0x%x", thread_name_prefix, thread->id_number()); +  thread_name_cb(thread, th_name); +  } +  } +     void handler()    {    array(object|array(function|array)) q;    object key = job_mutex->lock();    ready = 1;    while( 1 )    {    cond->wait(key);    if( q = job )    {
pike.git/lib/modules/Thread.pmod:533: Inside #if constant(thread_create)
   object lock = mutex->lock();    free_threads += ({ this });    lock = 0;    st = gethrtime()-st;    total_time += st/1000.0;    handled++;    job = 0;    if( st > max_time )    max_time = st;    ft_cond->broadcast(); -  } else { +  } else {    object lock = mutex->lock();    threads -= ({ this });    free_threads -= ({ this });    lock = 0; -  +  update_thread_name(1);    destruct();    return;    }    }    }       void run( array(function|array) what, object|void resobj )    {    while(!ready) sleep(0.1);    object key = job_mutex->lock();
pike.git/lib/modules/Thread.pmod:573: Inside #if constant(thread_create)
   (" "+    replace( describe_backtrace(thread->backtrace()),    "\n",    "\n ")):"")    +"\n\n");    }       protected void create()    {    thread = thread_create( handler ); +  update_thread_name(0);    }          protected string _sprintf( int f )    {    switch( f )    {    case 't':    return "Thread.Farm().Handler";    case 'O':
pike.git/lib/modules/Thread.pmod:616: Inside #if constant(thread_create)
   object(Handler) t = free_threads[0];    free_threads = free_threads[1..];    return t;    }          protected void dispatcher()    {    while( array q = [array]job_queue->read() )    aquire_thread()->run( q[1], q[0] ); +  if (thread_name_cb) +  thread_name_cb(this_thread(), 0);    }       protected class ValueAdjuster( object r, object r2, int i, mapping v )    {    void go(mixed vn, int err)    {    if (!r->status()) {    ([array]r->value)[ i ] = vn;    if( err )    r->provide_error( err );
pike.git/lib/modules/Thread.pmod:769: Inside #if constant(thread_create)
   object key = mutex->lock();    while( sizeof( free_threads ) )    free_threads[0]->cond->signal();    if( sizeof( threads ) > max_num_threads)    ft_cond->wait(key);    }    ft_cond->broadcast( );    return omnt;    }    +  //! Provide a callback function to track names of threads created by the +  //! farm. +  //! +  //! @param cb +  //! The callback function. This will get invoked with the thread as the +  //! first parameter and the name as the second whenever a thread is +  //! created. When the same thread terminates the callback is invoked +  //! again with @[0] as the second parameter. Set @[cb] to @[0] to stop +  //! any previously registered callbacks from being called. +  //! +  //! @param prefix +  //! An optional name prefix to distinguish different farms. If not given +  //! a prefix will be generated automatically. +  void set_thread_name_cb(function(object, string:void) cb, void|string prefix) +  { +  thread_name_cb = cb; +  thread_name_prefix = +  cb && +  (prefix || sprintf("Thread.Farm 0x%x", dispatcher_thread->id_number())); +  +  // Give a name to all existing threads +  if (thread_name_cb) { +  thread_name_cb(dispatcher_thread, thread_name_prefix + " Dispatcher"); +  foreach (threads, Handler t) +  t->update_thread_name(0); +  } +  } +     //! Get some statistics for the thread farm.    string debug_status()    {    string res = sprintf("Thread farm\n"    " Max threads = %d\n"    " Current threads = %d\n"    " Working threads = %d\n"    " Jobs in queue = %d\n\n",    max_num_threads, sizeof(threads),    (sizeof(threads)-sizeof(free_threads)),
pike.git/lib/modules/Thread.pmod:793: Inside #if constant(thread_create)
   return res;    }       protected string _sprintf( int f )    {    return f=='O' && sprintf( "%O(/* %s */)", this_program, debug_status() );    }       protected void create()    { -  thread_create( dispatcher ); +  dispatcher_thread = thread_create( dispatcher );    }   }      //! When this key is destroyed, the corresponding resource counter   //! will be decremented.   //!   //! @seealso   //! @[ResourceCount], @[MutexKey]   //!   optional class ResourceCountKey {