00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "debugs/debug_delay.h"
00028
00029
00030
00031
00032
00033 case SLG_NOT: {
00034
00035 #ifdef DEBUG_ASSERTIONS
00036 const int Arity = 1;
00037 #endif
00038 const int regSF = 1;
00039 VariantSF sf;
00040
00041 sf = ptoc_addr(regSF);
00042 #ifdef DEBUG_ASSERTIONS
00043
00044
00045
00046
00047
00048 #endif
00049 if ( has_no_answers(sf) &&
00050 (is_completed(sf) || neg_delay == FALSE) )
00051 return TRUE;
00052 if ( has_unconditional_answers(sf) )
00053 return FALSE;
00054 else {
00055 delay_negatively(sf);
00056 return TRUE;
00057 }
00058 }
00059
00060
00061
00062 case LRD_SUCCESS: {
00063
00064 #ifdef DEBUG_ASSERTIONS
00065 const int Arity = 2;
00066 #endif
00067 const int regProducerSF = 1;
00068 const int regNegSubgoal = 2;
00069 SubProdSF sf;
00070
00071 sf = (SubProdSF)ptoc_addr(regProducerSF);
00072 #ifdef DEBUG_ASSERTIONS
00073 if ( ! smIsValidStructRef(smProdSF,sf) )
00074 xsb_abort("Invalid Table Entry Handle\n\t Argument %d of %s/%d",
00075 regProducerSF, BuiltinName(SLG_NOT), Arity);
00076 #endif
00077 if ( is_completed(sf) || neg_delay == FALSE )
00078 return TRUE;
00079 else {
00080
00081
00082
00083
00084
00085 static XSB_StrDefine(vsNegSubgoal);
00086 XSB_StrSet(&vsNegSubgoal,"");
00087 print_pterm(CTXTc ptoc_tag(CTXTc regNegSubgoal),1,&vsNegSubgoal);
00088 xsb_abort("Illegal table operation\n\t Attempted DELAY of negative "
00089 "subsumptive literal: %s", vsNegSubgoal.string);
00090 }
00091 }
00092
00093
00094
00095 case IS_INCOMPLETE: {
00096
00097 #ifdef DEBUG_ASSERTIONS
00098 const int Arity = 2;
00099 #endif
00100 const int regSubgoalFrame = 1;
00101 const int regRootSubgoal = 2;
00102
00103 #ifdef SHARED_COMPL_TABLES
00104 int table_tid ;
00105 th_context *waiting_for_thread ;
00106 #endif
00107
00108 VariantSF producerSF = ptoc_addr(regSubgoalFrame);
00109 CPtr t_ptcp = ptoc_addr(regRootSubgoal);
00110 #ifdef DEBUG_ASSERTIONS
00111
00112
00113
00114
00115
00116
00117 #endif
00118 xsb_dbgmsg((LOG_DELAY, "Is incomplete for "));
00119 dbg_print_subgoal(LOG_DELAY, stddbg, producerSF);
00120 xsb_dbgmsg((LOG_DELAY, ", (%x)\n", (int)&subg_ans_root_ptr(producerSF)));
00121
00122 #ifdef SHARED_COMPL_TABLES
00123
00124 pthread_mutex_lock(&completing_mut);
00125 while( !is_completed(producerSF) )
00126 {
00127 table_tid = subg_tid(producerSF) ;
00128
00129 if (table_tid == th->tid)
00130 break ;
00131 waiting_for_thread = find_context(table_tid) ;
00132 if( would_deadlock( waiting_for_thread, th ) )
00133 {
00134 reset_other_threads( th, waiting_for_thread, producerSF );
00135 th->deadlock_brk_leader = TRUE ;
00136 pthread_cond_broadcast(&completing_cond) ;
00137 reset_leader( th ) ;
00138 pthread_mutex_unlock(&completing_mut) ;
00139 return TRUE ;
00140 }
00141 th->waiting_for_subgoal = producerSF ;
00142 th->waiting_for_thread = waiting_for_thread ;
00143 pthread_cond_wait(&completing_cond,&completing_mut) ;
00144 if( th->reset_thread )
00145 { th->reset_thread = FALSE ;
00146 pthread_mutex_unlock(&completing_mut) ;
00147
00148 return TRUE ;
00149 }
00150 }
00151 th->waiting_for_thread = NULL ;
00152 th->waiting_for_subgoal = NULL ;
00153 pthread_mutex_unlock(&completing_mut);
00154 #endif
00155
00156 if (is_completed(producerSF)) {
00157 neg_delay = FALSE;
00158 ptcpreg = t_ptcp;
00159 return TRUE;
00160 }
00161 else {
00162 #ifdef SLG_GC
00163 CPtr old_cptop;
00164 #endif
00165
00166 xsb_dbgmsg((LOG_DELAY, "... Saving a completion suspension (~"));
00167 dbg_print_subgoal(LOG_DELAY, stddbg, producerSF);
00168 xsb_dbgmsg((LOG_DELAY, " in the body of "));
00169 dbg_print_subgoal(LOG_DELAY, stddbg, (VariantSF)t_ptcp);
00170 xsb_dbgmsg((LOG_DELAY,"an UNTABLED predicate"));
00171 xsb_dbgmsg((LOG_DELAY, ")\n"));
00172
00173 check_tcpstack_overflow;
00174
00175 adjust_level(subg_compl_stack_ptr(producerSF));
00176 save_find_locx(ereg);
00177 efreg = ebreg;
00178 if (trreg > trfreg) trfreg = trreg;
00179 if (hfreg < hreg) hfreg = hreg;
00180 if (bfreg > breg) bfreg = breg;
00181
00182 #ifdef SLG_GC
00183 old_cptop = bfreg;
00184 #endif
00185 save_compl_susp_frame(bfreg, ereg, (CPtr)producerSF, t_ptcp, cpreg);
00186 #ifdef SLG_GC
00187 csf_prevtop(bfreg) = old_cptop;
00188 #endif
00189 subg_compl_susp_ptr(producerSF) = bfreg;
00190 return FALSE;
00191 }
00192 }
00193
00194
00195
00196 case GET_PTCP:
00197 ctop_int(CTXTc 1, (Integer)ptcpreg);
00198 break;
00199
00200
00201
00202 case GET_DELAY_LISTS: {
00203
00204
00205
00206
00207
00208
00209
00210
00211 DL dl;
00212 DE de;
00213 BTNptr as_leaf;
00214 Cell delay_lists;
00215 CPtr dls_head, dls_tail = NULL;
00216
00217 #ifdef DEBUG_DELAYVAR
00218 xsb_mesg(">>>> (at the beginning of GET_DELAY_LISTS");
00219 xsb_mesg(">>>> global_num_vars = %d)",global_num_vars);
00220
00221 {
00222 int i;
00223 for (i = 0; i <= global_num_vars; i++) {
00224 Cell x;
00225 fprintf(stddbg, ">>>> var_regs[%d] =", i);
00226 x = (Cell)var_regs[i];
00227 XSB_Deref(x);
00228 printterm(stddbg, x, 25);
00229 fprintf(stddbg, "\n");
00230 }
00231 }
00232 #endif
00233
00234 as_leaf = (NODEptr) ptoc_int(CTXTc 1);
00235 delay_lists = ptoc_tag(CTXTc 2);
00236 if (is_conditional_answer(as_leaf)) {
00237 int copy_of_var_addr_arraysz;
00238 bind_list((CPtr)delay_lists, hreg);
00239 {
00240
00241
00242
00243
00244
00245
00246
00247
00248 int i;
00249 if (var_addr_arraysz < global_num_vars+1)
00250 trie_expand_array(CPtr,var_addr,var_addr_arraysz,global_num_vars+1,"var_addr");
00251 copy_of_var_addr_arraysz = var_addr_arraysz;
00252 copy_of_var_addr = (CPtr *)mem_calloc(copy_of_var_addr_arraysz, sizeof(CPtr),OTHER_SPACE);
00253 if(copy_of_var_addr == NULL){
00254 xsb_exit("No enough memory to calloc copy_of_var_addr!\nBye");
00255 }
00256 for( i = 0; i <= global_num_vars; i++)
00257 copy_of_var_addr[i] = var_regs[i];
00258
00259 copy_of_num_heap_term_vars = global_num_vars + 1;
00260 }
00261
00262 for (dl = asi_dl_list((ASI) Delay(as_leaf)); dl != NULL; ) {
00263 dls_head = hreg;
00264 dls_tail = hreg+1;
00265 new_heap_free(hreg);
00266 new_heap_free(hreg);
00267 de = dl_de_list(dl);
00268
00269 xsb_dbgmsg((LOG_DELAY, "orig_delayed_term("));
00270 dbg_print_subgoal(LOG_DELAY, stddbg, de_subgoal(de));
00271 xsb_dbgmsg((LOG_DELAY, ").\n"));
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 copy_of_num_heap_term_vars = global_num_vars + 1;
00282 build_delay_list(CTXTc dls_head, de);
00283 if ((dl = dl_next(dl)) != NULL) {
00284 bind_list(dls_tail, hreg);
00285 }
00286 }
00287 bind_nil(dls_tail);
00288 mem_dealloc(copy_of_var_addr,copy_of_var_addr_arraysz*sizeof(CPtr),OTHER_SPACE);
00289 } else {
00290 bind_nil((CPtr)delay_lists);
00291 }
00292 break;
00293 }
00294
00295
00296