diff --git a/src/interpret.c b/src/interpret.c index 518524fa51d0a89f5a8e4266c08ca833c824f086..4094ff3a5bde68f7c8efe96333c7f47b51e33e8e 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -5,7 +5,7 @@ \*/ /**/ #include "global.h" -RCSID("$Id: interpret.c,v 1.143 2000/04/19 13:59:01 mast Exp $"); +RCSID("$Id: interpret.c,v 1.144 2000/04/19 16:03:30 mast Exp $"); #include "interpret.h" #include "object.h" #include "program.h" @@ -669,7 +669,7 @@ void mega_apply2(enum apply_type type, INT32 args, void *arg1, void *arg2) { THREAD_T self = th_self(); - TRYLOCK_INTERPRETER_LOCK(); + CHECK_INTERPRETER_LOCK(); if( thread_id && !th_equal( OBJ2THREAD(thread_id)->id, self) ) fatal("Current thread is wrong.\n"); diff --git a/src/interpreter.h b/src/interpreter.h index d590db1631733b4f1f5631632594025f485dc808..b98b728f7f39cd60ddf83b03e21a16e01b724529 100644 --- a/src/interpreter.h +++ b/src/interpreter.h @@ -32,10 +32,7 @@ static int eval_instruction(unsigned char *pc) if(d_flag) { #ifdef _REENTRANT -#ifndef __NT__ - if(!mt_trylock(& interpreter_lock)) - fatal("Interpreter running unlocked!\n"); -#endif + CHECK_INTERPRETER_LOCK(); if(d_flag>1 && thread_for_id(th_self()) != thread_id) fatal("thread_for_id() (or thread_id) failed in interpreter.h! %p != %p\n",thread_for_id(th_self()),thread_id); #endif diff --git a/src/modules/HTTPLoop/accept_and_parse.c b/src/modules/HTTPLoop/accept_and_parse.c index 04a9968088bfdcfc778ab4829a37b684fd52228b..d9f6c084d9352911c095f1b5134e0f6988609f87 100644 --- a/src/modules/HTTPLoop/accept_and_parse.c +++ b/src/modules/HTTPLoop/accept_and_parse.c @@ -462,7 +462,7 @@ static void low_accept_loop(struct args *arg) struct cache *c, *p = NULL; struct log *l, *n = NULL; /* oups. */ - mt_lock( &interpreter_lock ); + mt_lock_interpreter(); for(i=0; i<CACHE_HTABLE_SIZE; i++) { e = arg->cache->htable[i]; @@ -504,7 +504,7 @@ static void low_accept_loop(struct args *arg) else aap_first_log = l->next; aap_free(l); } - mt_unlock( &interpreter_lock ); + mt_unlock_interpreter(); aap_free(arg2); aap_free(arg); return; /* No more accept here.. */ diff --git a/src/modules/HTTPLoop/cache.c b/src/modules/HTTPLoop/cache.c index 5af3cc8b4efee9dea157a99aca20019357c8bbab..cfe12688d141fe3e93714e4152a1fbe74b907b2f 100644 --- a/src/modules/HTTPLoop/cache.c +++ b/src/modules/HTTPLoop/cache.c @@ -83,7 +83,7 @@ static int ensure_interpreter_lock( ) { if( thi->swapped ) /* We are swapped out.. */ { - mt_lock( &interpreter_lock ); + mt_lock_interpreter(); return 1; } return 0; /* we are swapped in */ @@ -93,7 +93,7 @@ static int ensure_interpreter_lock( ) if( num_threads == 1 ) free=num_threads++; wake_up_backend(); - mt_lock( &interpreter_lock ); + mt_lock_interpreter(); if( free ) num_threads--; return 1; @@ -122,7 +122,7 @@ void aap_enqueue_string_to_free( struct pike_string *s ) int free_interpreter_lock = ensure_interpreter_lock(); really_free_from_queue(); if( free_interpreter_lock ) - mt_unlock( &interpreter_lock ); + mt_unlock_interpreter(); } free_queue[ numtofree++ ] = s; mt_unlock( &tofree_mutex ); diff --git a/src/modules/Java/jvm.c b/src/modules/Java/jvm.c index d419db415b8610b9da52de142298d6a7a0f3415f..ebbe7c22dd5c9b947c27849458b55eb95eaec281 100644 --- a/src/modules/Java/jvm.c +++ b/src/modules/Java/jvm.c @@ -1,5 +1,5 @@ /* - * $Id: jvm.c,v 1.14 2000/04/04 14:11:59 marcus Exp $ + * $Id: jvm.c,v 1.15 2000/04/19 16:03:34 mast Exp $ * * Pike interface to Java Virtual Machine * @@ -16,7 +16,7 @@ #endif /* HAVE_CONFIG_H */ #include "global.h" -RCSID("$Id: jvm.c,v 1.14 2000/04/04 14:11:59 marcus Exp $"); +RCSID("$Id: jvm.c,v 1.15 2000/04/19 16:03:34 mast Exp $"); #include "program.h" #include "interpret.h" #include "stralloc.h" @@ -1660,18 +1660,18 @@ static void native_dispatch(struct native_method_context *ctx, do_native_dispatch(ctx, env, cls, args, rc); } else { /* Nope, let's get it... */ - mt_lock(&interpreter_lock); + mt_lock_interpreter(); SWAP_IN_THREAD(state); do_native_dispatch(ctx, env, cls, args, rc); /* Restore */ SWAP_OUT_THREAD(state); - mt_unlock(&interpreter_lock); + mt_unlock_interpreter(); } } else { /* Not a pike thread. Create a temporary thread_id... */ - mt_lock(&interpreter_lock); + mt_lock_interpreter(); init_interpreter(); stack_top=((char *)&state)+ (thread_stack_size-16384) * STACK_DIRECTION; recoveries = NULL; @@ -1690,7 +1690,7 @@ static void native_dispatch(struct native_method_context *ctx, thread_id=NULL; cleanup_interpret(); num_threads--; - mt_unlock(&interpreter_lock); + mt_unlock_interpreter(); } } diff --git a/src/modules/files/file.c b/src/modules/files/file.c index 39d7987877a6de2c856c106f8c040c7473cc35d0..1ea621c0750bcaabbf12e21ce4a4f635109f033d 100644 --- a/src/modules/files/file.c +++ b/src/modules/files/file.c @@ -5,7 +5,7 @@ \*/ /**/ #include "global.h" -RCSID("$Id: file.c,v 1.173 2000/04/14 17:30:31 grubba Exp $"); +RCSID("$Id: file.c,v 1.174 2000/04/19 16:14:37 mast Exp $"); #include "fdlib.h" #include "interpret.h" #include "svalue.h" @@ -1362,7 +1362,7 @@ static void file_open(INT32 args) /* This is a temporary kluge */ if(d_flag) { - fprintf(stderr,"Possible gc() failiure detected in open()\n"); + fprintf(stderr,"Possible gc() failure detected in open()\n"); describe(fp->current_object); } #endif @@ -2389,9 +2389,9 @@ static TH_RETURN_TYPE proxy_thread(void * data) fd_close(p->to); fd_close(p->from); - mt_lock(&interpreter_lock); + mt_lock_interpreter(); num_threads--; - mt_unlock(&interpreter_lock); + mt_unlock_interpreter(); free((char *)p); th_exit(0); return 0; @@ -2658,6 +2658,7 @@ static int PIKE_CONCAT(Y,_function_number); #define FILE_FUNC(X,Y,Z) \ void PIKE_CONCAT(Y,_ref) (INT32 args) { \ struct object *o=REF; \ + debug_malloc_touch(o); \ if(!o || !o->prog) { \ /* This is a temporary kluge */ \ DO_IF_DMALLOC( \ diff --git a/src/modules/files/sendfile.c b/src/modules/files/sendfile.c index dc7a4f4812e472e87724e98b4c4b69468bfa2a7f..842574c812ddb99f39f453427b4de7f1b0799cb2 100644 --- a/src/modules/files/sendfile.c +++ b/src/modules/files/sendfile.c @@ -1,5 +1,5 @@ /* - * $Id: sendfile.c,v 1.37 2000/03/21 22:34:13 grubba Exp $ + * $Id: sendfile.c,v 1.38 2000/04/19 16:03:35 mast Exp $ * * Sends headers + from_fd[off..off+len-1] + trailers to to_fd asyncronously. * @@ -565,7 +565,7 @@ static void worker(void *this_) low_do_sendfile(this); - mt_lock(&interpreter_lock); + mt_lock_interpreter(); /* * Unlock the fd's @@ -606,7 +606,7 @@ static void worker(void *this_) /* We're gone... */ num_threads--; - mt_unlock(&interpreter_lock); + mt_unlock_interpreter(); /* Die */ return; diff --git a/src/program.c b/src/program.c index 355453cecf743c0fb511616d1d8aa96263bdc8d0..06d98682f0c70e1c16b65e024b1bbd0f367eaca4 100644 --- a/src/program.c +++ b/src/program.c @@ -5,7 +5,7 @@ \*/ /**/ #include "global.h" -RCSID("$Id: program.c,v 1.228 2000/04/16 16:00:35 mast Exp $"); +RCSID("$Id: program.c,v 1.229 2000/04/19 16:03:30 mast Exp $"); #include "program.h" #include "object.h" #include "dynamic_buffer.h" @@ -3734,11 +3734,7 @@ char *get_storage(struct object *o, struct program *p) int offset; #ifdef _REENTRANT -#ifndef __NT__ - if(d_flag) - if(!mt_trylock(& interpreter_lock)) - fatal("get_storage running unlocked!\n"); -#endif + if(d_flag) CHECK_INTERPRETER_LOCK(); #endif offset= low_get_storage(o->prog, p); diff --git a/src/signal_handler.c b/src/signal_handler.c index 9ed267d49ede498e6030d8d3587b8ec06bcb9703..be895e4649c59105245a9eaf3b8c0030a7bf6639 100644 --- a/src/signal_handler.c +++ b/src/signal_handler.c @@ -25,7 +25,7 @@ #include "main.h" #include <signal.h> -RCSID("$Id: signal_handler.c,v 1.168 2000/03/24 01:24:52 hubbe Exp $"); +RCSID("$Id: signal_handler.c,v 1.169 2000/04/19 16:03:31 mast Exp $"); #ifdef HAVE_PASSWD_H # include <passwd.h> @@ -1066,7 +1066,7 @@ static TH_RETURN_TYPE wait_thread(void *data) fprintf(stderr, "wait thread: locking interpreter, pid=%d\n",pid); #endif - mt_lock(&interpreter_lock); + mt_lock_interpreter(); #ifdef PROC_DEBUG fprintf(stderr, "wait thread: reporting the event!\n"); @@ -1075,7 +1075,7 @@ static TH_RETURN_TYPE wait_thread(void *data) report_child(pid, status); co_broadcast(& process_status_change); - mt_unlock(&interpreter_lock); + mt_unlock_interpreter(); continue; } @@ -1134,7 +1134,7 @@ static void f_pid_status_wait(INT32 args) while(THIS->state == PROCESS_RUNNING) { SWAP_OUT_CURRENT_THREAD(); - co_wait( & process_status_change, &interpreter_lock); + co_wait_interpreter( & process_status_change); SWAP_IN_CURRENT_THREAD(); } diff --git a/src/threads.c b/src/threads.c index 5672cea4eebbd329a3e6b6ccc435981dd515f2c7..1b90be46badcd2ef582b8c4c45b364967d9bf96f 100644 --- a/src/threads.c +++ b/src/threads.c @@ -1,5 +1,5 @@ #include "global.h" -RCSID("$Id: threads.c,v 1.121 2000/04/19 13:57:36 mast Exp $"); +RCSID("$Id: threads.c,v 1.122 2000/04/19 16:03:31 mast Exp $"); int num_threads = 1; int threads_disabled = 0; @@ -201,6 +201,10 @@ static struct callback *threads_evaluator_callback=0; int thread_id_result_variable; int th_running = 0; +#ifdef PIKE_DEBUG +int debug_interpreter_is_locked = 0; +THREAD_T debug_locking_thread; +#endif MUTEX_T interpreter_lock, thread_table_lock, interleave_lock; struct program *mutex_key = 0; struct program *thread_id_prog = 0; @@ -292,7 +296,7 @@ void init_threads_disable(struct object *o) (stderr, "_disable_threads(): Waiting for %d threads to finish\n", live_threads)); - co_wait(&live_threads_change, &interpreter_lock); + co_wait_interpreter(&live_threads_change); } SWAP_IN_CURRENT_THREAD(); } @@ -559,7 +563,7 @@ TH_RETURN_TYPE new_thread_func(void * data) THREADS_FPRINTF(0, (stderr,"THREADS_DISALLOW() Thread %08x created...\n", (unsigned int)arg.id)); - if((tmp=mt_lock( & interpreter_lock))) + if((tmp=mt_lock_interpreter())) fatal("Failed to lock interpreter, return value=%d, errno=%d\n",tmp, #ifdef __NT__ GetLastError() @@ -654,7 +658,7 @@ TH_RETURN_TYPE new_thread_func(void * data) remove_callback(threads_evaluator_callback); threads_evaluator_callback=0; } - mt_unlock(& interpreter_lock); + mt_unlock_interpreter(); th_exit(0); /* NOT_REACHED, but removes a warning */ return(NULL); @@ -803,7 +807,7 @@ void f_mutex_lock(INT32 args) do { THREADS_FPRINTF(1, (stderr,"WAITING TO LOCK m:%08x\n",(unsigned int)m)); - co_wait(& m->condition, & interpreter_lock); + co_wait_interpreter(& m->condition); }while(m->key); SWAP_IN_CURRENT_THREAD(); } @@ -969,11 +973,11 @@ void f_cond_wait(INT32 args) /* Wait and allow mutex operations */ SWAP_OUT_CURRENT_THREAD(); - co_wait(c, &interpreter_lock); + co_wait_interpreter(c); /* Lock mutex */ while(mut->key) - co_wait(& mut->condition, &interpreter_lock); + co_wait_interpreter(& mut->condition); mut->key=key; OB2KEY(key)->mut=mut; @@ -984,7 +988,7 @@ void f_cond_wait(INT32 args) } SWAP_OUT_CURRENT_THREAD(); - co_wait(c, &interpreter_lock); + co_wait_interpreter(c); SWAP_IN_CURRENT_THREAD(); pop_n_elems(args); @@ -1040,7 +1044,7 @@ static void f_thread_id_result(INT32 args) SWAP_OUT_CURRENT_THREAD(); while(th->status != THREAD_EXITED) - co_wait(&th->status_change, &interpreter_lock); + co_wait_interpreter(&th->status_change); SWAP_IN_CURRENT_THREAD(); @@ -1166,7 +1170,7 @@ void low_th_init(void) #endif /* POSIX_THREADS */ mt_init( & interpreter_lock); - mt_lock( & interpreter_lock); + mt_lock_interpreter(); mt_init( & thread_table_lock); mt_init( & interleave_lock); co_init( & live_threads_change); diff --git a/src/threads.h b/src/threads.h index 39550ef32a5ef58ca9060cb0e1a5966e51c2a487..ed6ad41d2fa6a40ac1c8dd13165bf72636eaacd4 100644 --- a/src/threads.h +++ b/src/threads.h @@ -1,5 +1,5 @@ /* - * $Id: threads.h,v 1.85 2000/04/19 13:57:35 mast Exp $ + * $Id: threads.h,v 1.86 2000/04/19 16:03:31 mast Exp $ */ #ifndef THREADS_H #define THREADS_H @@ -260,6 +260,42 @@ extern int th_running; extern PIKE_MUTEX_T interpreter_lock; +#if defined(PIKE_DEBUG) && !defined(__NT__) + +/* This is a debug wrapper to enable checks that the interpreter lock + * is hold by the current thread. */ + +extern THREAD_T debug_locking_thread; +#define SET_LOCKING_THREAD (debug_locking_thread = th_self(), 0) + +#define mt_lock_interpreter() (mt_lock(&interpreter_lock) || SET_LOCKING_THREAD) +#define mt_trylock_interpreter() (mt_trylock(&interpreter_lock) || SET_LOCKING_THREAD) +#define mt_unlock_interpreter() (mt_unlock(&interpreter_lock)) +#define co_wait_interpreter(COND) \ + do {co_wait((COND), &interpreter_lock); SET_LOCKING_THREAD;} while (0) + +#define CHECK_INTERPRETER_LOCK() do { \ + if (th_running) { \ + THREAD_T self; \ + if (!mt_trylock(&interpreter_lock)) \ + fatal("Interpreter is not locked.\n"); \ + self = th_self(); \ + if (!th_equal(debug_locking_thread, self)) \ + fatal("Interpreter is not locked by this thread.\n"); \ + } \ +} while (0) + +#else + +#define mt_lock_interpreter() (mt_lock(&interpreter_lock)) +#define mt_trylock_interpreter() (mt_trylock(&interpreter_lock)) +#define mt_unlock_interpreter() (mt_unlock(&interpreter_lock)) +#define co_wait_interpreter(COND) do {co_wait((COND), &interpreter_lock);} while (0) + +#define CHECK_INTERPRETER_LOCK() do {} while (0) + +#endif + extern COND_T live_threads_change; /* Used by _disable_threads */ extern COND_T threads_disabled_change; /* Used by _disable_threads */ @@ -478,21 +514,21 @@ struct thread_state { THREADS_FPRINTF(1, (stderr, "THREADS_ALLOW() %s:%d t:%08x(#%d)\n", \ __FILE__, __LINE__, \ (unsigned int)_tmp->thread_id, live_threads)); \ - mt_unlock(& interpreter_lock); \ + mt_unlock_interpreter(); \ } else {} \ HIDE_GLOBAL_VARIABLES() #define THREADS_DISALLOW() \ REVEAL_GLOBAL_VARIABLES(); \ if(_tmp->swapped) { \ - mt_lock(& interpreter_lock); \ + mt_lock_interpreter(); \ THREADS_FPRINTF(1, (stderr, "THREADS_DISALLOW() %s:%d t:%08x(#%d)\n", \ __FILE__, __LINE__, \ (unsigned int)_tmp->thread_id, live_threads)); \ while (threads_disabled) { \ THREADS_FPRINTF(1, (stderr, \ "THREADS_DISALLOW(): Threads disabled\n")); \ - co_wait(&threads_disabled_change, &interpreter_lock); \ + co_wait_interpreter(&threads_disabled_change); \ } \ SWAP_IN_THREAD(_tmp);\ } \ @@ -514,14 +550,14 @@ struct thread_state { THREADS_FPRINTF(1, (stderr, "THREADS_ALLOW_UID() %s:%d t:%08x(#%d)\n", \ __FILE__, __LINE__, \ (unsigned int)_tmp_uid->thread_id, live_threads)); \ - mt_unlock(& interpreter_lock); \ + mt_unlock_interpreter(); \ } else {} \ HIDE_GLOBAL_VARIABLES() #define THREADS_DISALLOW_UID() \ REVEAL_GLOBAL_VARIABLES(); \ if(_tmp_uid->swapped) { \ - mt_lock(& interpreter_lock); \ + mt_lock_interpreter(); \ live_threads--; \ THREADS_FPRINTF(1, (stderr, \ "THREADS_DISALLOW_UID() %s:%d t:%08x(#%d)\n", \ @@ -530,7 +566,7 @@ struct thread_state { co_broadcast(&live_threads_change); \ while (threads_disabled) { \ THREADS_FPRINTF(1, (stderr, "THREADS_DISALLOW_UID(): Wait...\n")); \ - co_wait(&threads_disabled_change, &interpreter_lock); \ + co_wait_interpreter(&threads_disabled_change); \ } \ SWAP_IN_THREAD(_tmp_uid);\ } \ @@ -542,36 +578,13 @@ struct thread_state { THREADS_DISALLOW() #ifdef PIKE_DEBUG - #define ASSERT_THREAD_SWAPPED_IN() do { \ struct thread_state *_tmp=thread_state_for_id(th_self()); \ if(_tmp->swapped) fatal("Thread is not swapped in!\n"); \ }while(0) -#ifdef __NT__ -#define TRYLOCK_INTERPRETER_LOCK() do {} while (0) -#else -#define TRYLOCK_INTERPRETER_LOCK() do { \ - if (th_running && !mt_trylock(&interpreter_lock)) \ - fatal("Haven't got interpreter lock.\n"); \ -} while (0) -#endif - -#define CHECK_INTERPRETER_LOCK() do { \ - if (th_running) { \ - THREAD_T self; \ - TRYLOCK_INTERPRETER_LOCK(); \ - self = th_self(); \ - if( thread_id && !th_equal( OBJ2THREAD(thread_id)->id, self) ) \ - fatal("Current thread is wrong.\n"); \ - } \ -} while (0) - #else - #define ASSERT_THREAD_SWAPPED_IN() -#define CHECK_INTERPRETER_LOCK() do {} while (0) - #endif /* Prototypes begin here */ @@ -663,9 +676,6 @@ void th_farm(void (*fun)(void *), void *here); #define init_threads_disable(X) #define exit_threads_disable(X) -#define TRYLOCK_INTERPRETER_LOCK() do {} while (0) -#define CHECK_INTERPRETER_LOCK() do {} while (0) - #endif /* PIKE_THREADS */ #ifdef __NT__