07513e1996-10-04Fredrik Hübinette (Hubbe) #include "global.h"
1f88bf2001-09-24Henrik Grubbström (Grubba) RCSID("$Id: threads.c,v 1.166 2001/09/24 16:45:39 grubba Exp $");
a29e021996-10-15Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int num_threads = 1; PMOD_EXPORT int threads_disabled = 0;
a29e021996-10-15Fredrik Hübinette (Hubbe)  #ifdef _REENTRANT
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "threads.h" #include "array.h"
d86cd71998-08-24Marcus Comstedt #include "mapping.h"
07513e1996-10-04Fredrik Hübinette (Hubbe) #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"
be478c1997-08-30Henrik Grubbström (Grubba) #include "program.h"
3f87be1999-12-14Martin Stjernholm #include "program_id.h"
c5b96b1997-11-11Henrik Grubbström (Grubba) #include "gc.h"
3c0c281998-01-26Fredrik Hübinette (Hubbe) #include "main.h"
a49ee61999-04-02Fredrik Hübinette (Hubbe) #include "module_support.h"
52e4c61999-12-13Martin Stjernholm #include "pike_types.h"
9a0d422000-02-06Martin Stjernholm #include "operators.h"
7e8ea32000-08-13Henrik Grubbström (Grubba) #include "bignum.h"
07513e1996-10-04Fredrik Hübinette (Hubbe) 
cd11fa1999-05-13Henrik Grubbström (Grubba) #include <errno.h>
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int live_threads = 0; PMOD_EXPORT COND_T live_threads_change; PMOD_EXPORT COND_T threads_disabled_change;
78797d2001-05-31Fredrik Hübinette (Hubbe) PMOD_EXPORT size_t thread_stack_size=256 * 1204;
ef1e931998-03-26Henrik Grubbström (Grubba) 
1f88bf2001-09-24Henrik Grubbström (Grubba) /* SCO magic... */ int __thread_sys_behavior = 1;
3c55762001-09-20Fredrik Hübinette (Hubbe) #if !defined(HAVE_PTHREAD_ATFORK) && !defined(th_atfork)
71ac9e1999-08-29Fredrik Hübinette (Hubbe) #include "callback.h"
3c55762001-09-20Fredrik Hübinette (Hubbe) 
71ac9e1999-08-29Fredrik Hübinette (Hubbe) static struct callback_list atfork_prepare_callback; static struct callback_list atfork_parent_callback; static struct callback_list atfork_child_callback; int th_atfork(void (*prepare)(void),void (*parent)(void),void (*child)(void)) { if(prepare) add_to_callback(&atfork_prepare_callback, (callback_func) prepare, 0, 0); if(parent) add_to_callback(&atfork_parent_callback, (callback_func) parent, 0, 0); if(child) add_to_callback(&atfork_child_callback, (callback_func) child, 0, 0); return 0; } void th_atfork_prepare(void) { call_callback(& atfork_prepare_callback, 0); } void th_atfork_parent(void) { call_callback(& atfork_parent_callback, 0); } void th_atfork_child(void) { call_callback(& atfork_child_callback, 0); } #endif
dc7cc91998-01-14Fredrik Hübinette (Hubbe) #ifdef __NT__
7965d72001-01-24Fredrik Hübinette (Hubbe) int low_nt_create_thread(unsigned Pike_stack_size,
cd67ac1999-05-11Fredrik Hübinette (Hubbe)  unsigned (TH_STDCALL *fun)(void *), void *arg, unsigned *id) {
7965d72001-01-24Fredrik Hübinette (Hubbe)  HANDLE h = (HANDLE)_beginthreadex(NULL, Pike_stack_size, fun, arg, 0, id);
cd67ac1999-05-11Fredrik Hübinette (Hubbe)  if(h) { CloseHandle(h); return 0; } else { return 1; } }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
56f4f42001-09-18Fredrik Hübinette (Hubbe) PMOD_EXPORT HANDLE CheckValidHandle(HANDLE h);
dc7cc91998-01-14Fredrik Hübinette (Hubbe) #endif #endif
e42eaf1998-01-02Fredrik Hübinette (Hubbe) #ifdef SIMULATE_COND_WITH_EVENT
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int co_wait(COND_T *c, MUTEX_T *m)
e42eaf1998-01-02Fredrik Hübinette (Hubbe) { struct cond_t_queue me; event_init(&me.event);
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  me.next=0;
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  mt_lock(& c->lock);
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  if(c->tail) { c->tail->next=&me; c->tail=&me; }else{ c->head=c->tail=&me; }
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  mt_unlock(& c->lock); mt_unlock(m); event_wait(&me.event); mt_lock(m); event_destroy(& me.event); /* Cancellation point?? */
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  if(me.next) fatal("Wait on event return prematurely!!\n"); #endif
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  return 0; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int co_signal(COND_T *c)
e42eaf1998-01-02Fredrik Hübinette (Hubbe) { struct cond_t_queue *t; mt_lock(& c->lock);
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  if((t=c->head))
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  { c->head=t->next; t->next=0; if(!c->head) c->tail=0; } mt_unlock(& c->lock); if(t) event_signal(& t->event); return 0; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int co_broadcast(COND_T *c)
e42eaf1998-01-02Fredrik Hübinette (Hubbe) { struct cond_t_queue *t,*n; mt_lock(& c->lock); n=c->head; c->head=c->tail=0; mt_unlock(& c->lock); while((t=n)) { n=t->next;
dc7cc91998-01-14Fredrik Hübinette (Hubbe)  t->next=0;
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  event_signal(& t->event); } return 0; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int co_destroy(COND_T *c)
e42eaf1998-01-02Fredrik Hübinette (Hubbe) { struct cond_t_queue *t; mt_lock(& c->lock);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  t=c->head; mt_unlock(& c->lock); if(t) return -1; mt_destroy(& c->lock);
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  return 0; } #endif
17f08c2000-07-06Fredrik Hübinette (Hubbe) #define THIS_THREAD ((struct thread_state *)CURRENT_STORAGE)
5740881998-01-01Fredrik Hübinette (Hubbe) 
a29e021996-10-15Fredrik Hübinette (Hubbe) static struct callback *threads_evaluator_callback=0;
5740881998-01-01Fredrik Hübinette (Hubbe) int thread_id_result_variable;
07513e1996-10-04Fredrik Hübinette (Hubbe) 
b32ef12000-04-19Martin Stjernholm int th_running = 0;
c91f892000-04-19Martin Stjernholm #ifdef PIKE_DEBUG int debug_interpreter_is_locked = 0; THREAD_T debug_locking_thread; #endif
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT MUTEX_T interpreter_lock; MUTEX_T thread_table_lock, interleave_lock;
07513e1996-10-04Fredrik Hübinette (Hubbe) struct program *mutex_key = 0;
fa8c692000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT struct program *thread_id_prog = 0;
d86cd71998-08-24Marcus Comstedt struct program *thread_local_prog = 0;
97ffe41997-01-26Per Hedbor #ifdef POSIX_THREADS
07513e1996-10-04Fredrik Hübinette (Hubbe) pthread_attr_t pattr;
864d3c1998-01-29Fredrik Hübinette (Hubbe) pthread_attr_t small_pattr;
97ffe41997-01-26Per Hedbor #endif
89fc4c2000-08-10Henrik Grubbström (Grubba) PMOD_EXPORT ptrdiff_t thread_storage_offset;
07513e1996-10-04Fredrik Hübinette (Hubbe) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) struct thread_starter { struct object *id; struct array *args;
222d922000-05-20Henrik Grubbström (Grubba) #ifdef HAVE_BROKEN_LINUX_THREAD_EUID
c17fed2000-05-20Henrik Grubbström (Grubba)  int euid, egid;
222d922000-05-20Henrik Grubbström (Grubba) #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) };
d86cd71998-08-24Marcus Comstedt struct thread_local { INT32 id; };
a91ca01998-07-10Henrik Grubbström (Grubba) static volatile IMUTEX_T *interleave_list = NULL;
f328f11998-07-17Henrik Grubbström (Grubba) void low_init_threads_disable(void)
a91ca01998-07-10Henrik Grubbström (Grubba) { /* Serious black magic to avoid dead-locks */ if (!threads_disabled) {
f328f11998-07-17Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "low_init_threads_disable(): Locking IM's...\n"));
a91ca01998-07-10Henrik Grubbström (Grubba) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if (Pike_interpreter.thread_id) {
2f748a1999-02-20Henrik Grubbström (Grubba)  /* Threads have been enabled. */
a91ca01998-07-10Henrik Grubbström (Grubba)  IMUTEX_T *im; THREADS_ALLOW(); /* Keep this the entire session. */ mt_lock(&interleave_lock); im = (IMUTEX_T *)interleave_list; while(im) { mt_lock(&(im->lock)); im = im->next; } THREADS_DISALLOW(); } else {
2f748a1999-02-20Henrik Grubbström (Grubba)  /* Threads haven't been enabled yet. */
a91ca01998-07-10Henrik Grubbström (Grubba)  IMUTEX_T *im; /* Keep this the entire session. */ mt_lock(&interleave_lock); im = (IMUTEX_T *)interleave_list; while(im) { mt_lock(&(im->lock)); im = im->next; } }
f328f11998-07-17Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "low_init_threads_disable(): Disabling threads.\n"));
a91ca01998-07-10Henrik Grubbström (Grubba)  threads_disabled = 1; } else { threads_disabled++; }
f328f11998-07-17Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "low_init_threads_disable(): threads_disabled:%d\n", threads_disabled)); }
16df701998-07-16Fredrik Hübinette (Hubbe) 
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl object(_disable_threads) _disable_threads() *! *! This function first posts a notice to all threads that it is time to stop. *! It then waits until all threads actually *have* stopped, and then then *! returns an object. All other threads will be blocked from running until *! that object has been freed/destroyed. *! *! @note *! This function can completely block Pike if used incorrectly. *! Use with extreme caution. */
f328f11998-07-17Henrik Grubbström (Grubba) void init_threads_disable(struct object *o) { low_init_threads_disable(); if(live_threads) { SWAP_OUT_CURRENT_THREAD(); while (live_threads) { THREADS_FPRINTF(0, (stderr, "_disable_threads(): Waiting for %d threads to finish\n", live_threads));
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(&live_threads_change);
16df701998-07-16Fredrik Hübinette (Hubbe)  }
f328f11998-07-17Henrik Grubbström (Grubba)  SWAP_IN_CURRENT_THREAD();
a91ca01998-07-10Henrik Grubbström (Grubba)  } }
de413f1998-03-26Per Hedbor void exit_threads_disable(struct object *o) {
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "exit_threads_disable(): threads_disabled:%d\n", threads_disabled));
40e9491998-07-05Henrik Grubbström (Grubba)  if(threads_disabled) { if(!--threads_disabled) {
a91ca01998-07-10Henrik Grubbström (Grubba)  IMUTEX_T *im = (IMUTEX_T *)interleave_list; /* Order shouldn't matter for unlock, so no need to do it backwards. */ while(im) { THREADS_FPRINTF(0, (stderr, "exit_threads_disable(): Unlocking IM 0x%08p\n", im)); mt_unlock(&(im->lock)); im = im->next; } mt_unlock(&interleave_lock); THREADS_FPRINTF(0, (stderr, "_exit_threads_disable(): Wake up!\n"));
2202fe1998-04-23Fredrik Hübinette (Hubbe)  co_broadcast(&threads_disabled_change);
40e9491998-07-05Henrik Grubbström (Grubba)  }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
40e9491998-07-05Henrik Grubbström (Grubba)  } else { fatal("exit_threads_disable() called too many times!\n");
71f3a21998-11-22Fredrik Hübinette (Hubbe) #endif /* PIKE_DEBUG */
40e9491998-07-05Henrik Grubbström (Grubba)  }
de413f1998-03-26Per Hedbor }
063fe31998-03-10Per Hedbor 
a91ca01998-07-10Henrik Grubbström (Grubba) void init_interleave_mutex(IMUTEX_T *im)
063fe31998-03-10Per Hedbor {
a91ca01998-07-10Henrik Grubbström (Grubba)  mt_init(&(im->lock));
6e1db51998-07-09Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "init_interleave_mutex(): init_threads_disable()\n"));
6e1db51998-07-09Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  init_threads_disable(NULL);
6e1db51998-07-09Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "init_interleave_mutex(): Locking IM 0x%08p\n", im));
6e1db51998-07-09Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  /* Lock it so that it can be unlocked by exit_threads_disable() */ mt_lock(&(im->lock)); im->next = (IMUTEX_T *)interleave_list; if (interleave_list) { interleave_list->prev = im;
6e1db51998-07-09Henrik Grubbström (Grubba)  }
a91ca01998-07-10Henrik Grubbström (Grubba)  interleave_list = im; im->prev = NULL;
6e1db51998-07-09Henrik Grubbström (Grubba) 
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "init_interleave_mutex(): exit_threads_disable()\n")); exit_threads_disable(NULL); } void exit_interleave_mutex(IMUTEX_T *im) { init_threads_disable(NULL); if (im->prev) { im->prev->next = im->next; } else { interleave_list = im->next; } if (im->next) { im->next->prev = im->prev;
ef1e931998-03-26Henrik Grubbström (Grubba)  }
a91ca01998-07-10Henrik Grubbström (Grubba)  /* Just to be nice... */ mt_unlock(&(im->lock)); exit_threads_disable(NULL);
063fe31998-03-10Per Hedbor }
eac2091998-02-27Marcus Comstedt  /* Thread hashtable */ #define THREAD_TABLE_SIZE 127 /* Totally arbitrary prime */
17f08c2000-07-06Fredrik Hübinette (Hubbe) static struct thread_state *thread_table_chains[THREAD_TABLE_SIZE];
56ac102000-03-29Fredrik Hübinette (Hubbe) static int num_pike_threads=0;
eac2091998-02-27Marcus Comstedt 
22ca071998-04-08Fredrik Hübinette (Hubbe) void thread_table_init(void)
eac2091998-02-27Marcus Comstedt { INT32 x; for(x=0; x<THREAD_TABLE_SIZE; x++) thread_table_chains[x] = NULL; } unsigned INT32 thread_table_hash(THREAD_T *tid) {
d213271998-02-28Fredrik Hübinette (Hubbe)  return th_hash(*tid) % THREAD_TABLE_SIZE;
eac2091998-02-27Marcus Comstedt }
26cd941999-05-07Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG static void dumpmem(char *desc, void *x, int size) { int e; unsigned char *tmp=(unsigned char *)x; fprintf(stderr,"%s: ",desc); for(e=0;e<size;e++) fprintf(stderr,"%02x",tmp[e]); fprintf(stderr,"\n"); } #endif
fa8c692000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT void thread_table_insert(struct object *o)
eac2091998-02-27Marcus Comstedt {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *s = OBJ2THREAD(o);
eac2091998-02-27Marcus Comstedt  unsigned INT32 h = thread_table_hash(&s->id);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
939b181998-07-17Fredrik Hübinette (Hubbe)  if(h>=THREAD_TABLE_SIZE) fatal("thread_table_hash failed miserably!\n");
26cd941999-05-07Fredrik Hübinette (Hubbe)  if(thread_state_for_id(s->id))
3be5501999-06-08Fredrik Hübinette (Hubbe)  { if(thread_state_for_id(s->id) == s) fatal("Registring thread twice!\n"); else fatal("Forgot to unregister thread!\n"); }
26cd941999-05-07Fredrik Hübinette (Hubbe) /* dumpmem("thread_table_insert",&s->id, sizeof(THREAD_T)); */
939b181998-07-17Fredrik Hübinette (Hubbe) #endif
eac2091998-02-27Marcus Comstedt  mt_lock( & thread_table_lock );
56ac102000-03-29Fredrik Hübinette (Hubbe)  num_pike_threads++;
eac2091998-02-27Marcus Comstedt  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 ); }
fa8c692000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT void thread_table_delete(struct object *o)
eac2091998-02-27Marcus Comstedt {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *s = OBJ2THREAD(o);
26cd941999-05-07Fredrik Hübinette (Hubbe) /* dumpmem("thread_table_delete",&s->id, sizeof(THREAD_T)); */
eac2091998-02-27Marcus Comstedt  mt_lock( & thread_table_lock );
56ac102000-03-29Fredrik Hübinette (Hubbe)  num_pike_threads--;
eac2091998-02-27Marcus Comstedt  if(s->hashlink != NULL) s->hashlink->backlink = s->backlink; *(s->backlink) = s->hashlink; mt_unlock( & thread_table_lock ); }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct thread_state *thread_state_for_id(THREAD_T tid)
eac2091998-02-27Marcus Comstedt { unsigned INT32 h = thread_table_hash(&tid);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *s = NULL;
26cd941999-05-07Fredrik Hübinette (Hubbe) #if 0 if(num_threads>1) dumpmem("thread_state_for_id: ",&tid,sizeof(tid)); #endif
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
939b181998-07-17Fredrik Hübinette (Hubbe)  if(h>=THREAD_TABLE_SIZE) fatal("thread_table_hash failed miserably!\n"); #endif
eac2091998-02-27Marcus Comstedt  mt_lock( & thread_table_lock );
d213271998-02-28Fredrik Hübinette (Hubbe)  if(thread_table_chains[h] == NULL) {
eac2091998-02-27Marcus Comstedt  /* NULL result */
d213271998-02-28Fredrik Hübinette (Hubbe)  } else if(th_equal((s=thread_table_chains[h])->id, tid)) {
eac2091998-02-27Marcus Comstedt  /* Quick return */
d213271998-02-28Fredrik Hübinette (Hubbe)  } else {
eac2091998-02-27Marcus Comstedt  while((s = s->hashlink) != NULL)
d213271998-02-28Fredrik Hübinette (Hubbe)  if(th_equal(s->id, tid))
eac2091998-02-27Marcus Comstedt  break; if(s != NULL) {
ec2bab2000-06-24Fredrik Hübinette (Hubbe)  /* Move the Pike_interpreter to the head of the chain, in case
eac2091998-02-27Marcus Comstedt  we want to search for it again */ /* Unlink */ if(s->hashlink != NULL) s->hashlink->backlink = s->backlink; *(s->backlink) = s->hashlink; /* And relink at the head of the chain */ 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 );
26cd941999-05-07Fredrik Hübinette (Hubbe) #if 0 if(num_threads>1 && s) dumpmem("thread_state_for_id return value: ",&s->id,sizeof(tid)); #endif
eac2091998-02-27Marcus Comstedt  return s; /* NOTEZ BIEN: Return value only guaranteed to remain valid as long as you have the interpreter lock, unless tid == th_self() */ }
048d232001-02-27Martin Stjernholm struct thread_state *gdb_thread_state_for_id(THREAD_T tid) /* Should only be used from a debugger session. */ { unsigned INT32 h = thread_table_hash(&tid); struct thread_state *s; for (s = thread_table_chains[h]; s != NULL; s = s->hashlink) if(th_equal(s->id, tid)) break; return s; } INT32 gdb_next_thread_state(INT32 prev, struct thread_state **ts) /* Used by gdb_backtraces. */ { if (!*ts || !(*ts)->hashlink) { if (!*ts) prev = -1; while (++prev < THREAD_TABLE_SIZE) if ((*ts = thread_table_chains[prev])) return prev; *ts = NULL; return 0; } *ts = (*ts)->hashlink; return prev; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct object *thread_for_id(THREAD_T tid)
eac2091998-02-27Marcus Comstedt {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *s = thread_state_for_id(tid);
26cd941999-05-07Fredrik Hübinette (Hubbe)  return (s == NULL? NULL : THREADSTATE2OBJ(s));
eac2091998-02-27Marcus Comstedt  /* See NB in thread_state_for_id. Lifespan of result can be prolonged by incrementing refcount though. */ }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @module Thread */
95363a2000-04-11Fredrik Hübinette (Hubbe) 
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl array(Thread.Thread) all_threads() *! *! This function returns an array with the thread ids of all threads. *! *! @seealso *! @[Thread.Thread()] */
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void f_all_threads(INT32 args)
eac2091998-02-27Marcus Comstedt { /* Return an unordered array containing all threads that was running at the time this function was invoked */ INT32 x; struct svalue *oldsp;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *s;
eac2091998-02-27Marcus Comstedt  pop_n_elems(args);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  oldsp = Pike_sp;
eac2091998-02-27Marcus Comstedt  mt_lock( & thread_table_lock ); for(x=0; x<THREAD_TABLE_SIZE; x++) for(s=thread_table_chains[x]; s; s=s->hashlink) {
26cd941999-05-07Fredrik Hübinette (Hubbe)  struct object *o = THREADSTATE2OBJ(s);
d6ac731998-04-20Henrik Grubbström (Grubba)  ref_push_object(o);
eac2091998-02-27Marcus Comstedt  } mt_unlock( & thread_table_lock );
89fc4c2000-08-10Henrik Grubbström (Grubba)  f_aggregate(DO_NOT_WARN(Pike_sp - oldsp));
eac2091998-02-27Marcus Comstedt }
95363a2000-04-11Fredrik Hübinette (Hubbe) 
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT int count_pike_threads(void)
56ac102000-03-29Fredrik Hübinette (Hubbe) { return num_pike_threads; }
eac2091998-02-27Marcus Comstedt 
9ef9a31996-11-16Fredrik Hübinette (Hubbe) static void check_threads(struct callback *cb, void *arg, void * arg2)
a29e021996-10-15Fredrik Hübinette (Hubbe) {
a85ac52001-08-23Per Hedbor #ifdef HAVE_GETHRTIME static long long last_; if( gethrtime()-last_ < 50000000 ) /* 0.05s slice */ return; last_ = gethrtime(); #else
0a861b1997-09-17Fredrik Hübinette (Hubbe)  static int div_;
a85ac52001-08-23Per Hedbor  if(div_++ & 255) return; #endif
0a861b1997-09-17Fredrik Hübinette (Hubbe) 
26cd941999-05-07Fredrik Hübinette (Hubbe) #ifdef DEBUG
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(thread_for_id(th_self()) != Pike_interpreter.thread_id) fatal("thread_for_id() (or Pike_interpreter.thread_id) failed!\n") if(Pike_interpreter.backlink != OBJ2THREAD(Pike_interpreter.thread_id)) fatal("Hashlink is wrong!\n");
26cd941999-05-07Fredrik Hübinette (Hubbe) #endif
de413f1998-03-26Per Hedbor  THREADS_ALLOW(); /* Allow other threads to run */
a85ac52001-08-23Per Hedbor #ifdef HAVE_THR_YIELD thr_yield(); #endif
de413f1998-03-26Per Hedbor  THREADS_DISALLOW();
26cd941999-05-07Fredrik Hübinette (Hubbe)  #ifdef DEBUG
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(thread_for_id(th_self()) != Pike_interpreter.thread_id) fatal("thread_for_id() (or Pike_interpreter.thread_id) failed!\n")
26cd941999-05-07Fredrik Hübinette (Hubbe) #endif
a29e021996-10-15Fredrik Hübinette (Hubbe) }
26cd941999-05-07Fredrik Hübinette (Hubbe) TH_RETURN_TYPE new_thread_func(void * data)
07513e1996-10-04Fredrik Hübinette (Hubbe) {
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) 
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr,"THREADS_DISALLOW() Thread %08x created...\n", (unsigned int)arg.id));
c17fed2000-05-20Henrik Grubbström (Grubba) 
222d922000-05-20Henrik Grubbström (Grubba) #ifdef HAVE_BROKEN_LINUX_THREAD_EUID
c17fed2000-05-20Henrik Grubbström (Grubba)  /* Work-around for Linux's pthreads not propagating the * effective uid & gid. */ if (!geteuid()) {
6617302000-05-22Henrik Grubbström (Grubba)  setegid(arg.egid); seteuid(arg.euid);
c17fed2000-05-20Henrik Grubbström (Grubba)  }
222d922000-05-20Henrik Grubbström (Grubba) #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */
fe76ce1997-09-08Fredrik Hübinette (Hubbe) 
c91f892000-04-19Martin Stjernholm  if((tmp=mt_lock_interpreter()))
cd67ac1999-05-11Fredrik Hübinette (Hubbe)  fatal("Failed to lock interpreter, return value=%d, errno=%d\n",tmp, #ifdef __NT__ GetLastError() #else errno #endif );
17f08c2000-07-06Fredrik Hübinette (Hubbe)  SWAP_IN_THREAD(OBJ2THREAD(arg.id)); /* Init struct */
07513e1996-10-04Fredrik Hübinette (Hubbe)  init_interpreter();
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_interpreter.thread_id=arg.id;
0612442001-05-16Fredrik Hübinette (Hubbe) #ifdef PROFILING Pike_interpreter.stack_bottom=((char *)&data); #endif
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_interpreter.stack_top=((char *)&data)+ (thread_stack_size-16384) * STACK_DIRECTION; Pike_interpreter.recoveries = NULL; SWAP_OUT_THREAD(OBJ2THREAD(Pike_interpreter.thread_id)); /* Init struct */ OBJ2THREAD(Pike_interpreter.thread_id)->swapped=0;
f0cd4c1999-03-21Henrik Grubbström (Grubba) 
26cd941999-05-07Fredrik Hübinette (Hubbe) #if defined(PIKE_DEBUG) if(d_flag) {
f469831999-05-08Henrik Grubbström (Grubba)  THREAD_T self = th_self();
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if( Pike_interpreter.thread_id && !th_equal( OBJ2THREAD(Pike_interpreter.thread_id)->id, self) )
a4033e2000-04-14Fredrik Hübinette (Hubbe)  fatal("Current thread is wrong. %lx %lx\n",
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (long)OBJ2THREAD(Pike_interpreter.thread_id)->id, (long)self);
26cd941999-05-07Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(thread_for_id(th_self()) != Pike_interpreter.thread_id) fatal("thread_for_id() (or Pike_interpreter.thread_id) failed in new_thread_func! " "%p != %p\n", thread_for_id(self), Pike_interpreter.thread_id);
f469831999-05-08Henrik Grubbström (Grubba)  }
26cd941999-05-07Fredrik Hübinette (Hubbe) #endif
78112c1998-04-13Henrik Grubbström (Grubba) #ifdef THREAD_TRACE { t_flag = default_t_flag; } #endif /* THREAD_TRACE */
07513e1996-10-04Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  THREADS_FPRINTF(0, (stderr,"THREAD %08x INITED\n",(unsigned int)Pike_interpreter.thread_id));
07513e1996-10-04Fredrik Hübinette (Hubbe)  if(SETJMP(back)) {
61e9a01998-01-25Fredrik Hübinette (Hubbe)  if(throw_severity < THROW_EXIT)
6697042000-11-20Martin Stjernholm  call_handle_error();
61e9a01998-01-25Fredrik Hübinette (Hubbe)  if(throw_severity == THROW_EXIT) {
d40e381999-10-14Henrik Grubbström (Grubba)  free((char *) data);
eb2fd52000-11-06Henrik Grubbström (Grubba)  pike_do_exit(throw_value.u.integer);
61e9a01998-01-25Fredrik Hübinette (Hubbe)  }
07513e1996-10-04Fredrik Hübinette (Hubbe)  } else {
725ba91996-10-12Fredrik Hübinette (Hubbe)  INT32 args=arg.args->size;
61e9a01998-01-25Fredrik Hübinette (Hubbe)  back.severity=THROW_EXIT;
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  push_array_items(arg.args); arg.args=0;
97ffe41997-01-26Per Hedbor  f_call_function(args);
5740881998-01-01Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  /* copy return value to the Pike_interpreter.thread_id here */ object_low_set_index(Pike_interpreter.thread_id,
5740881998-01-01Fredrik Hübinette (Hubbe)  thread_id_result_variable,
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_sp-1);
5740881998-01-01Fredrik Hübinette (Hubbe)  pop_stack();
07513e1996-10-04Fredrik Hübinette (Hubbe)  }
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(OBJ2THREAD(Pike_interpreter.thread_id)->thread_local != NULL) { free_mapping(OBJ2THREAD(Pike_interpreter.thread_id)->thread_local); OBJ2THREAD(Pike_interpreter.thread_id)->thread_local = NULL;
d86cd71998-08-24Marcus Comstedt  }
17f08c2000-07-06Fredrik Hübinette (Hubbe)  OBJ2THREAD(Pike_interpreter.thread_id)->status=THREAD_EXITED; co_broadcast(& OBJ2THREAD(Pike_interpreter.thread_id)->status_change);
5740881998-01-01Fredrik Hübinette (Hubbe) 
4bdad01997-09-01Per Hedbor  free((char *)data); /* Moved by per, to avoid some bugs.... */
07513e1996-10-04Fredrik Hübinette (Hubbe)  UNSETJMP(back);
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr,"THREADS_ALLOW() Thread %08x done\n",
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (unsigned int)Pike_interpreter.thread_id));
b1b51f1996-10-11Fredrik Hübinette (Hubbe) 
60d9872000-03-23Fredrik Hübinette (Hubbe)  cleanup_interpret(); DO_IF_DMALLOC(
17f08c2000-07-06Fredrik Hübinette (Hubbe)  SWAP_OUT_THREAD(OBJ2THREAD(Pike_interpreter.thread_id)); /* de-Init struct */ OBJ2THREAD(Pike_interpreter.thread_id)->swapped=0;
60d9872000-03-23Fredrik Hübinette (Hubbe)  )
17f08c2000-07-06Fredrik Hübinette (Hubbe)  thread_table_delete(Pike_interpreter.thread_id); free_object(Pike_interpreter.thread_id); Pike_interpreter.thread_id=0;
07513e1996-10-04Fredrik Hübinette (Hubbe)  num_threads--;
fe76ce1997-09-08Fredrik Hübinette (Hubbe)  if(!num_threads && threads_evaluator_callback)
a29e021996-10-15Fredrik Hübinette (Hubbe)  { remove_callback(threads_evaluator_callback); threads_evaluator_callback=0; }
c91f892000-04-19Martin Stjernholm  mt_unlock_interpreter();
07513e1996-10-04Fredrik Hübinette (Hubbe)  th_exit(0);
9c8b2d1997-04-20Henrik Grubbström (Grubba)  /* NOT_REACHED, but removes a warning */
89fc4c2000-08-10Henrik Grubbström (Grubba)  return(0);
07513e1996-10-04Fredrik Hübinette (Hubbe) }
97ffe41997-01-26Per Hedbor #ifdef UNIX_THREADS int num_lwps = 1; #endif
b1b51f1996-10-11Fredrik Hübinette (Hubbe) 
e413da2001-02-01Henrik Grubbström (Grubba) /*! @class Thread */ /* @decl void create(function(mixed...:void) f, mixed ... args) *! *! This function creates a new thread which will run simultaneously *! to the rest of the program. The new thread will call the function *! @[f] with the arguments @[args]. When @[f] returns the thread will cease *! to exist. *! *! All Pike functions are 'thread safe' meaning that running *! a function at the same time from different threads will not corrupt *! any internal data in the Pike process. *! *! @returns *! The returned value will be the same as the return value of *! @[Thread.this_thread()] for the new thread. *! *! @note *! This function is only available on systems with POSIX or UNIX or WIN32 *! threads support. *! *! @seealso *! @[Mutex], @[Condition], @[this_thread()] */
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_thread_create(INT32 args) {
b1b51f1996-10-11Fredrik Hübinette (Hubbe)  struct thread_starter *arg;
07513e1996-10-04Fredrik Hübinette (Hubbe)  int tmp;
d40e381999-10-14Henrik Grubbström (Grubba)  arg = ALLOC_STRUCT(thread_starter);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  arg->args=aggregate_array(args);
e709751997-03-12Fredrik Hübinette (Hubbe)  arg->id=clone_object(thread_id_prog,0);
9b92d31999-01-31Fredrik Hübinette (Hubbe)  OBJ2THREAD(arg->id)->status=THREAD_RUNNING;
b2a0fb1997-02-06Henrik Grubbström (Grubba) 
222d922000-05-20Henrik Grubbström (Grubba) #ifdef HAVE_BROKEN_LINUX_THREAD_EUID
c17fed2000-05-20Henrik Grubbström (Grubba)  arg->euid = geteuid(); arg->egid = getegid();
222d922000-05-20Henrik Grubbström (Grubba) #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */
c17fed2000-05-20Henrik Grubbström (Grubba) 
4743f81999-06-02Fredrik Hübinette (Hubbe)  do {
d40e381999-10-14Henrik Grubbström (Grubba)  tmp = th_create(& OBJ2THREAD(arg->id)->id, new_thread_func, arg);
4743f81999-06-02Fredrik Hübinette (Hubbe)  } while( tmp == EINTR );
b2a0fb1997-02-06Henrik Grubbström (Grubba) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  if(!tmp) { num_threads++;
eac2091998-02-27Marcus Comstedt  thread_table_insert(arg->id);
a29e021996-10-15Fredrik Hübinette (Hubbe) 
fe76ce1997-09-08Fredrik Hübinette (Hubbe)  if(!threads_evaluator_callback)
a29e021996-10-15Fredrik Hübinette (Hubbe)  { threads_evaluator_callback=add_to_callback(&evaluator_callbacks, check_threads, 0,0);
424d9c1999-05-02Fredrik Hübinette (Hubbe)  dmalloc_accept_leak(threads_evaluator_callback);
a29e021996-10-15Fredrik Hübinette (Hubbe)  }
d6ac731998-04-20Henrik Grubbström (Grubba)  ref_push_object(arg->id);
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "THREAD_CREATE -> t:%08x\n", (unsigned int)arg->id));
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  } else { free_object(arg->id); free_array(arg->args); free((char *)arg);
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Failed to create thread (errno = %d).\n",tmp);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  } }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @endclass */
1e4a641997-09-03Martin Stjernholm #ifdef UNIX_THREADS
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl void thread_set_concurrency(int concurrency) */
97ffe41997-01-26Per Hedbor void f_thread_set_concurrency(INT32 args) { int c=1;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(args) c=Pike_sp[-args].u.integer;
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  else Pike_error("No argument to thread_set_concurrency(int concurrency);\n");
97ffe41997-01-26Per Hedbor  pop_n_elems(args);
09dceb1997-09-01Per Hedbor  num_lwps=c;
97ffe41997-01-26Per Hedbor  th_setconcurrency(c); }
1e4a641997-09-03Martin Stjernholm #endif
97ffe41997-01-26Per Hedbor 
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl Thread.Thread this_thread() *! *! This function returns the object that identifies this thread. *! *! @seealso *! @[Thread.Thread()] */
fa8c692000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT void f_this_thread(INT32 args)
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) { pop_n_elems(args);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  ref_push_object(Pike_interpreter.thread_id);
07513e1996-10-04Fredrik Hübinette (Hubbe) }
60d9872000-03-23Fredrik Hübinette (Hubbe) #define THIS_MUTEX ((struct mutex_storage *)(CURRENT_STORAGE))
5988921996-10-05Fredrik Hübinette (Hubbe)  /* 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;
09dceb1997-09-01Per Hedbor  struct object *owner;
4bdad01997-09-01Per Hedbor  int initialized;
5988921996-10-05Fredrik Hübinette (Hubbe) }; #define OB2KEY(X) ((struct key_storage *)((X)->storage))
07513e1996-10-04Fredrik Hübinette (Hubbe) 
e413da2001-02-01Henrik Grubbström (Grubba) /*! @class Mutex *! *! @[Thread.Mutex] is a class that implements mutual exclusion locks. *! Mutex locks are used to prevent multiple threads from simultaneously *! execute sections of code which access or change shared data. The basic *! operations for a mutex is locking and unlocking. If a thread attempts *! to lock an already locked mutex the thread will sleep until the mutex *! is unlocked. *! *! @note *! This class is simulated when Pike is compiled without thread support, *! so it's always available. *! *! In POSIX threads, mutex locks can only be unlocked by the same thread *! that locked them. In Pike any thread can unlock a locked mutex. */ /*! @decl MutexKey lock() *! @decl MutexKey lock(int type) *! *! This function attempts to lock the mutex. If the mutex is already *! locked by a different thread the current thread will sleep until the *! mutex is unlocked. The value returned is the 'key' to the lock. When *! the key is destructed or has no more references the lock will *! automatically be unlocked. The key will also be destructed if the lock *! is destructed. *! *! The @[type] argument specifies what @[lock()] should do if the *! mutex is already locked by this thread: *! @int
a9cdcf2001-02-06Henrik Grubbström (Grubba)  *! @value 0
e413da2001-02-01Henrik Grubbström (Grubba)  *! Throw an error. *! @value 1 *! Sleep until the mutex is unlocked. Useful if some *! other thread will unlock it. *! @value 2 *! Return zero. This allows recursion within a locked region of *! code, but in conjunction with other locks it easily leads *! to unspecified locking order and therefore a risk for deadlocks. *! @endint *! *! @seealso *! @[trylock()] */
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;
a49ee61999-04-02Fredrik Hübinette (Hubbe)  INT_TYPE type;
5988921996-10-05Fredrik Hübinette (Hubbe) 
07513e1996-10-04Fredrik Hübinette (Hubbe)  m=THIS_MUTEX;
a49ee61999-04-02Fredrik Hübinette (Hubbe)  if(!args) type=0; else get_all_args("mutex->lock",args,"%i",&type); switch(type) { default:
17f08c2000-07-06Fredrik Hübinette (Hubbe)  bad_arg_error("mutex->lock", Pike_sp-args, args, 2, "int(0..2)", Pike_sp+1-args,
a49ee61999-04-02Fredrik Hübinette (Hubbe)  "Unknown mutex locking style: %d\n",type); case 0: case 2:
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(m->key && OB2KEY(m->key)->owner == Pike_interpreter.thread_id)
a49ee61999-04-02Fredrik Hübinette (Hubbe)  { THREADS_FPRINTF(0, (stderr, "Recursive LOCK k:%08x, m:%08x(%08x), t:%08x\n", (unsigned int)OB2KEY(m->key), (unsigned int)m, (unsigned int)OB2KEY(m->key)->mut,
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (unsigned int) Pike_interpreter.thread_id));
a49ee61999-04-02Fredrik Hübinette (Hubbe) 
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  if(type==0) Pike_error("Recursive mutex locks!\n");
a49ee61999-04-02Fredrik Hübinette (Hubbe)  pop_n_elems(args); push_int(0);
c273692000-02-10Fredrik Hübinette (Hubbe)  return;
a49ee61999-04-02Fredrik Hübinette (Hubbe)  } case 1: break; }
46d7bf1997-09-03Henrik Grubbström (Grubba)  /* Needs to be cloned here, since create() * might use threads. */
65df5c1997-09-01Per Hedbor  o=clone_object(mutex_key,0);
4bdad01997-09-01Per Hedbor 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  DO_IF_DEBUG( if(thread_for_id(th_self()) != Pike_interpreter.thread_id) fatal("thread_for_id() (or Pike_interpreter.thread_id) failed! %p != %p\n",thread_for_id(th_self()),Pike_interpreter.thread_id) ; )
26cd941999-05-07Fredrik Hübinette (Hubbe) 
0a861b1997-09-17Fredrik Hübinette (Hubbe)  if(m->key)
fe76ce1997-09-08Fredrik Hübinette (Hubbe)  {
adb19b1999-06-30Fredrik Hübinette (Hubbe)  if(threads_disabled) { free_object(o);
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Cannot wait for mutexes when threads are disabled!\n");
adb19b1999-06-30Fredrik Hübinette (Hubbe)  }
b504ed1997-09-21Fredrik Hübinette (Hubbe)  SWAP_OUT_CURRENT_THREAD(); do
0a861b1997-09-17Fredrik Hübinette (Hubbe)  {
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(1, (stderr,"WAITING TO LOCK m:%08x\n",(unsigned int)m));
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(& m->condition);
b504ed1997-09-21Fredrik Hübinette (Hubbe)  }while(m->key); SWAP_IN_CURRENT_THREAD();
fe76ce1997-09-08Fredrik Hübinette (Hubbe)  }
5988921996-10-05Fredrik Hübinette (Hubbe)  m->key=o;
46d7bf1997-09-03Henrik Grubbström (Grubba)  OB2KEY(o)->mut=m;
fe76ce1997-09-08Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  DO_IF_DEBUG( if(thread_for_id(th_self()) != Pike_interpreter.thread_id) fatal("thread_for_id() (or Pike_interpreter.thread_id) failed! %p != %p\n",thread_for_id(th_self()),Pike_interpreter.thread_id) ; )
26cd941999-05-07Fredrik Hübinette (Hubbe) 
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(1, (stderr, "LOCK k:%08x, m:%08x(%08x), t:%08x\n", (unsigned int)OB2KEY(o), (unsigned int)m, (unsigned int)OB2KEY(m->key)->mut,
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (unsigned int)Pike_interpreter.thread_id));
65d3c31997-09-08Fredrik Hübinette (Hubbe)  pop_n_elems(args);
07513e1996-10-04Fredrik Hübinette (Hubbe)  push_object(o); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl MutexKey trylock() *! @decl MutexKey trylock(int type) *! *! This function performs the same operation as @[lock()], but if the mutex *! is already locked, it will return zero instead of sleeping until it's *! unlocked. *! *! @seealso *! @[lock()] */
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_mutex_trylock(INT32 args) {
5988921996-10-05Fredrik Hübinette (Hubbe)  struct mutex_storage *m; struct object *o;
65a5492000-08-10Per Hedbor  INT_TYPE type;
5988921996-10-05Fredrik Hübinette (Hubbe)  int i=0;
07513e1996-10-04Fredrik Hübinette (Hubbe) 
46d7bf1997-09-03Henrik Grubbström (Grubba)  /* No reason to release the interpreter lock here * since we aren't calling any functions that take time. */
a49ee61999-04-02Fredrik Hübinette (Hubbe)  m=THIS_MUTEX; if(!args) type=0; else
afcf5f2000-10-04Martin Stjernholm  get_all_args("mutex->trylock",args,"%i",&type);
a49ee61999-04-02Fredrik Hübinette (Hubbe)  switch(type)
65df5c1997-09-01Per Hedbor  {
a49ee61999-04-02Fredrik Hübinette (Hubbe)  default:
17f08c2000-07-06Fredrik Hübinette (Hubbe)  bad_arg_error("mutex->trylock", Pike_sp-args, args, 2, "int(0..2)", Pike_sp+1-args,
a49ee61999-04-02Fredrik Hübinette (Hubbe)  "Unknown mutex locking style: %d\n",type); case 0:
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(m->key && OB2KEY(m->key)->owner == Pike_interpreter.thread_id)
a49ee61999-04-02Fredrik Hübinette (Hubbe)  {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Recursive mutex locks!\n");
a49ee61999-04-02Fredrik Hübinette (Hubbe)  } case 2: case 1: break;
65df5c1997-09-01Per Hedbor  }
50fd181997-09-17Fredrik Hübinette (Hubbe) 
a49ee61999-04-02Fredrik Hübinette (Hubbe)  o=clone_object(mutex_key,0);
5988921996-10-05Fredrik Hübinette (Hubbe)  if(!m->key) {
e4293f1997-04-17Fredrik Hübinette (Hubbe)  OB2KEY(o)->mut=m;
5988921996-10-05Fredrik Hübinette (Hubbe)  m->key=o; i=1; }
4bdad01997-09-01Per Hedbor 
65d3c31997-09-08Fredrik Hübinette (Hubbe)  pop_n_elems(args);
07513e1996-10-04Fredrik Hübinette (Hubbe)  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); } }
b9bba02001-08-08Leif Stensson /*! @decl Thread.Thread current_locking_thread() *! *! This mutex method returns the object that identifies the thread that *! has locked the mutex. 0 is returned if the mutex isn't locked. *! *! @seealso *! @[Thread.Thread()] */ PMOD_EXPORT void f_mutex_locking_thread(INT32 args) { struct mutex_storage *m = THIS_MUTEX; pop_n_elems(args); if (m->key && OB2KEY(m->key)->owner) ref_push_object(OB2KEY(m->key)->owner); else push_int(0); } /*! @decl Thread.Thread current_locking_key() *! *! This mutex method returns the key object currently governing *! the lock on this mutex. 0 is returned if the mutex isn't locked. *! *! @seealso *! @[Thread.Thread()] */ PMOD_EXPORT void f_mutex_locking_key(INT32 args) { struct mutex_storage *m = THIS_MUTEX; pop_n_elems(args); if (m->key) ref_push_object(m->key); else 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) {
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(1, (stderr, "DESTROYING MUTEX m:%08x\n", (unsigned int)THIS_MUTEX));
5988921996-10-05Fredrik Hübinette (Hubbe)  if(THIS_MUTEX->key) destruct(THIS_MUTEX->key);
88fe361997-09-04Per Hedbor  THIS_MUTEX->key=0;
5988921996-10-05Fredrik Hübinette (Hubbe)  co_destroy(& THIS_MUTEX->condition); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @endclass */
60d9872000-03-23Fredrik Hübinette (Hubbe) #define THIS_KEY ((struct key_storage *)(CURRENT_STORAGE))
5988921996-10-05Fredrik Hübinette (Hubbe) void init_mutex_key_obj(struct object *o) {
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(1, (stderr, "KEY k:%08x, o:%08x\n",
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (unsigned int)THIS_KEY, (unsigned int)Pike_interpreter.thread_id));
5988921996-10-05Fredrik Hübinette (Hubbe)  THIS_KEY->mut=0;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  add_ref(THIS_KEY->owner=Pike_interpreter.thread_id);
5988921996-10-05Fredrik Hübinette (Hubbe)  THIS_KEY->initialized=1; }
07513e1996-10-04Fredrik Hübinette (Hubbe)  void exit_mutex_key_obj(struct object *o) {
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(1, (stderr, "UNLOCK k:%08x m:(%08x) t:%08x o:%08x\n", (unsigned int)THIS_KEY, (unsigned int)THIS_KEY->mut,
17f08c2000-07-06Fredrik Hübinette (Hubbe)  (unsigned int)Pike_interpreter.thread_id,
a91ca01998-07-10Henrik Grubbström (Grubba)  (unsigned int)THIS_KEY->owner));
5988921996-10-05Fredrik Hübinette (Hubbe)  if(THIS_KEY->mut)
07513e1996-10-04Fredrik Hübinette (Hubbe)  {
03b1fe1997-09-06Per Hedbor  struct mutex_storage *mut = THIS_KEY->mut;
c113871997-09-15Henrik Grubbström (Grubba) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
03b1fe1997-09-06Per Hedbor  if(mut->key != o) fatal("Mutex unlock from wrong key %p != %p!\n",THIS_KEY->mut->key,o);
5988921996-10-05Fredrik Hübinette (Hubbe) #endif
03b1fe1997-09-06Per Hedbor  mut->key=0;
46d7bf1997-09-03Henrik Grubbström (Grubba)  if (THIS_KEY->owner) { free_object(THIS_KEY->owner); THIS_KEY->owner=0; }
5988921996-10-05Fredrik Hübinette (Hubbe)  THIS_KEY->mut=0; THIS_KEY->initialized=0;
c113871997-09-15Henrik Grubbström (Grubba)  co_signal(& mut->condition);
07513e1996-10-04Fredrik Hübinette (Hubbe)  } }
60d9872000-03-23Fredrik Hübinette (Hubbe) #define THIS_COND ((COND_T *)(CURRENT_STORAGE))
e413da2001-02-01Henrik Grubbström (Grubba)  /*! @class Condition *! *! Implementation of condition variables. *! *! Condition variables are used by threaded programs *! to wait for events happening in other threads. *! *! @note *! Condition variables are only available on systems with thread *! support. The Condition class is not simulated otherwise, since that *! can't be done accurately without continuations. *! *! @seealso *! @[Thread.Mutex] */ /*! @decl void wait() *! @decl void wait(Thread.MutexKey mutex_key) *! *! Wait for contition. *! *! This function makes the current thread sleep until the condition *! variable is signalled. The optional argument should be the 'key' *! to a mutex lock. If present the mutex lock will be unlocked before *! waiting for the condition in one atomic operation. After waiting *! for the condition the mutex referenced by mutex_key will be re-locked. *! *! @seealso *! @[Thread.Mutex->lock()] */
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_cond_wait(INT32 args) { COND_T *c;
1c3c3d2000-03-24Henrik Grubbström (Grubba)  if(threads_disabled)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Cannot wait for conditions when threads are disabled!\n");
1c3c3d2000-03-24Henrik Grubbström (Grubba)  if(args > 1) { pop_n_elems(args - 1); args = 1; }
07513e1996-10-04Fredrik Hübinette (Hubbe) 
5988921996-10-05Fredrik Hübinette (Hubbe)  c=THIS_COND;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if((args > 0) && !IS_ZERO(Pike_sp-1))
07513e1996-10-04Fredrik Hübinette (Hubbe)  {
1c3c3d2000-03-24Henrik Grubbström (Grubba)  struct object *key;
5988921996-10-05Fredrik Hübinette (Hubbe)  struct mutex_storage *mut;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp[-1].type != T_OBJECT)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to condition->wait()\n");
5988921996-10-05Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  key=Pike_sp[-1].u.object;
5988921996-10-05Fredrik Hübinette (Hubbe)  if(key->prog != mutex_key)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to condition->wait()\n");
368d4a1997-09-03Per Hedbor 
1c3c3d2000-03-24Henrik Grubbström (Grubba)  if (OB2KEY(key)->initialized) {
adb19b1999-06-30Fredrik Hübinette (Hubbe) 
1c3c3d2000-03-24Henrik Grubbström (Grubba)  mut = OB2KEY(key)->mut; if(!mut)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Bad argument 1 to condition->wait()\n");
1c3c3d2000-03-24Henrik Grubbström (Grubba)  /* Unlock mutex */ mut->key=0; OB2KEY(key)->mut=0; co_signal(& mut->condition);
88fe361997-09-04Per Hedbor 
1c3c3d2000-03-24Henrik Grubbström (Grubba)  /* Wait and allow mutex operations */ SWAP_OUT_CURRENT_THREAD();
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(c);
88fe361997-09-04Per Hedbor 
5988921996-10-05Fredrik Hübinette (Hubbe)  /* Lock mutex */
1c3c3d2000-03-24Henrik Grubbström (Grubba)  while(mut->key)
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(& mut->condition);
5988921996-10-05Fredrik Hübinette (Hubbe)  mut->key=key; OB2KEY(key)->mut=mut;
1c3c3d2000-03-24Henrik Grubbström (Grubba)  SWAP_IN_CURRENT_THREAD(); pop_n_elems(args); return;
5988921996-10-05Fredrik Hübinette (Hubbe)  }
07513e1996-10-04Fredrik Hübinette (Hubbe)  }
1c3c3d2000-03-24Henrik Grubbström (Grubba)  SWAP_OUT_CURRENT_THREAD();
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(c);
1c3c3d2000-03-24Henrik Grubbström (Grubba)  SWAP_IN_CURRENT_THREAD(); pop_n_elems(args);
07513e1996-10-04Fredrik Hübinette (Hubbe) }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl void signal() *! *! @[signal()] wakes up one of the threads currently waiting for the *! condition. *!
a9cdcf2001-02-06Henrik Grubbström (Grubba)  *! @note
e413da2001-02-01Henrik Grubbström (Grubba)  *! Sometimes more than one thread is woken up. *! *! @seealso *! @[broadcast()] */
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_cond_signal(INT32 args) { pop_n_elems(args); co_signal(THIS_COND); }
e413da2001-02-01Henrik Grubbström (Grubba)  /*! @decl void broadcast() *! *! @[broadcast()] wakes up all threads currently waiting for this condition. *! *! @seealso *! @[signal()] */
07513e1996-10-04Fredrik Hübinette (Hubbe) void f_cond_broadcast(INT32 args) { pop_n_elems(args); co_broadcast(THIS_COND); }
e413da2001-02-01Henrik Grubbström (Grubba) 
07513e1996-10-04Fredrik Hübinette (Hubbe) void init_cond_obj(struct object *o) { co_init(THIS_COND); } void exit_cond_obj(struct object *o) { co_destroy(THIS_COND); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @endclass */ /*! @class Thread */ /*! @decl array(mixed) backtrace() *! *! Returns the current call stack for the thread. *! *! @returns *! The result has the same format as for @[predef::backtrace()]. *! *! @seealso *! @[predef::backtrace()] */
a7fef41997-09-03Per Hedbor void f_thread_backtrace(INT32 args) {
a8e9892001-09-05Fredrik Hübinette (Hubbe)  void low_backtrace(struct Pike_interpreter *);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *foo = THIS_THREAD;
a8e9892001-09-05Fredrik Hübinette (Hubbe) 
a7fef41997-09-03Per Hedbor  pop_n_elems(args);
a8e9892001-09-05Fredrik Hübinette (Hubbe) 
fad4ca2001-09-05Fredrik Hübinette (Hubbe)  if(foo->state.thread_id == Pike_interpreter.thread_id) { f_backtrace(0); } else if(foo->state.stack_pointer)
a7fef41997-09-03Per Hedbor  {
a8e9892001-09-05Fredrik Hübinette (Hubbe)  low_backtrace(& foo->state);
fad4ca2001-09-05Fredrik Hübinette (Hubbe)  } else {
a7fef41997-09-03Per Hedbor  push_int(0); f_allocate(1); } }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl int status() */
5740881998-01-01Fredrik Hübinette (Hubbe) void f_thread_id_status(INT32 args) { pop_n_elems(args); push_int(THIS_THREAD->status); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl static string _sprintf(int c) *! *! Returns a string identifing the thread. */
9a0d422000-02-06Martin Stjernholm void f_thread_id__sprintf (INT32 args) { pop_n_elems (args); push_constant_text ("Thread.Thread(");
9898172000-08-11Henrik Grubbström (Grubba)  push_int64((ptrdiff_t)THIS_THREAD->id);
9a0d422000-02-06Martin Stjernholm  push_constant_text (")"); f_add (3); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl mixed result() *! *! Waits for the thread to complete, and then returns *! the value returned from the thread function. */
5740881998-01-01Fredrik Hübinette (Hubbe) static void f_thread_id_result(INT32 args) {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *th=THIS_THREAD;
5740881998-01-01Fredrik Hübinette (Hubbe)  SWAP_OUT_CURRENT_THREAD(); while(th->status != THREAD_EXITED)
c91f892000-04-19Martin Stjernholm  co_wait_interpreter(&th->status_change);
5740881998-01-01Fredrik Hübinette (Hubbe)  SWAP_IN_CURRENT_THREAD();
17f08c2000-07-06Fredrik Hübinette (Hubbe)  low_object_index_no_free(Pike_sp, Pike_fp->current_object,
5740881998-01-01Fredrik Hübinette (Hubbe)  thread_id_result_variable);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_sp++;
5740881998-01-01Fredrik Hübinette (Hubbe) }
a7fef41997-09-03Per Hedbor void init_thread_obj(struct object *o) {
ec2bab2000-06-24Fredrik Hübinette (Hubbe)  MEMSET(THIS_THREAD, 0, sizeof(struct Pike_interpreter));
5740881998-01-01Fredrik Hübinette (Hubbe)  THIS_THREAD->status=THREAD_NOT_STARTED; co_init(& THIS_THREAD->status_change);
d86cd71998-08-24Marcus Comstedt  THIS_THREAD->thread_local=NULL;
5740881998-01-01Fredrik Hübinette (Hubbe) } void exit_thread_obj(struct object *o) {
d86cd71998-08-24Marcus Comstedt  if(THIS_THREAD->thread_local != NULL) { free_mapping(THIS_THREAD->thread_local); THIS_THREAD->thread_local = NULL; }
5740881998-01-01Fredrik Hübinette (Hubbe)  co_destroy(& THIS_THREAD->status_change);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  th_destroy(& THIS_THREAD->id);
a7fef41997-09-03Per Hedbor }
a9cdcf2001-02-06Henrik Grubbström (Grubba) /*! @endclass
e413da2001-02-01Henrik Grubbström (Grubba)  */
a5bd2b2000-06-10Martin Stjernholm static void thread_was_recursed(struct object *o)
f6d0171997-10-15Fredrik Hübinette (Hubbe) {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *tmp=THIS_THREAD;
d86cd71998-08-24Marcus Comstedt  if(tmp->thread_local != NULL)
b102b02000-06-10Martin Stjernholm  gc_recurse_mapping(tmp->thread_local);
d86cd71998-08-24Marcus Comstedt } static void thread_was_checked(struct object *o) {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  struct thread_state *tmp=THIS_THREAD;
d86cd71998-08-24Marcus Comstedt  if(tmp->thread_local != NULL)
d9d6f02001-06-30Martin Stjernholm  debug_gc_check2(tmp->thread_local, T_OBJECT, o, " as mapping for thread local values in thread");
1773212000-04-12Fredrik Hübinette (Hubbe)  #ifdef PIKE_DEBUG if(tmp->swapped) { struct pike_frame *f; debug_malloc_touch(o);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  debug_gc_xmark_svalues(tmp->state.evaluator_stack, tmp->state.stack_pointer-tmp->state.evaluator_stack-1, " in idle thread stack");
1773212000-04-12Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  for(f=tmp->state.frame_pointer;f;f=f->next)
1773212000-04-12Fredrik Hübinette (Hubbe)  { debug_malloc_touch(f); if(f->context.parent)
17f08c2000-07-06Fredrik Hübinette (Hubbe)  gc_external_mark2(f->context.parent,0," in Pike_fp->context.parent of idle thread"); gc_external_mark2(f->current_object,0," in Pike_fp->current_object of idle thread"); gc_external_mark2(f->context.prog,0," in Pike_fp->context.prog of idle thread");
1773212000-04-12Fredrik Hübinette (Hubbe)  } } #endif
d86cd71998-08-24Marcus Comstedt }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @class Local *! *! Thread local variable storage. *! *! This class allows you to have variables which are separate for each *! thread that uses it. It has two methods: @[get()] and @[set()]. A value *! stored in an instance of @[Thread.Local] can only be retrieved by that *! same thread. *! *! @note *! This class is simulated when Pike is compiled without thread support, *! so it's always available. */
d4e6372001-02-06Henrik Grubbström (Grubba) /* FIXME: Why not use an init callback()? */
0319392001-02-06Per Hedbor void f_thread_local_create( INT32 args )
d86cd71998-08-24Marcus Comstedt { static INT32 thread_local_id = 0;
6b87882001-06-21Martin Stjernholm  ((struct thread_local *)CURRENT_STORAGE)->id =
0319392001-02-06Per Hedbor  thread_local_id++; pop_n_elems(args); push_int(0); }
d86cd71998-08-24Marcus Comstedt 
0319392001-02-06Per Hedbor PMOD_EXPORT void f_thread_local(INT32 args) {
d86cd71998-08-24Marcus Comstedt  struct object *loc = clone_object(thread_local_prog,0); pop_n_elems(args); push_object(loc); }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl mixed get() *! *! Get the thread local value. *! *! This returns the value prevoiusly stored in the @[Thread.Local] object by *! the @[set()] method by this thread. *! *! @seealso *! @[set()] */
d86cd71998-08-24Marcus Comstedt void f_thread_local_get(INT32 args) { struct svalue key; struct mapping *m;
60d9872000-03-23Fredrik Hübinette (Hubbe)  key.u.integer = ((struct thread_local *)CURRENT_STORAGE)->id;
d86cd71998-08-24Marcus Comstedt  key.type = T_INT; key.subtype = NUMBER_NUMBER; pop_n_elems(args);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(Pike_interpreter.thread_id != NULL && (m = OBJ2THREAD(Pike_interpreter.thread_id)->thread_local) != NULL) mapping_index_no_free(Pike_sp++, m, &key);
d86cd71998-08-24Marcus Comstedt  else { push_int(0);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_sp[-1].subtype=NUMBER_UNDEFINED;
d86cd71998-08-24Marcus Comstedt  } }
e413da2001-02-01Henrik Grubbström (Grubba) /*! @decl mixed set(mixed value) *! *! Set the thread local value. *! *! This sets the value returned by the @[get] method. *! *! Calling this method does not affect the value returned by @[get()] when *! it's called by another thread (ie multiple values can be stored at the *! same time, but only one value per thread). *! *! @returns *! This function returns its argument. *! *! @note *! Note that the value set can only be retreived by the same thread. *! *! @seealso *! @[get()] */
d86cd71998-08-24Marcus Comstedt void f_thread_local_set(INT32 args) { struct svalue key; struct mapping *m;
60d9872000-03-23Fredrik Hübinette (Hubbe)  key.u.integer = ((struct thread_local *)CURRENT_STORAGE)->id;
d86cd71998-08-24Marcus Comstedt  key.type = T_INT; key.subtype = NUMBER_NUMBER; if(args>1) pop_n_elems(args-1); else if(args<1)
cd6f7e2000-12-01Martin Stjernholm  Pike_error("Too few arguments to Thread.Local.set()\n");
d86cd71998-08-24Marcus Comstedt 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(Pike_interpreter.thread_id == NULL)
cd6f7e2000-12-01Martin Stjernholm  Pike_error("Trying to set Thread.Local without thread!\n");
d86cd71998-08-24Marcus Comstedt 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if((m = OBJ2THREAD(Pike_interpreter.thread_id)->thread_local) == NULL) m = OBJ2THREAD(Pike_interpreter.thread_id)->thread_local =
d86cd71998-08-24Marcus Comstedt  allocate_mapping(4);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  mapping_insert(m, &key, &Pike_sp[-1]);
d86cd71998-08-24Marcus Comstedt }
f6d0171997-10-15Fredrik Hübinette (Hubbe) 
d9d6f02001-06-30Martin Stjernholm #ifdef PIKE_DEBUG void gc_check_thread_local (struct object *o) { /* Only used by with locate_references. */ if (Pike_in_gc == GC_PASS_LOCATE) { struct svalue key, *val; INT32 x; struct thread_state *s; key.u.integer = ((struct thread_local *)CURRENT_STORAGE)->id; key.type = T_INT; key.subtype = NUMBER_NUMBER; /* Hmm, should this be used here? We know we always got the * interpreter lock. */ /* mt_lock( & thread_table_lock ); */ for(x=0; x<THREAD_TABLE_SIZE; x++) for(s=thread_table_chains[x]; s; s=s->hashlink) { if (s->thread_local && (val = low_mapping_lookup(s->thread_local, &key))) debug_gc_check_svalues2(val, 1, T_OBJECT, o, " as thread local value in Thread.Local object" " (indirect ref)"); } /* mt_unlock( & thread_table_lock ); */ } } #endif
e413da2001-02-01Henrik Grubbström (Grubba) /*! @endclass */
a9cdcf2001-02-06Henrik Grubbström (Grubba) /*! @endmodule */
64b7b62000-11-02Henrik Grubbström (Grubba) /* Thread farm code by Per * */ static struct farmer { struct farmer *neighbour; void *field; void (*harvest)(void *); THREAD_T me; COND_T harvest_moon; #ifdef HAVE_BROKEN_LINUX_THREAD_EUID int euid, egid; #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */ } *farmers; static MUTEX_T rosie; static int _num_farmers, _num_idle_farmers; static TH_RETURN_TYPE farm(void *_a) { struct farmer *me = (struct farmer *)_a; #ifdef HAVE_BROKEN_LINUX_THREAD_EUID /* Work-around for Linux's pthreads not propagating the * effective uid & gid. */ if (!geteuid()) { setegid(me->egid); seteuid(me->euid); } #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */ 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 ); if( ++_num_idle_farmers > 16 ) { --_num_idle_farmers; --_num_farmers; mt_unlock( &rosie ); free( me ); return 0; } me->neighbour = farmers; farmers = me; /* fprintf(stderr, "farm_wait %p\n", me); */ while(!me->harvest) co_wait( &me->harvest_moon, &rosie ); --_num_idle_farmers; mt_unlock( &rosie ); /* fprintf(stderr, "farm_endwait %p\n", me); */ } while(1); /* NOT_REACHED */ return 0;/* Keep the compiler happy. */ } int th_num_idle_farmers(void) { return _num_idle_farmers; } int th_num_farmers(void) { return _num_farmers; } static struct farmer *new_farmer(void (*fun)(void *), void *args) { struct farmer *me = malloc(sizeof(struct farmer)); if (!me) { /* Out of memory */ fatal("new_farmer(): Out of memory!\n"); } dmalloc_accept_leak(me); _num_farmers++; me->neighbour = 0; me->field = args; me->harvest = fun; co_init( &me->harvest_moon ); #ifdef HAVE_BROKEN_LINUX_THREAD_EUID me->euid = geteuid(); me->egid = getegid(); #endif /* HAVE_BROKEN_LINUX_THREAD_EUID */ th_create_small(&me->me, farm, me); return me; } PMOD_EXPORT 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; f->field = here; f->harvest = fun; mt_unlock( &rosie ); co_signal( &f->harvest_moon ); return; } mt_unlock( &rosie ); new_farmer( fun, here ); } /* * Glue code. */
a91ca01998-07-10Henrik Grubbström (Grubba) void low_th_init(void)
07513e1996-10-04Fredrik Hübinette (Hubbe) {
e4419e1997-02-06Fredrik Hübinette (Hubbe) #ifdef SGI_SPROC_THREADS
a4a1722000-12-05Per Hedbor #error /* Need to specify a filename */
e4419e1997-02-06Fredrik Hübinette (Hubbe)  us_cookie = usinit(""); #endif /* SGI_SPROC_THREADS */
a91ca01998-07-10Henrik Grubbström (Grubba)  THREADS_FPRINTF(0, (stderr, "THREADS_DISALLOW() Initializing threads.\n"));
fed8de1997-10-05Henrik Grubbström (Grubba)  #ifdef POSIX_THREADS #ifdef HAVE_PTHREAD_INIT pthread_init(); #endif /* HAVE_PTHREAD_INIT */ #endif /* POSIX_THREADS */
12ae2f1997-09-05Henrik Grubbström (Grubba)  mt_init( & interpreter_lock);
c91f892000-04-19Martin Stjernholm  mt_lock_interpreter();
eac2091998-02-27Marcus Comstedt  mt_init( & thread_table_lock);
a91ca01998-07-10Henrik Grubbström (Grubba)  mt_init( & interleave_lock);
64b7b62000-11-02Henrik Grubbström (Grubba)  mt_init( & rosie);
ef1e931998-03-26Henrik Grubbström (Grubba)  co_init( & live_threads_change); co_init( & threads_disabled_change);
64b7b62000-11-02Henrik Grubbström (Grubba) 
eac2091998-02-27Marcus Comstedt  thread_table_init();
e4419e1997-02-06Fredrik Hübinette (Hubbe) #ifdef POSIX_THREADS pthread_attr_init(&pattr); #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
4908871998-08-10Fredrik Hübinette (Hubbe)  pthread_attr_setstacksize(&pattr, thread_stack_size);
e4419e1997-02-06Fredrik Hübinette (Hubbe) #endif pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
864d3c1998-01-29Fredrik Hübinette (Hubbe)  pthread_attr_init(&small_pattr); #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
26cd941999-05-07Fredrik Hübinette (Hubbe)  pthread_attr_setstacksize(&small_pattr, 4096*sizeof(char *));
864d3c1998-01-29Fredrik Hübinette (Hubbe) #endif pthread_attr_setdetachstate(&small_pattr, PTHREAD_CREATE_DETACHED);
b32ef12000-04-19Martin Stjernholm  th_running = 1;
e4419e1997-02-06Fredrik Hübinette (Hubbe) #endif
a91ca01998-07-10Henrik Grubbström (Grubba) } void th_init(void) {
89fc4c2000-08-10Henrik Grubbström (Grubba)  ptrdiff_t mutex_key_offset;
a91ca01998-07-10Henrik Grubbström (Grubba) 
e4419e1997-02-06Fredrik Hübinette (Hubbe) #ifdef UNIX_THREADS
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(int:void) */ ADD_EFUN("thread_set_concurrency",f_thread_set_concurrency,tFunc(tInt,tVoid), OPT_SIDE_EFFECT);
e4419e1997-02-06Fredrik Hübinette (Hubbe) #endif
07513e1996-10-04Fredrik Hübinette (Hubbe) 
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_MUTEX_KEY);
90e9781999-01-31Fredrik Hübinette (Hubbe)  mutex_key_offset = ADD_STORAGE(struct key_storage);
46d7bf1997-09-03Henrik Grubbström (Grubba)  /* This is needed to allow the gc to find the possible circular reference.
e413da2001-02-01Henrik Grubbström (Grubba)  * It also allows a thread to take over ownership of a key.
46d7bf1997-09-03Henrik Grubbström (Grubba)  */ map_variable("_owner", "object", 0, mutex_key_offset + OFFSETOF(key_storage, owner), T_OBJECT);
5988921996-10-05Fredrik Hübinette (Hubbe)  set_init_callback(init_mutex_key_obj);
07513e1996-10-04Fredrik Hübinette (Hubbe)  set_exit_callback(exit_mutex_key_obj);
bad5162000-06-23Fredrik Hübinette (Hubbe)  mutex_key=Pike_compiler->new_program;
3f87be1999-12-14Martin Stjernholm  add_ref(mutex_key); end_class("mutex_key", 0);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  mutex_key->flags|=PROGRAM_DESTRUCT_IMMEDIATE;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
f839fa1997-02-28Fredrik Hübinette (Hubbe)  if(!mutex_key) fatal("Failed to initialize mutex_key program!\n");
e45f661998-03-20Fredrik Hübinette (Hubbe) #endif
07513e1996-10-04Fredrik Hübinette (Hubbe) 
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_MUTEX);
52e4c61999-12-13Martin Stjernholm  ADD_STORAGE(struct mutex_storage); /* function(int(0..2)|void:object(mutex_key)) */
3f87be1999-12-14Martin Stjernholm  ADD_FUNCTION("lock",f_mutex_lock, tFunc(tOr(tInt02,tVoid),tObjIs_THREAD_MUTEX_KEY),0);
52e4c61999-12-13Martin Stjernholm  /* function(int(0..2)|void:object(mutex_key)) */
3f87be1999-12-14Martin Stjernholm  ADD_FUNCTION("trylock",f_mutex_trylock, tFunc(tOr(tInt02,tVoid),tObjIs_THREAD_MUTEX_KEY),0);
b9bba02001-08-08Leif Stensson  /* function(:object(Pike_interpreter.thread_id)) */ ADD_FUNCTION("current_locking_thread",f_mutex_locking_thread, tFunc(tNone,tObjIs_THREAD_ID), 0); /* function(:object(Pike_interpreter.thread_id)) */ ADD_FUNCTION("current_locking_key",f_mutex_locking_key, tFunc(tNone,tObjIs_THREAD_MUTEX_KEY), 0);
52e4c61999-12-13Martin Stjernholm  set_init_callback(init_mutex_obj); set_exit_callback(exit_mutex_obj); end_class("mutex", 0);
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_CONDITION);
90e9781999-01-31Fredrik Hübinette (Hubbe)  ADD_STORAGE(COND_T);
52e4c61999-12-13Martin Stjernholm  /* function(void|object(mutex_key):void) */
3f87be1999-12-14Martin Stjernholm  ADD_FUNCTION("wait",f_cond_wait, tFunc(tOr(tVoid,tObjIs_THREAD_MUTEX_KEY),tVoid),0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(:void) */
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("signal",f_cond_signal,tFunc(tNone,tVoid),0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(:void) */
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("broadcast",f_cond_broadcast,tFunc(tNone,tVoid),0);
07513e1996-10-04Fredrik Hübinette (Hubbe)  set_init_callback(init_cond_obj); set_exit_callback(exit_cond_obj);
b98eb31997-02-11Henrik Grubbström (Grubba)  end_class("condition", 0);
de413f1998-03-26Per Hedbor  { struct program *tmp;
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_DISABLE_THREADS);
de413f1998-03-26Per Hedbor  set_init_callback(init_threads_disable); set_exit_callback(exit_threads_disable);
bad5162000-06-23Fredrik Hübinette (Hubbe)  tmp = Pike_compiler->new_program;
3f87be1999-12-14Martin Stjernholm  add_ref(tmp); end_class("threads_disabled", 0);
de413f1998-03-26Per Hedbor  tmp->flags|=PROGRAM_DESTRUCT_IMMEDIATE; add_global_program("_disable_threads", tmp);
56a6961998-04-05Fredrik Hübinette (Hubbe)  free_program(tmp);
de413f1998-03-26Per Hedbor  }
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) 
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_LOCAL);
90e9781999-01-31Fredrik Hübinette (Hubbe)  ADD_STORAGE(struct thread_local);
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("get",f_thread_local_get,tFunc(tNone,tMix),0);
b21d9e2000-11-06Martin Stjernholm  ADD_FUNCTION("set",f_thread_local_set,tFunc(tSetvar(1,tMix),tVar(1)),0);
d4e6372001-02-06Henrik Grubbström (Grubba)  ADD_FUNCTION("create", f_thread_local_create, tFunc(tVoid,tVoid), ID_STATIC);
d9d6f02001-06-30Martin Stjernholm #ifdef PIKE_DEBUG set_gc_check_callback(gc_check_thread_local); #endif
bad5162000-06-23Fredrik Hübinette (Hubbe)  thread_local_prog=Pike_compiler->new_program;
3f87be1999-12-14Martin Stjernholm  add_ref(thread_local_prog); end_class("thread_local", 0);
d86cd71998-08-24Marcus Comstedt  if(!thread_local_prog) fatal("Failed to initialize thread_local program!\n");
d4e6372001-02-06Henrik Grubbström (Grubba)  ADD_EFUN("thread_local", f_thread_local,
3f87be1999-12-14Martin Stjernholm  tFunc(tNone,tObjIs_THREAD_LOCAL),
d4e6372001-02-06Henrik Grubbström (Grubba)  OPT_EXTERNAL_DEPEND);
d86cd71998-08-24Marcus Comstedt 
3f87be1999-12-14Martin Stjernholm  START_NEW_PROGRAM_ID(THREAD_ID);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  thread_storage_offset=ADD_STORAGE(struct thread_state);
5740881998-01-01Fredrik Hübinette (Hubbe)  thread_id_result_variable=simple_add_variable("result","mixed",0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(:array) */
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("backtrace",f_thread_backtrace,tFunc(tNone,tArray),0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(:mixed) */
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("wait",f_thread_id_result,tFunc(tNone,tMix),0);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  /* function(:int) */
b93e6e1999-06-19Fredrik Hübinette (Hubbe)  ADD_FUNCTION("status",f_thread_id_status,tFunc(tNone,tInt),0);
9a0d422000-02-06Martin Stjernholm  ADD_FUNCTION("_sprintf",f_thread_id__sprintf,tFunc(tNone,tStr),0);
a5bd2b2000-06-10Martin Stjernholm  set_gc_recurse_callback(thread_was_recursed);
d86cd71998-08-24Marcus Comstedt  set_gc_check_callback(thread_was_checked);
a7fef41997-09-03Per Hedbor  set_init_callback(init_thread_obj);
a2db6b1998-01-02Fredrik Hübinette (Hubbe)  set_exit_callback(exit_thread_obj);
bad5162000-06-23Fredrik Hübinette (Hubbe)  thread_id_prog=Pike_compiler->new_program;
95363a2000-04-11Fredrik Hübinette (Hubbe)  thread_id_prog->flags |= PROGRAM_NO_EXPLICIT_DESTRUCT;
3f87be1999-12-14Martin Stjernholm  add_ref(thread_id_prog); end_class("thread_id", 0);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  /* function(mixed ...:object(Pike_interpreter.thread_id)) */
3f87be1999-12-14Martin Stjernholm  ADD_EFUN("thread_create",f_thread_create, tFuncV(tNone,tMixed,tObjIs_THREAD_ID), OPT_SIDE_EFFECT);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  /* function(:object(Pike_interpreter.thread_id)) */
3f87be1999-12-14Martin Stjernholm  ADD_EFUN("this_thread",f_this_thread, tFunc(tNone,tObjIs_THREAD_ID), OPT_EXTERNAL_DEPEND);
17f08c2000-07-06Fredrik Hübinette (Hubbe)  /* function(:array(object(Pike_interpreter.thread_id))) */
3f87be1999-12-14Martin Stjernholm  ADD_EFUN("all_threads",f_all_threads, tFunc(tNone,tArr(tObjIs_THREAD_ID)), OPT_EXTERNAL_DEPEND);
52e4c61999-12-13Martin Stjernholm 
5d2f121998-08-27Henrik Grubbström (Grubba)  /* 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);
f839fa1997-02-28Fredrik Hübinette (Hubbe)  if(!mutex_key) fatal("Failed to initialize thread program!\n");
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) 
17f08c2000-07-06Fredrik Hübinette (Hubbe)  Pike_interpreter.thread_id=clone_object(thread_id_prog,0); SWAP_OUT_THREAD(OBJ2THREAD(Pike_interpreter.thread_id)); /* Init struct */ OBJ2THREAD(Pike_interpreter.thread_id)->id=th_self(); OBJ2THREAD(Pike_interpreter.thread_id)->swapped=0; thread_table_insert(Pike_interpreter.thread_id);
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) }
be478c1997-08-30Henrik Grubbström (Grubba) void th_cleanup(void)
6d1a5e1996-10-07Fredrik Hübinette (Hubbe) {
b32ef12000-04-19Martin Stjernholm  th_running = 0;
17f08c2000-07-06Fredrik Hübinette (Hubbe)  if(Pike_interpreter.thread_id)
c3b9952000-02-15Henrik Grubbström (Grubba)  {
17f08c2000-07-06Fredrik Hübinette (Hubbe)  thread_table_delete(Pike_interpreter.thread_id); destruct(Pike_interpreter.thread_id); free_object(Pike_interpreter.thread_id); Pike_interpreter.thread_id=0;
56f4f42001-09-18Fredrik Hübinette (Hubbe)  destruct_objects_to_destruct_cb();
c3b9952000-02-15Henrik Grubbström (Grubba)  }
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  if(mutex_key) { free_program(mutex_key); mutex_key=0; }
d86cd71998-08-24Marcus Comstedt  if(thread_local_prog) { free_program(thread_local_prog); thread_local_prog=0; }
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  if(thread_id_prog) { free_program(thread_id_prog); thread_id_prog=0; }
07513e1996-10-04Fredrik Hübinette (Hubbe) } #endif
7965d72001-01-24Fredrik Hübinette (Hubbe)