deadlock.c

00001 #include "xsb_config.h"
00002 
00003 #ifdef SHARED_COMPL_TABLES
00004                                                                                 
00005 #include "context.h"
00006 #include "basicdefs.h"
00007 #include "cell_xsb.h"
00008 #include "choice.h"
00009 #include "cut_xsb.h"
00010 #include "binding.h"
00011 #include "sw_envs.h"
00012 #include "deadlock.h"
00013 #include "debug_xsb.h"
00014 #include "macro_xsb.h"
00015 #include "thread_xsb.h"
00016 #include "trie_internals.h"
00017 
00018 int would_deadlock( th_context *t1, th_context *t2 )
00019 {
00020         th_context * t = t1 ;
00021                                                                                 
00022         while( t != NULL )
00023                 if( t == t2 )
00024                         return TRUE ;
00025                 else
00026                         t = t->waiting_for_thread;
00027                                                                                 
00028         return FALSE ;
00029 }
00030 
00031 static VariantSF bottom_leader(th_context *th, VariantSF to_sgf)
00032 {
00033         CPtr csf = subg_compl_stack_ptr(to_sgf) ;
00034 
00035         while( prev_compl_frame(csf) < COMPLSTACKBOTTOM && !is_leader(csf) )
00036                 csf = prev_compl_frame(csf) ;
00037         return compl_subgoal_ptr(csf) ;
00038 }
00039 
00040 static void ReclaimDSandMarkReset(th_context *th, VariantSF to, int leader)
00041 {
00042         CPtr csf = openreg ;
00043         for(;;)
00044         {       if( !is_completed(compl_subgoal_ptr(csf)))
00045                 /* Handle early completion */
00046                 {       subg_grabbed(compl_subgoal_ptr(csf)) = TRUE ;
00047                         subg_tid(compl_subgoal_ptr(csf)) = leader ;
00048                         subg_asf_list_ptr(compl_subgoal_ptr(csf)) = NULL;
00049                         subg_compl_susp_ptr(compl_subgoal_ptr(csf)) = NULL;
00050                 }
00051                 if( compl_subgoal_ptr(csf) == to ) 
00052                         break;
00053                 csf = prev_compl_frame(csf) ;
00054         }
00055 }
00056 
00057 static void reset_thread( th_context *th, th_context *ctxt, VariantSF sgf,
00058                           VariantSF *resetsgf )
00059 {
00060         CPtr tbreg ;
00061         /* if the subgoal has not yet been computed, the
00062            thread should not be reset */
00063         if( subg_grabbed(sgf) )
00064         {       subg_tid(sgf) = th->tid ;
00065                 return ;
00066         }
00067         ctxt->reset_thread = TRUE ;
00068         sgf = bottom_leader(ctxt, sgf) ;
00069         *resetsgf = sgf ;
00070         ReclaimDSandMarkReset(ctxt, sgf, th->tid);
00071         /* trick to use other thread's context */
00072         th = ctxt ;
00073         /* reset the stacks by restoring the generator cp of this sg */
00074         breg = subg_cp_ptr(sgf) ;
00075         tbreg = breg ;
00076         openreg = prev_compl_frame(subg_compl_stack_ptr(sgf)) ;
00077         switch_envs(tbreg);
00078         ptcpreg = tcp_subgoal_ptr(tbreg);
00079         delayreg = NULL;
00080         reclaim_stacks(tbreg) ;
00081         restore_some_wamregs(tbreg, ereg);
00082         pcreg = (byte *)tcp_reset_pcreg(tbreg) ;
00083         table_restore_registers(tbreg, pcreg[3], reg);
00084 
00085         /* delete the generator cp */
00086         breg = tcp_prevbreg(breg) ; 
00087 }
00088 
00089 void reset_leader( th_context *th )
00090 {
00091         VariantSF resetsgf ;
00092         reset_thread( th, th, compl_subgoal_ptr(openreg), &resetsgf );
00093         th->reset_thread = FALSE ;
00094 }
00095                                 
00096 
00097 void reset_other_threads( th_context *th, th_context *ctxt, VariantSF sgf )
00098 {
00099         th_context *next ;
00100         VariantSF resetsgf ;
00101         reset_thread( th, ctxt, sgf, &resetsgf );
00102         while( ctxt != th )
00103         {       next = ctxt->waiting_for_thread;
00104                 ctxt->deadlock_brk_leader = FALSE ;
00105                 ctxt->waiting_for_subgoal = resetsgf ;
00106                 ctxt->waiting_for_thread = th ;
00107                 if( next != th )
00108                         reset_thread( th, next, ctxt->waiting_for_subgoal,
00109                                         &resetsgf );
00110                 ctxt = next ;
00111         }
00112 }
00113 
00114 #endif

Generated on Wed Jul 26 13:30:40 2006 for XSB by  doxygen 1.4.5