Branch: Tag:

2018-04-04

2018-04-04 12:37:07 by Jonas Walldén <jonasw@roxen.com>

Added Thread.Farm()->set_thread_name_cb() to help applications that monitor
thread creation and termination for the purpose of tracking thread names.

398: Inside #if constant(__builtin.thread_id)
   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
506: Inside #if constant(__builtin.thread_id)
      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;
538: Inside #if constant(__builtin.thread_id)
   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;    }
578: Inside #if constant(__builtin.thread_id)
   protected void create()    {    thread = thread_create( handler ); +  update_thread_name(0);    }      
621: Inside #if constant(__builtin.thread_id)
   {    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 )
774: Inside #if constant(__builtin.thread_id)
   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()    {
798: Inside #if constant(__builtin.thread_id)
      protected void create()    { -  thread_create( dispatcher ); +  dispatcher_thread = thread_create( dispatcher );    }   }