context.h

00001 /* File:      context.h
00002 ** Contact:   xsb-contact@cs.sunysb.edu
00003 ** 
00004 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00005 ** Copyright (C) ECRC, Germany, 1990
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: context.h,v 1.45 2006/04/30 18:27:13 tswift Exp $
00022 ** 
00023 */
00024 
00025 #ifndef __CONTEXT_H__
00026 #define __CONTEXT_H__
00027 
00028 #include "cell_def_xsb.h"
00029 #include "basictypes.h"
00030 
00031 #include "setjmp_xsb.h"
00032 #include "flag_defs_xsb.h"
00033 #include "conc_compl.h"
00034 #include "hashtable_xsb.h"
00035 
00036 /* Note that ClRef pointers typically point to the end of a ClRef, so
00037    to access the components, the pointer must be decremented! */
00038 
00039 typedef struct ClRefHdr
00040 {       unsigned long buflen ;
00041         struct ClRefHdr *prev ;
00042 }       *ClRef, ClRefData, ClRefHdr ;
00043 
00044 #define ClRef_Buflen(CLREF)        ( (CLREF)->buflen )
00045 #define ClRef_Prev(CLREF)          ( (CLREF)->prev )
00046 
00047 struct token_t {
00048   int type;
00049   char *value;
00050   int nextch;
00051 };
00052 
00053 struct funstktype {                                         
00054     char *fun;          /* functor name */
00055     int funop;          /* index into opstk of first operand */
00056     int funtyp;         /* 0 if functor, 1 if list, 2 if dotted-tail list */
00057 };
00058 
00059 struct opstktype {
00060     int typ;
00061     prolog_term op;
00062 };
00063 
00064 #define MAXVAR 1000
00065 struct vartype {
00066   Cell varid;
00067   prolog_term varval;
00068 };
00069 
00070 struct sort_par_spec {
00071   long sort_num_pars;
00072   long sort_par_dir[10];
00073   long sort_par_ind[10];
00074 };
00075 
00076 struct random_seeds_t {
00077   short IX;
00078   short IY;
00079   short IZ;
00080 
00081   double TX;
00082   double TY;
00083   double TZ;
00084 };
00085 
00086 struct asrtBuff_t {
00087  char *Buff;
00088  int Buff_size;
00089  int *Loc;
00090  int BLim;
00091  int Size;
00092 };
00093 
00094 #define MAX_RETRACTED_CLAUSES   20
00095 
00096 #ifdef MULTI_THREAD
00097 
00098 #include <sys/types.h>
00099 #include <stdio.h>
00100 #include <pthread.h>
00101 
00102 #include "debugs/debug_attv.h"
00103 
00104 #include "basictypes.h"
00105 #include "memory_xsb.h"
00106 #include "varstring_xsb.h"
00107 #include "dynamic_stack.h"
00108 #include "psc_xsb.h"
00109 #include "tries.h"
00110 #include "choice.h"
00111 #include "macro_xsb.h"
00112 #include "token_defs_xsb.h"
00113 #include "odbc_def_xsb.h"
00114 #include "findall.h"
00115 #include "struct_manager.h"
00116 
00117 //BELOW INCLUDES ARE FOR SQL Interfaces
00118 #ifdef CYGWIN
00119 #define FAR
00120 #include "sql.h"
00121 #else
00122 #ifdef WIN_NT
00123 #include <windows.h>
00124 #include <sql.h>
00125 #endif
00126 #endif
00127 //end of SQL related includes.
00128 
00129 #define MAX_REGS 257
00130 
00131 /************************************************************************/
00132 
00133 struct th_context
00134 {
00135 /* System & user Flags */
00136 
00137   int _call_intercept ;
00138 
00139 /* The SLG-WAM data regions
00140    ------------------------ */
00141 
00142   System_Stack  _pdl,           /* PDL                   */
00143     _glstack,   /* Global + Local Stacks */
00144     _tcpstack,  /* Trail + CP Stack      */
00145     _complstack;        /* Completion Stack  */
00146 
00147 /* Argument Registers
00148    ------------------ */
00149   Cell _reg[MAX_REGS];
00150 
00151 
00152 /* Special Registers
00153    ----------------- */
00154   CPtr _ereg;           /* last activation record       */
00155   CPtr _breg;           /* last choice point            */
00156   CPtr _hreg;           /* top of heap                  */
00157   CPtr *_trreg;         /* top of trail stack           */
00158   CPtr _hbreg;          /* heap back track point        */
00159   CPtr _sreg;           /* current build or unify field */
00160   byte *_cpreg;         /* return point register        */
00161   byte *_pcreg;         /* program counter              */
00162   CPtr _ebreg;          /* breg into environment stack  */
00163 
00164   CPtr _efreg;
00165   CPtr _bfreg;
00166   CPtr _hfreg;
00167   CPtr *_trfreg;
00168   CPtr _pdlreg;
00169   CPtr _openreg;
00170   xsbBool _neg_delay;
00171   int _xwammode;
00172   int _level_num;
00173   CPtr _root_address;
00174 
00175   CPtr _ptcpreg;
00176   CPtr _delayreg;
00177   CPtr _interrupt_reg;
00178   Cell _interrupt_counter;
00179 
00180   int _asynint_code;
00181   int _asynint_val;
00182 
00183   /*********** Global Variables for various tries --- some of this may
00184   be able to be changed to local variables.  Regarray size is the size
00185   of the reg_array, used for expanding the reg_array (and is
00186   reset). ***********/
00187 
00188   Cell *_reg_array;
00189   CPtr _reg_arrayptr;
00190   int  _reg_array_size;
00191 
00192 #define MAX_TRIE_REGS 500
00193   CPtr _var_regs[MAX_TRIE_REGS];
00194   int  _num_vars_in_var_regs ;
00195 
00196   int  _num_heap_term_vars;
00197   CPtr *_var_addr;
00198   int  _var_addr_arraysz;
00199 
00200   Cell _VarEnumerator[NUM_TRIEVARS];
00201   Cell _TrieVarBindings[NUM_TRIEVARS];
00202 
00203   CPtr _VarEnumerator_trail[NUM_TRIEVARS];
00204   CPtr *_VarEnumerator_trail_top;
00205 
00206   int _addr_stack_pointer;
00207   CPtr *_Addr_Stack;
00208   int _addr_stack_size;
00209                                                                                 
00210   int  _term_stackptr;
00211   Cell *_term_stack;
00212   long _term_stacksize;
00213 
00214   int _global_num_vars;
00215 
00216   struct tif_list _private_tif_list;
00217   DelTFptr _private_deltf_chain_begin;
00218   DelCFptr _private_delcf_chain_begin;
00219 
00220   BTNptr  _NodePtr, 
00221     _Last_Nod_Sav;
00222 
00223   int     _delay_it;
00224 
00225   int   _AnsVarCtr;
00226   CPtr  _ans_var_pos_reg;
00227 
00228 /* Flag used in the locking of called tries */
00229   int   trie_locked;
00230 
00231   IGRptr _IGRhead;
00232 
00233   /* Variables for subsumptive and TST tries.  */
00234 struct VariantContinuation *_variant_cont;
00235 struct tstCCPStack_t *_tstCCPStack;
00236 struct tstCPStack_t *_tstCPStack;
00237 CPtr *_trail_base;
00238 CPtr *_orig_trreg;            
00239 CPtr _orig_hreg;
00240 CPtr _orig_hbreg;
00241 CPtr _orig_ebreg;
00242 
00243 DynamicStack  _tstTermStack;
00244 DynamicStack  _tstTermStackLog;
00245 DynamicStack  _tstSymbolStack;
00246 DynamicStack  _tstTrail;
00247 
00248   /* Error checking for TST unification */
00249 BTNptr _gAnsLeaf;    
00250 CPtr _gAnsTmplt;     
00251 int _gSizeTmplt;     
00252 
00253   /* delay, simplification, etc. */
00254 Cell _cell_array[500];
00255 CPtr *_copy_of_var_addr;
00256 int _copy_of_num_heap_term_vars;
00257 
00258   /********* Variables for array of interned tries *********/
00259 BTNptr *_Set_ArrayPtr;
00260   Integer _first_free_set;
00261   int _Set_ArraySz;
00262   int _num_sets;
00263 
00264   /* for backtrackable updates & assoc arrays (storage_xsb) */
00265   xsbHashTable _bt_storage_hash_table;
00266 
00267   /********** variables for findall buffers **********/
00268 findall_solution_list *_findall_solutions;  /*= NULL;*/
00269 findall_solution_list *_current_findall;
00270 int     _nextfree ; /* nextfree index in findall array */
00271 CPtr    _gl_bot, _gl_top ;
00272 f_tr_chunk *_cur_tr_chunk ;
00273 CPtr    *_cur_tr_top ;
00274 CPtr    *_cur_tr_limit ;
00275 
00276 VarString **_LSBuff; /* 30 buffers for making longstring in ctop_longstring */
00277 
00278   /********** Global thread-specific charstring buffers for local use
00279               within io-builtins **********/
00280 
00281 VarString *_last_answer;  /* for c-calling-xsb interface */
00282 VarString *_tsgLBuff1;
00283 VarString *_tsgLBuff2;
00284 VarString *_tsgSBuff1;
00285 VarString *_tsgSBuff2;
00286 /* read_canonical stacks */
00287 int _opstk_size;
00288 int _funstk_size;
00289 struct funstktype *_funstk;
00290 struct opstktype *_opstk;
00291 struct vartype *_rc_vars;
00292 
00293   /********** Global variables for tokenizing **********/
00294 struct token_t *_token;
00295 int     _lastc; // = ' ';    /* previous character */
00296 char*   _strbuff; // = NULL;  /* Pointer to token buffer; Will be allocated on first call to GetToken */
00297 int     _strbuff_len; // = InitStrLen;  /* first allocation size, doubled on subsequent overflows */
00298 double  _double_v;
00299 long    _rad_int;
00300 int _token_too_long_warning;
00301 
00302 struct sort_par_spec _par_spec;         /* spec for par_sort */
00303 
00304   /********** Global variables for assert / retract **********/
00305   /* used for C-level longjumps in assert */
00306 jmp_buf _assertcmp_env;
00307 
00308 ClRef _retracted_buffer[MAX_RETRACTED_CLAUSES+1];
00309 ClRef *_OldestCl;
00310 ClRef *_NewestCl;
00311 struct asrtBuff_t *_asrtBuff;   /* assert code buffer */
00312 int    _i_have_dyn_mutex;       /* This thread has dynamic mutex, for asserted code read */
00313 
00314 struct random_seeds_t *_random_seeds;   /* struct containing seeds for random num gen */
00315 
00316   /* Pointers to common structure managers (table vs. assert) */
00317   struct Structure_Manager *_smBTN;
00318   struct Structure_Manager *_smBTHT;
00319 
00320   /* private structure managers */
00321   Structure_Manager *_private_smTableBTN;
00322   Structure_Manager *_private_smTableBTHT;
00323   Structure_Manager *_private_smTSTN; 
00324   Structure_Manager *_private_smTSTHT;
00325   Structure_Manager *_private_smTSIN;
00326   Structure_Manager *_private_smVarSF; 
00327   Structure_Manager *_private_smProdSF;
00328   Structure_Manager *_private_smConsSF;
00329   Structure_Manager *_private_smALN;
00330 
00331   int    _threads_current_sm;
00332   
00333 
00334   /********** Error handling  **********/
00335 
00336 byte *_catch_scope_marker;
00337 jmp_buf _xsb_abort_fallback_environment;
00338 
00339   /********** cinterf stuff  **********/
00340 jmp_buf _cinterf_env;
00341 
00342  /************ Pointers to cursor information used by
00343  odbc_xsb.c context-local cursor table ***********/
00344 
00345 struct Cursor *_FCursor;  /* root of curser chain*/
00346 struct Cursor *_LCursor;  /* tail of curser chain*/
00347 struct NumberofCursors *_FCurNum;
00348 
00349 /* Private flags */
00350 
00351 Cell _pflags[MAX_PRIVATE_FLAGS];
00352 
00353 /* Thread Id (for fast access) */
00354 
00355 int tid ;
00356 
00357 /* stuff for deadlock detection in completion */
00358 #ifdef SHARED_COMPL_TABLES
00359 struct th_context *     waiting_for_thread;
00360 struct subgoal_frame *  waiting_for_subgoal;
00361 int deadlock_brk_leader ;
00362 int reset_thread ;
00363 #endif
00364 
00365 #ifdef CONC_COMPL
00366 int completing ;
00367 int completed ;
00368 int last_ans ;
00369 CPtr cc_leader ;
00370 ThreadDepList TDL ;
00371 pthread_cond_t cond_var ;
00372 #endif
00373 
00374 Cell _attv_interrupts[20480][2];
00375 
00376 int _num_gc;
00377 double _total_time_gc;
00378 unsigned long _total_collected;
00379   
00380 } ;
00381 
00382 typedef struct th_context th_context ;
00383 
00384 
00385 #define xsb_thread_id           (th -> tid)
00386 
00387 #define call_intercept          (th->_call_intercept)
00388 
00389 #define pdl                     (th->_pdl)
00390 #define glstack                 (th->_glstack)
00391 #define tcpstack                (th->_tcpstack)
00392 #define complstack              (th->_complstack)
00393 
00394 #define reg                     (th->_reg)
00395 
00396 #define ereg                    (th->_ereg)
00397 #define breg                    (th->_breg)
00398 #define hreg                    (th->_hreg)
00399 #define trreg                   (th->_trreg)
00400 #define hbreg                   (th->_hbreg)
00401 #define sreg                    (th->_sreg)
00402 #define cpreg                   (th->_cpreg)
00403 #define pcreg                   (th->_pcreg)
00404 #define ebreg                   (th->_ebreg)
00405 
00406 #define efreg                   (th->_efreg)
00407 #define bfreg                   (th->_bfreg)
00408 #define hfreg                   (th->_hfreg)
00409 #define trfreg                  (th->_trfreg)
00410 
00411 #define pdlreg                  (th->_pdlreg)
00412 #define openreg                 (th->_openreg)
00413 #define neg_delay               (th->_neg_delay)
00414 #define xwammode                (th->_xwammode)
00415 #define level_num               (th->_level_num)
00416 #define root_address            (th->_root_address)
00417 
00418 #define ptcpreg                 (th->_ptcpreg)
00419 #define delayreg                (th->_delayreg)
00420 #define interrupt_reg           (th->_interrupt_reg)
00421 #define interrupt_counter       (th->_interrupt_counter)
00422 
00423 #define asynint_code            (th->_asynint_code)
00424 #define asynint_val             (th->_asynint_val)
00425 
00426 #define reg_array               (th->_reg_array)
00427 #define reg_arrayptr            (th->_reg_arrayptr)
00428 #define reg_array_size          (th->_reg_array_size)
00429 
00430 #define var_regs                (th->_var_regs)
00431 #define num_vars_in_var_regs    (th->_num_vars_in_var_regs)
00432 
00433 #define num_heap_term_vars      (th->_num_heap_term_vars)
00434 #define var_addr                (th->_var_addr)
00435 #define var_addr_arraysz        (th->_var_addr_arraysz)
00436 
00437 #define VarEnumerator           (th->_VarEnumerator)
00438 #define TrieVarBindings         (th->_TrieVarBindings)
00439 
00440 #define VarEnumerator_trail     (th->_VarEnumerator_trail)
00441 #define VarEnumerator_trail_top (th->_VarEnumerator_trail_top)
00442 
00443 #define addr_stack_pointer      (th->_addr_stack_pointer)
00444 #define Addr_Stack              (th->_Addr_Stack)
00445 #define addr_stack_size         (th->_addr_stack_size)
00446 
00447 #define term_stackptr           (th->_term_stackptr)
00448 #define term_stack              (th->_term_stack)
00449 #define term_stacksize          (th->_term_stacksize)
00450 
00451 #define global_num_vars         (th->_global_num_vars)
00452 
00453 #define private_tif_list        (th-> _private_tif_list)
00454 #define private_deltf_chain_begin (th-> _private_deltf_chain_begin)
00455 #define private_delcf_chain_begin (th-> _private_delcf_chain_begin)
00456 
00457 #define NodePtr                 (th->_NodePtr)
00458 #define Last_Nod_Sav            (th->_Last_Nod_Sav)
00459 
00460 #define delay_it                (th->_delay_it)
00461 
00462 #define IGRhead                 (th->_IGRhead)
00463 
00464 #define variant_cont            (*(th->_variant_cont))
00465 #define a_variant_cont          (th->_variant_cont)
00466 #define tstCCPStack             (*(th->_tstCCPStack))
00467 #define a_tstCCPStack           (th->_tstCCPStack)
00468 #define tstCPStack              (*(th->_tstCPStack))
00469 #define a_tstCPStack            (th->_tstCPStack)
00470 
00471 #define trail_base              (th->_trail_base)
00472 #define orig_trreg              (th->_orig_trreg)
00473 #define orig_hreg               (th->_orig_hreg)
00474 #define orig_hbreg              (th->_orig_hbreg)
00475 #define orig_ebreg              (th->_orig_ebreg)
00476 
00477 #define findall_solutions       (th->_findall_solutions)
00478 #define current_findall         (th->_current_findall)
00479 #define nextfree                (th->_nextfree)
00480 #define gl_bot                  (th->_gl_bot)
00481 #define gl_top                  (th->_gl_top)
00482 #define cur_tr_chunk            (th->_cur_tr_chunk)
00483 #define cur_tr_top              (th->_cur_tr_top)
00484 #define cur_tr_limit            (th->_cur_tr_limit)
00485 
00486 #define LSBuff                  (th->_LSBuff)
00487 
00488 #define last_answer           (th->_last_answer)
00489 #define tsgLBuff1               (th->_tsgLBuff1)
00490 #define tsgLBuff2               (th->_tsgLBuff2)
00491 #define tsgSBuff1               (th->_tsgSBuff1)
00492 #define tsgSBuff2               (th->_tsgSBuff2)
00493 
00494 #define opstk_size              (th->_opstk_size)
00495 #define funstk_size             (th->_funstk_size)
00496 #define funstk                  (th->_funstk)
00497 #define opstk                   (th->_opstk)
00498 #define rc_vars                 (th->_rc_vars)
00499 
00500 #define token                   (th->_token)
00501 #define lastc                   (th->_lastc)
00502 #define strbuff                 (th->_strbuff)
00503 #define strbuff_len             (th->_strbuff_len)
00504 #define double_v                (th->_double_v)
00505 #define rad_int                 (th->_rad_int)
00506 #define token_too_long_warning  (th->_token_too_long_warning)
00507 
00508 #define par_spec                (th->_par_spec)
00509 
00510 #define assertcmp_env           (th->_assertcmp_env)
00511 #define cinterf_env             (th->_cinterf_env)
00512 
00513 #define retracted_buffer        (th->_retracted_buffer)
00514 #define OldestCl                (th->_OldestCl)
00515 #define NewestCl                (th->_NewestCl)
00516 
00517 #define xsb_abort_fallback_environment (th->_xsb_abort_fallback_environment)
00518 #define catch_scope_marker            (th->_catch_scope_marker)
00519 
00520 #define random_seeds            (th->_random_seeds)
00521 
00522 #define asrtBuff              (th->_asrtBuff)
00523 #define i_have_dyn_mutex      (th->_i_have_dyn_mutex)
00524 
00525 #define AnsVarCtr               (th->_AnsVarCtr)
00526 #define ans_var_pos_reg         (th->_ans_var_pos_reg)
00527 
00528 /******/
00529 
00530 #define smBTN                   (th->_smBTN)
00531 #define smBTHT                  (th->_smBTHT)
00532 
00533 #define private_smTableBTN        (th->_private_smTableBTN)
00534 #define private_smTableBTHT       (th->_private_smTableBTHT)
00535 #define private_smTSTN          (th-> _private_smTSTN)
00536 #define private_smTSTHT         (th-> _private_smTSTHT)
00537 #define private_smTSIN          (th-> _private_smTSIN)
00538 #define private_smVarSF         (th-> _private_smVarSF)
00539 #define private_smProdSF        (th-> _private_smProdSF)
00540 #define private_smConsSF        (th-> _private_smConsSF)
00541 #define private_smALN           (th-> _private_smALN)
00542 
00543 #define subsumptive_smALN       (*private_smALN)
00544 #define subsumptive_smBTN       (*private_smTableBTN)
00545 #define subsumptive_smBTHT      (*private_smTableBTHT)
00546 
00547 #define threads_current_sm      (th->_threads_current_sm)
00548 
00549 /* For now, Subsumptive-tables are all private*/
00550 #define smProdSF                (*private_smProdSF)
00551 #define smConsSF                (*private_smConsSF)
00552 #define smTSTN                  (*private_smTSTN)
00553 #define smTSIN                  (*private_smTSIN)
00554 #define smTSTHT                 (*private_smTSTHT)
00555 
00556 /******/
00557 
00558 #define  tstTermStack           (th->_tstTermStack)
00559 #define  tstTermStackLog        (th->_tstTermStackLog)
00560 #define  tstSymbolStack         (th->_tstSymbolStack)
00561 #define  tstTrail               (th->_tstTrail)
00562 
00563 #define  gAnsLeaf               (th->_gAnsLeaf)
00564 #define  gAnsTmplt              (th->_gAnsTmplt)
00565 #define  gSizeTmplt             (th->_gSizeTmplt)
00566 
00567 #define  cell_array                         (th->_cell_array)
00568 #define  copy_of_var_addr                   (th->_copy_of_var_addr)
00569 #define  copy_of_num_heap_term_vars         (th->_copy_of_num_heap_term_vars)
00570 
00571 #define  Set_ArrayPtr           (th->_Set_ArrayPtr)
00572 #define  first_free_set         (th->_first_free_set)
00573 #define  Set_ArraySz            (th->_Set_ArraySz)
00574 #define  num_sets               (th->_num_sets)
00575 
00576 #define  bt_storage_hash_table             (th-> _bt_storage_hash_table)
00577 
00578 #define FCursor (th->_FCursor)
00579 #define LCursor (th->_LCursor)
00580 #define FCurNum (th->_FCurNum)
00581 
00582 #define  pflags                 (th->_pflags)
00583 
00584 #define  attv_interrupts        (th->_attv_interrupts)
00585 
00586 #define num_gc                  (th->_num_gc)
00587 #define total_time_gc           (th->_total_time_gc)
00588 #define total_collected         (th->_total_collected)
00589 
00590 #define CTXT                    th
00591 #define CTXTc                   th ,
00592 
00593 #define CTXTdecl                th_context *th
00594 #define CTXTdeclc               th_context *th ,
00595 
00596 #define CTXTdecltype            th_context *
00597 #define CTXTdecltypec           th_context *,
00598 
00599 #else
00600 
00601 #define CTXT
00602 #define CTXTc
00603 
00604 #define CTXTdecl
00605 #define CTXTdeclc
00606 
00607 #define CTXTdecltype
00608 #define CTXTdecltypec
00609 
00610 #define subsumptive_smBTN        smTableBTN
00611 #define subsumptive_smBTHT       smTableBTHT
00612 
00613 
00614 #endif /* MULTI_THREAD */
00615 
00616 #endif /* __CONTEXT_H__ */

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