pike.git / src / threads.c

version» Context lines:

pike.git/src/threads.c:1:   #include "global.h" - RCSID("$Id: threads.c,v 1.87 1999/02/01 02:41:50 hubbe Exp $"); + RCSID("$Id: threads.c,v 1.88 1999/02/01 04:11:35 hubbe Exp $");      int num_threads = 1;   int threads_disabled = 0;      #ifdef _REENTRANT   #include "threads.h"   #include "array.h"   #include "mapping.h"   #include "object.h"   #include "pike_macros.h"
pike.git/src/threads.c:145:   int thread_id_result_variable;      MUTEX_T interpreter_lock, thread_table_lock, interleave_lock;   struct program *mutex_key = 0;   struct program *thread_id_prog = 0;   struct program *thread_local_prog = 0;   #ifdef POSIX_THREADS   pthread_attr_t pattr;   pthread_attr_t small_pattr;   #endif + int thread_storage_offset;      struct thread_starter   {    struct object *id;    struct array *args;   };      struct thread_local   {    INT32 id;
pike.git/src/threads.c:325:    thread_table_chains[x] = NULL;   }      unsigned INT32 thread_table_hash(THREAD_T *tid)   {    return th_hash(*tid) % THREAD_TABLE_SIZE;   }      void thread_table_insert(struct object *o)   { -  struct thread_state *s = (struct thread_state *)o->storage; +  struct thread_state *s = OBJ2THREAD(o);    unsigned INT32 h = thread_table_hash(&s->id);   #ifdef PIKE_DEBUG    if(h>=THREAD_TABLE_SIZE)    fatal("thread_table_hash failed miserably!\n");   #endif    mt_lock( & thread_table_lock );    if((s->hashlink = thread_table_chains[h]) != NULL)    s->hashlink->backlink = &s->hashlink;    thread_table_chains[h] = s;    s->backlink = &thread_table_chains[h];    mt_unlock( & thread_table_lock );   }      void thread_table_delete(struct object *o)   { -  struct thread_state *s = (struct thread_state *)o->storage; +  struct thread_state *s = OBJ2THREAD(o);    mt_lock( & thread_table_lock );    if(s->hashlink != NULL)    s->hashlink->backlink = s->backlink;    *(s->backlink) = s->hashlink;    mt_unlock( & thread_table_lock );   }      struct thread_state *thread_state_for_id(THREAD_T tid)   {    unsigned INT32 h = thread_table_hash(&tid);
pike.git/src/threads.c:449:    JMP_BUF back;    INT32 tmp;       THREADS_FPRINTF(0, (stderr,"THREADS_DISALLOW() Thread %08x created...\n",    (unsigned int)arg.id));       if((tmp=mt_lock( & interpreter_lock)))    fatal("Failed to lock interpreter, errno %d\n",tmp);    init_interpreter();    thread_id=arg.id; -  SWAP_OUT_THREAD((struct thread_state *)thread_id->storage); /* Init struct */ -  ((struct thread_state *)thread_id->storage)->swapped=0; +  SWAP_OUT_THREAD(OBJ2THREAD(thread_id)); /* Init struct */ +  OBJ2THREAD(thread_id)->swapped=0;    stack_top=((char *)&data)+ (thread_stack_size-16384) * STACK_DIRECTION   #ifdef THREAD_TRACE    {    t_flag = default_t_flag;    }   #endif /* THREAD_TRACE */       THREADS_FPRINTF(0, (stderr,"THREAD %08x INITED\n",(unsigned int)thread_id));    if(SETJMP(back))    {
pike.git/src/threads.c:488:    arg.args=0;    f_call_function(args);       /* copy return value to the thread_id here */    object_low_set_index(thread_id,    thread_id_result_variable,    sp-1);    pop_stack();    }    -  if(((struct thread_state *)(thread_id->storage))->thread_local != NULL) { -  free_mapping(((struct thread_state *)(thread_id->storage))->thread_local); -  ((struct thread_state *)(thread_id->storage))->thread_local = NULL; +  if(OBJ2THREAD(thread_id)->thread_local != NULL) { +  free_mapping(OBJ2THREAD(thread_id)->thread_local); +  OBJ2THREAD(thread_id)->thread_local = NULL;    }    -  ((struct thread_state *)(thread_id->storage))->status=THREAD_EXITED; -  co_broadcast(& ((struct thread_state *)(thread_id->storage))->status_change); +  OBJ2THREAD(thread_id)->status=THREAD_EXITED; +  co_broadcast(& OBJ2THREAD(thread_id)->status_change);       free((char *)data); /* Moved by per, to avoid some bugs.... */    UNSETJMP(back);       THREADS_FPRINTF(0, (stderr,"THREADS_ALLOW() Thread %08x done\n",    (unsigned int)thread_id));       thread_table_delete(thread_id);    free_object(thread_id);    thread_id=0;
pike.git/src/threads.c:530:   #endif      void f_thread_create(INT32 args)   {    THREAD_T dummy;    struct thread_starter *arg;    int tmp;    arg=ALLOC_STRUCT(thread_starter);    arg->args=aggregate_array(args);    arg->id=clone_object(thread_id_prog,0); -  ((struct thread_state *)arg->id->storage)->status=THREAD_RUNNING; +  OBJ2THREAD(arg->id)->status=THREAD_RUNNING;    -  tmp=th_create(&((struct thread_state *)arg->id->storage)->id, +  tmp=th_create(& OBJ2THREAD(arg->id)->id,    new_thread_func,    arg);       if(!tmp)    {    num_threads++;    thread_table_insert(arg->id);       if(!threads_evaluator_callback)    {
pike.git/src/threads.c:794:    }   }      void f_cond_signal(INT32 args) { pop_n_elems(args); co_signal(THIS_COND); }   void f_cond_broadcast(INT32 args) { pop_n_elems(args); co_broadcast(THIS_COND); }   void init_cond_obj(struct object *o) { co_init(THIS_COND); }   void exit_cond_obj(struct object *o) { co_destroy(THIS_COND); }      void f_thread_backtrace(INT32 args)   { -  struct thread_state *foo = (struct thread_state *)fp->current_object->storage; -  struct thread_state *bar = (struct thread_state *)thread_id->storage; +  struct thread_state *foo = THIS_THREAD; +  struct thread_state *bar = OBJ2THREAD( thread_id );    struct svalue *osp = sp;    pop_n_elems(args);    if(foo->sp)    {    SWAP_OUT_THREAD(bar);    SWAP_IN_THREAD(foo);    sp=osp;    f_backtrace(0);    osp=sp;    sp=foo->sp;
pike.git/src/threads.c:840:    SWAP_IN_CURRENT_THREAD();       low_object_index_no_free(sp,    fp->current_object,    thread_id_result_variable);    sp++;   }      void init_thread_obj(struct object *o)   { -  MEMSET(o->storage, 0, sizeof(struct thread_state)); +  MEMSET(THIS_THREAD, 0, sizeof(struct thread_state));    THIS_THREAD->status=THREAD_NOT_STARTED;    co_init(& THIS_THREAD->status_change);    THIS_THREAD->thread_local=NULL;   }         void exit_thread_obj(struct object *o)   {    if(THIS_THREAD->thread_local != NULL) {    free_mapping(THIS_THREAD->thread_local);    THIS_THREAD->thread_local = NULL;    }    co_destroy(& THIS_THREAD->status_change);    th_destroy(& THIS_THREAD->id);   }      static void thread_was_marked(struct object *o)   { -  struct thread_state *tmp=(struct thread_state *)(o->storage); +  struct thread_state *tmp=THIS_THREAD;    if(tmp->thread_local != NULL)    gc_mark_mapping_as_referenced(tmp->thread_local);   #ifdef PIKE_DEBUG    if(tmp->swapped)    {    debug_gc_xmark_svalues(tmp->evaluator_stack,tmp->sp-tmp->evaluator_stack-1,"idle thread stack");    }   #endif   }      static void thread_was_checked(struct object *o)   { -  struct thread_state *tmp=(struct thread_state *)(o->storage); +  struct thread_state *tmp=THIS_THREAD;    if(tmp->thread_local != NULL)    gc_check(tmp->thread_local);   }      void f_thread_local(INT32 args)   {    static INT32 thread_local_id = 0;       struct object *loc = clone_object(thread_local_prog,0);    ((struct thread_local *)loc->storage)->id = thread_local_id++;
pike.git/src/threads.c:896:      void f_thread_local_get(INT32 args)   {    struct svalue key;    struct mapping *m;    key.u.integer = ((struct thread_local *)fp->current_storage)->id;    key.type = T_INT;    key.subtype = NUMBER_NUMBER;    pop_n_elems(args);    if(thread_id != NULL && -  (m = ((struct thread_state *)thread_id->storage)->thread_local) != NULL) +  (m = OBJ2THREAD(thread_id)->thread_local) != NULL)    mapping_index_no_free(sp++, m, &key);    else {    push_int(0);    sp[-1].subtype=NUMBER_UNDEFINED;    }   }      void f_thread_local_set(INT32 args)   {    struct svalue key;
pike.git/src/threads.c:919:    key.type = T_INT;    key.subtype = NUMBER_NUMBER;    if(args>1)    pop_n_elems(args-1);    else if(args<1)    error("Too few arguments to thread_local->set()\n");       if(thread_id == NULL)    error("Trying to set thread_local without thread!\n");    -  if((m = ((struct thread_state *)thread_id->storage)->thread_local) == NULL) -  m = ((struct thread_state *)thread_id->storage)->thread_local = +  if((m = OBJ2THREAD(thread_id)->thread_local) == NULL) +  m = OBJ2THREAD(thread_id)->thread_local =    allocate_mapping(4);       mapping_insert(m, &key, &sp[-1]);   }      void low_th_init(void)   {   #ifdef SGI_SPROC_THREADS   #error /* Need to specify a filename */    us_cookie = usinit("");
pike.git/src/threads.c:1035:       start_new_program();    ADD_STORAGE(struct thread_local);    add_function("get",f_thread_local_get,"function(:mixed)",0);    add_function("set",f_thread_local_set,"function(mixed:mixed)",0);    thread_local_prog=end_program();    if(!thread_local_prog)    fatal("Failed to initialize thread_local program!\n");       start_new_program(); -  ADD_STORAGE(struct thread_state); +  thread_storage_offset=ADD_STORAGE(struct thread_state);    thread_id_result_variable=simple_add_variable("result","mixed",0);    add_function("backtrace",f_thread_backtrace,"function(:array)",0);    add_function("wait",f_thread_id_result,"function(:mixed)",0);    add_function("status",f_thread_id_status,"function(:int)",0);    set_gc_mark_callback(thread_was_marked);    set_gc_check_callback(thread_was_checked);    set_init_callback(init_thread_obj);    set_exit_callback(exit_thread_obj);    thread_id_prog=end_program();       /* Some constants... */    add_integer_constant("THREAD_NOT_STARTED", THREAD_NOT_STARTED, 0);    add_integer_constant("THREAD_RUNNING", THREAD_RUNNING, 0);    add_integer_constant("THREAD_EXITED", THREAD_EXITED, 0);       if(!mutex_key)    fatal("Failed to initialize thread program!\n");       thread_id=clone_object(thread_id_prog,0); -  SWAP_OUT_THREAD((struct thread_state *)thread_id->storage); /* Init struct */ -  ((struct thread_state *)thread_id->storage)->swapped=0; -  ((struct thread_state *)thread_id->storage)->id=th_self(); +  SWAP_OUT_THREAD(OBJ2THREAD(thread_id)); /* Init struct */ +  OBJ2THREAD(thread_id)->swapped=0; +  OBJ2THREAD(thread_id)->id=th_self();    thread_table_insert(thread_id);   }      void th_cleanup(void)   {    if(mutex_key)    {    free_program(mutex_key);    mutex_key=0;    }