Branch: Tag:

2002-10-01

2002-10-01 08:17:37 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Fixed condition variable handling in Thread.Farm.

Rev: lib/modules/Thread.pmod:1.34

247: Inside #if constant(thread_create)
     optional class Farm   { +  static Mutex mutex = Mutex(); +  static Condition ft_cond = Condition(); +  static Queue job_queue = Queue(); +     class Result    {    int ready;
265: Inside #if constant(thread_create)
      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;    }
309: Inside #if constant(thread_create)
      static class Handler    { +  Mutex job_mutex = Mutex();    Condition cond = Condition();    array(object|array(function|array)) job;    object thread;
321: Inside #if constant(thread_create)
   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;
357: Inside #if constant(thread_create)
   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()
395: Inside #if constant(thread_create)
   }    }    -  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;
413: Inside #if constant(thread_create)
   threads += ({ Handler() });    free_threads += ({ threads[-1] });    } else { -  lock = 0; -  ft_cond->wait( ); -  mutex->lock(); +  ft_cond->wait(mutex);    }    }    object(Handler) t = free_threads[0];
490: Inside #if constant(thread_create)
   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;