pike.git / src / threads.c

version» Context lines:

pike.git/src/threads.c:1:   #include "global.h"   #include "threads.h"   #include "array.h"   #include "object.h" + #include "macros.h"      int num_threads = 1;   int threads_disabled = 0; -  + struct object *thread_id;      #ifdef _REENTRANT      MUTEX_T interpreter_lock;   struct program *mutex_key = 0; -  + struct program *thread_id_prog = 0;   pthread_attr_t pattr;    -  + struct thread_starter + { +  struct object *id; +  struct array *args; + }; +    void *new_thread_func(void * data)   { -  +  struct thread_starter arg = *(struct thread_starter *)data;    JMP_BUF back; -  struct array *foo; +     INT32 args; -  foo=(struct array *)data; -  args=foo->size; +  free((char *)data); +  args=arg.args->size;    mt_lock( & interpreter_lock);    init_interpreter();    -  +  thread_id=arg.id;       if(SETJMP(back))    {    exit_on_error="Error in handle_error in master object!\nPrevious error:";    assign_svalue_no_free(sp++, & throw_value);    APPLY_MASTER("handle_error", 1);    pop_stack();    automatic_fatal=0;    } else { -  push_array_items(foo); +  push_array_items(arg.args); +  arg.args=0;    f_call_function(args); -  +     }       UNSETJMP(back);    -  +  destruct(thread_id); +  free_object(thread_id); +  thread_id=0; +     cleanup_interpret(); -  mt_unlock(& interpreter_lock); +     num_threads--; -  +  mt_unlock(& interpreter_lock);    th_exit(0);   }      void f_thread_create(INT32 args)   { -  +  struct thread_starter *arg;    pthread_t dummy;    int tmp; -  struct array *a=aggregate_array(args); +  arg=ALLOC_STRUCT(thread_starter); +  arg->args=aggregate_array(args); +  arg->id=clone(thread_id_prog,0); +  tmp=th_create(&dummy,new_thread_func,arg); +  if(!tmp) +  {    num_threads++; -  tmp=th_create(&dummy,new_thread_func,a); -  if(tmp) num_threads--; -  push_int(tmp); +  push_object(arg->id); +  arg->id->refs++; +  } else { +  free_object(arg->id); +  free_array(arg->args); +  free((char *)arg); +  push_int(0);    } -  + }    -  + void f_this_thread(INT32 args) + { +  pop_n_elems(args); +  push_object(thread_id); +  thread_id->refs++; + } +    void th_init()   { -  thr_setconcurrency(9); +     mt_lock( & interpreter_lock);    pthread_attr_init(&pattr);    pthread_attr_setstacksize(&pattr, 2 * 1024 * 1204);    pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);       add_efun("thread_create",f_thread_create,"function(mixed ...:int)",OPT_SIDE_EFFECT); -  +  add_efun("this_thread",f_this_thread,"function(:object)",OPT_EXTERNAL_DEPEND);   }         #define THIS_MUTEX ((struct mutex_storage *)(fp->current_storage))         /* Note:    * No reference is kept to the key object, it is destructed if the    * mutex is destructed. The key pointer is set to zero by the    * key object when the key is destructed.
pike.git/src/threads.c:239:    add_function("trylock",f_mutex_trylock,"function(:object)",0);    set_init_callback(init_mutex_obj);    set_exit_callback(exit_mutex_obj);    end_c_program("/precompiled/mutex");       start_new_program();    add_storage(sizeof(struct key_storage));    set_init_callback(init_mutex_key_obj);    set_exit_callback(exit_mutex_key_obj);    mutex_key=end_c_program("/precompiled/mutex_key"); +  mutex_key->refs++;       start_new_program();    add_storage(sizeof(COND_T));    add_function("wait",f_cond_wait,"function(void|object:void)",0);    add_function("signal",f_cond_signal,"function(:void)",0);    add_function("broadcast",f_cond_broadcast,"function(:void)",0);    set_init_callback(init_cond_obj);    set_exit_callback(exit_cond_obj);    end_c_program("/precompiled/condition"); -  +  +  start_new_program(); +  thread_id_prog=end_c_program("/precompiled/thread"); +  thread_id_prog->refs++; +  +  thread_id=clone(thread_id_prog,0);   }    -  + void th_cleanup() + { +  if(mutex_key) +  { +  free_program(mutex_key); +  mutex_key=0; +  } +  +  if(thread_id_prog) +  { +  free_program(thread_id_prog); +  thread_id_prog=0; +  } +  +  if(thread_id) +  { +  destruct(thread_id); +  free_object(thread_id); +  thread_id=0; +  } + } +    #endif