pike.git / src / threads.c

version» Context lines:

pike.git/src/threads.c:1:   #include "global.h" - RCSID("$Id: threads.c,v 1.58 1998/03/01 03:33:50 hubbe Exp $"); + RCSID("$Id: threads.c,v 1.59 1998/03/10 03:14:55 per Exp $");      int num_threads = 1;   int threads_disabled = 0;      #ifdef _REENTRANT   #include "threads.h"   #include "array.h"   #include "object.h"   #include "pike_macros.h"   #include "callback.h"
pike.git/src/threads.c:145: Inside #if defined(POSIX_THREADS)
  pthread_attr_t pattr;   pthread_attr_t small_pattr;   #endif      struct thread_starter   {    struct object *id;    struct array *args;   };    + int threads_denied;    -  + void f_thread_disallow(INT32 args) + { +  threads_denied = sp[-1].u.integer; +  pop_n_elems(args); + } +    /* Thread hashtable */      #define THREAD_TABLE_SIZE 127 /* Totally arbitrary prime */      static struct thread_state *thread_table_chains[THREAD_TABLE_SIZE];      void thread_table_init()   {    INT32 x;    for(x=0; x<THREAD_TABLE_SIZE; x++)
pike.git/src/threads.c:265:    mt_unlock( & thread_table_lock );    f_aggregate(sp-oldsp);   }         static void check_threads(struct callback *cb, void *arg, void * arg2)   {    static int div_;    if(div_++ & 255) return;    +  if(!threads_denied) +  {    THREADS_ALLOW();       /* Allow other threads to run */       THREADS_DISALLOW();    } -  + }      void *new_thread_func(void * data)   {    struct thread_starter arg = *(struct thread_starter *)data;    JMP_BUF back;    INT32 tmp;       THREADS_FPRINTF((stderr,"THREADS_DISALLOW() Thread %08x created...\n",    (unsigned int)arg.id));   
pike.git/src/threads.c:371:    if(!tmp)    {    num_threads++;    thread_table_insert(arg->id);       if(!threads_evaluator_callback)    {    threads_evaluator_callback=add_to_callback(&evaluator_callbacks,    check_threads, 0,0);    } - #ifdef UNIX_THREADS -  if((num_lwps==1) || num_threads/3 > num_lwps) -  th_setconcurrency(++num_lwps); - #endif +     push_object(arg->id);    arg->id->refs++;    THREADS_FPRINTF((stderr,"THREAD_CREATE -> t:%08x\n",(unsigned int)arg->id));    } else {    free_object(arg->id);    free_array(arg->args);    free((char *)arg);    error("Failed to create thread.\n");    }   }
pike.git/src/threads.c:729: Inside #if defined(POSIX_THREADS)
   pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);       pthread_attr_init(&small_pattr);   #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE    pthread_attr_setstacksize(&small_pattr, 32768);   #endif    pthread_attr_setdetachstate(&small_pattr, PTHREAD_CREATE_DETACHED);      #endif    +  add_efun("thread_disallow", f_thread_disallow, "function(int:void)", +  OPT_SIDE_EFFECT); +     add_efun("thread_create",f_thread_create,"function(mixed ...:object)",    OPT_SIDE_EFFECT);   #ifdef UNIX_THREADS    add_efun("thread_set_concurrency",f_thread_set_concurrency,    "function(int:void)", OPT_SIDE_EFFECT);   #endif    add_efun("this_thread",f_this_thread,"function(:object)",    OPT_EXTERNAL_DEPEND);    add_efun("all_threads",f_all_threads,"function(:array(object))",    OPT_EXTERNAL_DEPEND);
pike.git/src/threads.c:815:    }       if(thread_id)    {    destruct(thread_id);    free_object(thread_id);    thread_id=0;    }   }    + /* Thread farm code by Per +  * +  */ + static struct farmer { +  struct farmer *neighbour; +  void *field; +  void (*harvest)(void *); +  THREAD_T me; +  COND_T harvest_moon; + } *farmers; +  + static MUTEX_T rosie; +  + static void *farm(void *_a) + { +  struct farmer *me = (struct farmer *)_a; +  do +  { + /* if(farmers == me) fatal("Ouch!\n"); */ + /* fprintf(stderr, "farm_begin %p\n",me ); */ +  me->harvest( me->field ); + /* fprintf(stderr, "farm_end %p\n", me); */ +  +  me->harvest = 0; +  mt_lock( &rosie ); +  me->neighbour = farmers; +  farmers = me; + /* fprintf(stderr, "farm_wait %p\n", me); */ +  while(!me->harvest) co_wait( &me->harvest_moon, &rosie ); +  mt_unlock( &rosie ); + /* fprintf(stderr, "farm_endwait %p\n", me); */ +  } while(1); + } +  + int th_num_idle_farmers() + { +  int q = 0; +  struct farmer *f = farmers; +  while(f) { f = f->neighbour; q++; } +  return q; + } +  + static int _num_farmers; + int th_num_farmers() + { +  return _num_farmers; + } +  + static struct farmer *new_farmer(void (*fun)(void *), void *args) + { +  struct farmer *me = malloc(sizeof(struct farmer)); +  _num_farmers++; +  me->neighbour = 0; +  me->field = args; +  me->harvest = fun; +  co_init( &me->harvest_moon ); + #ifdef UNIX_THREADS +  thr_create(NULL,8192,farm,(void *)me,THR_DAEMON|THR_DETACHED|THR_BOUND,0); + #else +  th_create_small(&me->me, farm, me);   #endif -  + } +  + void th_farm(void (*fun)(void *), void *here) + { +  if(!fun) fatal("The farmers don't known how to handle empty fields\n"); +  mt_lock( &rosie ); +  if(farmers) +  { +  struct farmer *f = farmers; +  farmers = f->neighbour; +  mt_unlock( &rosie ); +  f->field = here; +  f->harvest = fun; +  co_signal( &f->harvest_moon ); +  return; +  } +  mt_unlock( &rosie ); +  new_farmer( fun, here ); + } + #endif