07513e1996-10-04Fredrik Hübinette (Hubbe) #include "global.h"
9c6f7d1997-04-15Fredrik Hübinette (Hubbe) RCSID("$Id: threads.c,v 1.21 1997/04/16 03:09:18 hubbe Exp $");
a29e021996-10-15Fredrik Hübinette (Hubbe)  int num_threads = 1; int threads_disabled = 0; #ifdef _REENTRANT
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "threads.h" #include "array.h" #include "object.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
a29e021996-10-15Fredrik Hübinette (Hubbe) #include "callback.h"
9c6f7d1997-04-15Fredrik Hübinette (Hubbe) #include "builtin_functions.h" #include "constants.h"
07513e1996-10-04Fredrik Hübinette (Hubbe) 
b2a0fb1997-02-06Henrik Grubbström (Grubba) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct object *thread_id;
a29e021996-10-15Fredrik Hübinette (Hubbe) static struct callback *threads_evaluator_callback=0;
07513e1996-10-04Fredrik Hübinette (Hubbe) 
b1b51f1996-10-11Fredrik Hübinette (Hubbe) MUTEX_T interpreter_lock = PTHREAD_MUTEX_INITIALIZER;
07513e1996-10-04Fredrik Hübinette (Hubbe) struct program *mutex_key = 0;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct program *thread_id_prog = 0;
97ffe41997-01-26Per Hedbor #ifdef POSIX_THREADS
07513e1996-10-04Fredrik Hübinette (Hubbe) pthread_attr_t pattr;
97ffe41997-01-26Per Hedbor #endif
07513e1996-10-04Fredrik Hübinette (Hubbe) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct thread_starter { struct object *id; struct array *args; };
9ef9a31996-11-16Fredrik Hübinette (Hubbe) static void check_threads(struct callback *cb, void *arg, void * arg2)
a29e021996-10-15Fredrik Hübinette (Hubbe) { THREADS_ALLOW(); /* Allow other threads to run */ THREADS_DISALLOW(); }
07513e1996-10-04Fredrik Hübinette (Hubbe) void *new_thread_func(void * data) {
97ffe41997-01-26Per Hedbor /* static int dbt;*/
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  struct thread_starter arg = *(struct thread_starter *)data;
07513e1996-10-04Fredrik Hübinette (Hubbe)  JMP_BUF back;
725ba91996-10-12Fredrik Hübinette (Hubbe)  INT32 tmp;
b1b51f1996-10-11Fredrik Hübinette (Hubbe) 
97ffe41997-01-26Per Hedbor /* fprintf(stderr, "Thread create[%d]...",dbt++);*/
9c6f7d1997-04-15Fredrik Hübinette (Hubbe)  if((tmp=mt_lock( & interpreter_lock)))
725ba91996-10-12Fredrik Hübinette (Hubbe)  fatal("Failed to lock interpreter, errno %d\n",tmp);
97ffe41997-01-26Per Hedbor /* fprintf(stderr,"Created[%d]...",dbt);*/ free((char *)data); /* Moved by per, to avoid some bugs.... */
07513e1996-10-04Fredrik Hübinette (Hubbe)  init_interpreter();
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  thread_id=arg.id;
07513e1996-10-04Fredrik Hübinette (Hubbe)  if(SETJMP(back)) {
b48cc31996-10-09Fredrik Hübinette (Hubbe)  ONERROR tmp; SET_ONERROR(tmp,exit_on_error,"Error in handle_error in master object!");
07513e1996-10-04Fredrik Hübinette (Hubbe)  assign_svalue_no_free(sp++, & throw_value); APPLY_MASTER("handle_error", 1); pop_stack();
b48cc31996-10-09Fredrik Hübinette (Hubbe)  UNSET_ONERROR(tmp);
07513e1996-10-04Fredrik Hübinette (Hubbe)  } else {
725ba91996-10-12Fredrik Hübinette (Hubbe)  INT32 args=arg.args->size;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  push_array_items(arg.args); arg.args=0;
97ffe41997-01-26Per Hedbor  f_call_function(args); pop_stack(); /* Discard the return value. /Per */
07513e1996-10-04Fredrik Hübinette (Hubbe)  } UNSETJMP(back);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  destruct(thread_id);
b1b51f1996-10-11Fredrik Hübinette (Hubbe) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  free_object(thread_id); thread_id=0;
07513e1996-10-04Fredrik Hübinette (Hubbe)  cleanup_interpret(); num_threads--;
a29e021996-10-15Fredrik Hübinette (Hubbe)  if(!num_threads) { remove_callback(threads_evaluator_callback); threads_evaluator_callback=0; }
97ffe41997-01-26Per Hedbor /* fprintf(stderr,"Done[%d]\n",dbt--);*/
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  mt_unlock(& interpreter_lock);
07513e1996-10-04Fredrik Hübinette (Hubbe)  th_exit(0); }
97ffe41997-01-26Per Hedbor #ifdef UNIX_THREADS int num_lwps = 1; #endif
b1b51f1996-10-11Fredrik Hübinette (Hubbe) 
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_thread_create(INT32 args) {
97ffe41997-01-26Per Hedbor  THREAD_T dummy;
b1b51f1996-10-11Fredrik Hübinette (Hubbe)  struct thread_starter *arg;
07513e1996-10-04Fredrik Hübinette (Hubbe)  int tmp;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  arg=ALLOC_STRUCT(thread_starter); arg->args=aggregate_array(args);
e709751997-03-12Fredrik Hübinette (Hubbe)  arg->id=clone_object(thread_id_prog,0);
b2a0fb1997-02-06Henrik Grubbström (Grubba) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  tmp=th_create(&dummy,new_thread_func,arg);
b2a0fb1997-02-06Henrik Grubbström (Grubba) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  if(!tmp) { num_threads++;
a29e021996-10-15Fredrik Hübinette (Hubbe)  if(num_threads == 1 && !threads_evaluator_callback) { threads_evaluator_callback=add_to_callback(&evaluator_callbacks, check_threads, 0,0); }
97ffe41997-01-26Per Hedbor #ifdef UNIX_THREADS if((num_lwps==1) || num_threads/3 > num_lwps) th_setconcurrency(++num_lwps); #endif
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  push_object(arg->id); arg->id->refs++; } else { free_object(arg->id); free_array(arg->args); free((char *)arg); push_int(0); } }
97ffe41997-01-26Per Hedbor void f_thread_set_concurrency(INT32 args) { int c=1; if(args) c=sp[-args].u.integer; else error("No argument to thread_set_concurrency(int concurrency);\n"); pop_n_elems(args); num_threads=c; th_setconcurrency(c); }
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) void f_this_thread(INT32 args) { pop_n_elems(args); push_object(thread_id); thread_id->refs++;
07513e1996-10-04Fredrik Hübinette (Hubbe) }
5988921996-10-05Fredrik Hübinette (Hubbe) #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. */ struct mutex_storage { COND_T condition; struct object *key; }; struct key_storage { struct mutex_storage *mut; int initialized; };
b1b51f1996-10-11Fredrik Hübinette (Hubbe) static MUTEX_T mutex_kluge = PTHREAD_MUTEX_INITIALIZER;
5988921996-10-05Fredrik Hübinette (Hubbe)  #define OB2KEY(X) ((struct key_storage *)((X)->storage))
07513e1996-10-04Fredrik Hübinette (Hubbe)  void f_mutex_lock(INT32 args) {
5988921996-10-05Fredrik Hübinette (Hubbe)  struct mutex_storage *m;
07513e1996-10-04Fredrik Hübinette (Hubbe)  struct object *o;
5988921996-10-05Fredrik Hübinette (Hubbe) 
07513e1996-10-04Fredrik Hübinette (Hubbe)  pop_n_elems(args); m=THIS_MUTEX;
e709751997-03-12Fredrik Hübinette (Hubbe)  o=clone_object(mutex_key,0);
5988921996-10-05Fredrik Hübinette (Hubbe)  mt_lock(& mutex_kluge);
07513e1996-10-04Fredrik Hübinette (Hubbe)  THREADS_ALLOW();
5988921996-10-05Fredrik Hübinette (Hubbe)  while(m->key) co_wait(& m->condition, & mutex_kluge); OB2KEY(o)->mut=m; m->key=o; mt_unlock(&mutex_kluge);
07513e1996-10-04Fredrik Hübinette (Hubbe)  THREADS_DISALLOW(); push_object(o); } void f_mutex_trylock(INT32 args) {
5988921996-10-05Fredrik Hübinette (Hubbe)  struct mutex_storage *m; struct object *o; int i=0;
07513e1996-10-04Fredrik Hübinette (Hubbe)  pop_n_elems(args);
e709751997-03-12Fredrik Hübinette (Hubbe)  o=clone_object(mutex_key,0);
07513e1996-10-04Fredrik Hübinette (Hubbe)  m=THIS_MUTEX;
5988921996-10-05Fredrik Hübinette (Hubbe)  mt_lock(& mutex_kluge);
07513e1996-10-04Fredrik Hübinette (Hubbe)  THREADS_ALLOW();
5988921996-10-05Fredrik Hübinette (Hubbe)  if(!m->key) { OB2KEY(o)->mut=THIS_MUTEX; m->key=o; i=1; } mt_unlock(&mutex_kluge);
07513e1996-10-04Fredrik Hübinette (Hubbe)  THREADS_DISALLOW(); if(i) { push_object(o); } else {
5988921996-10-05Fredrik Hübinette (Hubbe)  destruct(o); free_object(o);
07513e1996-10-04Fredrik Hübinette (Hubbe)  push_int(0); } }
5988921996-10-05Fredrik Hübinette (Hubbe) void init_mutex_obj(struct object *o) { co_init(& THIS_MUTEX->condition); THIS_MUTEX->key=0; }
07513e1996-10-04Fredrik Hübinette (Hubbe) 
5988921996-10-05Fredrik Hübinette (Hubbe) void exit_mutex_obj(struct object *o) { if(THIS_MUTEX->key) destruct(THIS_MUTEX->key); co_destroy(& THIS_MUTEX->condition); } #define THIS_KEY ((struct key_storage *)(fp->current_storage)) void init_mutex_key_obj(struct object *o) { THIS_KEY->mut=0; THIS_KEY->initialized=1; }
07513e1996-10-04Fredrik Hübinette (Hubbe)  void exit_mutex_key_obj(struct object *o) {
5988921996-10-05Fredrik Hübinette (Hubbe)  mt_lock(& mutex_kluge); if(THIS_KEY->mut)
07513e1996-10-04Fredrik Hübinette (Hubbe)  {
5988921996-10-05Fredrik Hübinette (Hubbe) #ifdef DEBUG if(THIS_KEY->mut->key != o) fatal("Mutex unlock from wrong key %p != %p!\n",THIS_KEY->mut->key,o); #endif THIS_KEY->mut->key=0; co_signal(& THIS_KEY->mut->condition); THIS_KEY->mut=0; THIS_KEY->initialized=0;
07513e1996-10-04Fredrik Hübinette (Hubbe)  }
5988921996-10-05Fredrik Hübinette (Hubbe)  mt_unlock(& mutex_kluge);
07513e1996-10-04Fredrik Hübinette (Hubbe) } #define THIS_COND ((COND_T *)(fp->current_storage)) void f_cond_wait(INT32 args) { COND_T *c;
5988921996-10-05Fredrik Hübinette (Hubbe)  struct object *key;
07513e1996-10-04Fredrik Hübinette (Hubbe)  if(args > 1) pop_n_elems(args - 1);
5988921996-10-05Fredrik Hübinette (Hubbe)  c=THIS_COND; if(args > 0)
07513e1996-10-04Fredrik Hübinette (Hubbe)  {
5988921996-10-05Fredrik Hübinette (Hubbe)  struct mutex_storage *mut; if(sp[-1].type != T_OBJECT)
07513e1996-10-04Fredrik Hübinette (Hubbe)  error("Bad argument 1 to condition->wait()\n");
5988921996-10-05Fredrik Hübinette (Hubbe)  key=sp[-1].u.object; if(key->prog != mutex_key) error("Bad argument 1 to condition->wait()\n"); mt_lock(&mutex_kluge); mut=OB2KEY(key)->mut; THREADS_ALLOW(); /* Unlock mutex */ mut->key=0; OB2KEY(key)->mut=0; co_signal(& mut->condition);
07513e1996-10-04Fredrik Hübinette (Hubbe) 
5988921996-10-05Fredrik Hübinette (Hubbe)  /* Wait and allow mutex operations */ co_wait(c,&mutex_kluge); if(OB2KEY(key)->initialized) { /* Lock mutex */ while(mut->key) co_wait(& mut->condition, & mutex_kluge); mut->key=key; OB2KEY(key)->mut=mut; } mt_unlock(&mutex_kluge); THREADS_DISALLOW();
07513e1996-10-04Fredrik Hübinette (Hubbe)  pop_stack();
5988921996-10-05Fredrik Hübinette (Hubbe)  } else { THREADS_ALLOW(); co_wait(c, 0); THREADS_DISALLOW();
07513e1996-10-04Fredrik Hübinette (Hubbe)  } } 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); }
e4419e1997-02-06Fredrik Hübinette (Hubbe) void th_init()
07513e1996-10-04Fredrik Hübinette (Hubbe) {
e4419e1997-02-06Fredrik Hübinette (Hubbe)  struct program *tmp; #ifdef SGI_SPROC_THREADS #error /* Need to specify a filename */ us_cookie = usinit(""); #endif /* SGI_SPROC_THREADS */ mt_lock( & interpreter_lock); #ifdef POSIX_THREADS pthread_attr_init(&pattr); #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE pthread_attr_setstacksize(&pattr, 2 * 1024 * 1204); #endif pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED); #endif 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);
07513e1996-10-04Fredrik Hübinette (Hubbe)  start_new_program();
5988921996-10-05Fredrik Hübinette (Hubbe)  add_storage(sizeof(struct mutex_storage));
07513e1996-10-04Fredrik Hübinette (Hubbe)  add_function("lock",f_mutex_lock,"function(:object)",0); add_function("trylock",f_mutex_trylock,"function(:object)",0); set_init_callback(init_mutex_obj); set_exit_callback(exit_mutex_obj);
b98eb31997-02-11Henrik Grubbström (Grubba)  end_class("mutex", 0);
07513e1996-10-04Fredrik Hübinette (Hubbe)  start_new_program();
5988921996-10-05Fredrik Hübinette (Hubbe)  add_storage(sizeof(struct key_storage)); set_init_callback(init_mutex_key_obj);
07513e1996-10-04Fredrik Hübinette (Hubbe)  set_exit_callback(exit_mutex_key_obj);
e4419e1997-02-06Fredrik Hübinette (Hubbe)  mutex_key=end_program();
f839fa1997-02-28Fredrik Hübinette (Hubbe)  if(!mutex_key) fatal("Failed to initialize mutex_key program!\n");
07513e1996-10-04Fredrik Hübinette (Hubbe)  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);
b98eb31997-02-11Henrik Grubbström (Grubba)  end_class("condition", 0);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  start_new_program();
e4419e1997-02-06Fredrik Hübinette (Hubbe)  thread_id_prog=end_program();
f839fa1997-02-28Fredrik Hübinette (Hubbe)  if(!mutex_key) fatal("Failed to initialize thread program!\n");
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) 
e709751997-03-12Fredrik Hübinette (Hubbe)  thread_id=clone_object(thread_id_prog,0);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) } 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; }
07513e1996-10-04Fredrik Hübinette (Hubbe) } #endif