From: Charles R. <cr...@us...> - 2005-07-26 22:46:45
|
Update of /cvsroot/xsb/XSB/emu In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3511 Modified Files: context.h odbc_xsb.c odbc_xsb.h thread_defs_xsb.h token_xsb.c tst_retrv.c unify_xsb_i.h Added Files: odbc_def_xsb.h Log Message: Fixed the ODBC interface in the multithreaded configuration. --- NEW FILE: odbc_def_xsb.h --- /* File: odbc_def_xsb.h ** Author(s): Lily Dong, Charles Rojo ** Contact: xsb...@cs..., c....@xs... ** ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998 ** ** XSB is free software; you can redistribute it and/or modify it under the ** terms of the GNU Library General Public License as published by the Free ** Software Foundation; either version 2 of the License, or (at your option) ** any later version. ** ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ** FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for ** more details. ** ** You should have received a copy of the GNU Library General Public License ** along with XSB; if not, write to the Free Software Foundation, ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ** ** ** */ #ifndef __ODBC_DEF_XSB_H__ #define __ODBC_DEF_XSB_H__ struct Cursor; struct NumberofCursors; #define ODBC_CONNECT 1 #define ODBC_PARSE 2 #define ODBC_SET_BIND_VAR_NUM 3 #define ODBC_FETCH_NEXT_ROW 4 #define ODBC_GET_COLUMN 5 #define ODBC_SET_BIND_VAL 6 #define ODBC_FIND_FREE_CURSOR 7 #define ODBC_DISCONNECT 8 #define ODBC_SET_CURSOR_CLOSE 9 #define ODBC_COMMIT 10 #define ODBC_ROLLBACK 11 #define ODBC_COLUMNS 12 #define ODBC_TABLES 13 #define ODBC_USER_TABLES 14 #define ODBC_DESCRIBE_SELECT 15 #define ODBC_CONNECT_OPTION 16 #define ODBC_DATA_SOURCES 17 #define ODBC_GET_INFO 18 #define ODBC_ROW_COUNT 19 #endif Index: context.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/context.h,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- context.h 22 Jul 2005 17:21:51 -0000 1.15 +++ context.h 26 Jul 2005 22:46:23 -0000 1.16 @@ -1,4 +1,4 @@ -/* File: emudef.h +/* File: context.h ** Author(s): Warren, Swift, Xu, Sagonas ** Contact: xsb...@cs... ** @@ -36,7 +36,7 @@ int nextch; }; -struct funstktype { +struct funstktype { char *fun; /* functor name */ int funop; /* index into opstk of first operand */ int funtyp; /* 0 if functor, 1 if list, 2 if dotted-tail list */ @@ -92,8 +92,23 @@ #include "tries.h" #include "choice.h" #include "macro_xsb.h" +#include "token_defs_xsb.h" +#include "odbc_def_xsb.h" #include "findall.h" + +//BELOW INCLUDES ARE FOR SQL Interfaces +#ifdef CYGWIN +#define FAR +#include "sql.h" +#else +#ifdef WIN_NT +#include <windows.h> +#endif +#include <sql.h> +#endif +//end of SQL related includes. + #define MAX_REGS 257 #define MAX_FLAGS 64 @@ -201,8 +216,6 @@ VarString *_tsgLBuff2; VarString *_tsgSBuff1; VarString *_tsgSBuff2; - - /* read_canonical stacks */ int _opstk_size; int _funstk_size; @@ -239,9 +252,17 @@ DynamicStack _tstSymbolStack; DynamicStack _tstTrail; +/* Pointers to cursor information used by odbc_xsb.c */ +/* context-local cursor table*/ + +struct Cursor *_FCursor; /* root of curser chain*/ +struct Cursor *_LCursor; /* tail of curser chain*/ +struct NumberofCursors *_FCurNum; + + /* stuff for deadlock detection in completion */ -struct th_context * waiting_for_thread; +struct th_context * waiting_for_thread; struct subgoal_frame * waiting_for_subgoal; int tid ; int deadlock_brk_leader ; @@ -331,7 +352,7 @@ #define LSBuff (th->_LSBuff) -#define last_answer (th->_last_answer) +#define last_answer (th->_last_answer) #define tsgLBuff1 (th->_tsgLBuff1) #define tsgLBuff2 (th->_tsgLBuff2) #define tsgSBuff1 (th->_tsgSBuff1) @@ -354,8 +375,8 @@ #define random_seeds (th->_random_seeds) -#define asrtBuff (th->_asrtBuff) -#define i_have_dyn_mutex (th->_i_have_dyn_mutex) +#define asrtBuff (th->_asrtBuff) +#define i_have_dyn_mutex (th->_i_have_dyn_mutex) #define AnsVarCtr (th->_AnsVarCtr) #define ans_var_pos_reg (th->_ans_var_pos_reg) @@ -368,12 +389,19 @@ #define tstSymbolStack (th->_tstSymbolStack) #define tstTrail (th->_tstTrail) +#define FCursor (th->_FCursor) +#define LCursor (th->_LCursor) +#define FCurNum (th->_FCurNum) + #define CTXT th #define CTXTc th , #define CTXTdecl th_context *th #define CTXTdeclc th_context *th , +#define CTXTdecltype th_context * +#define CTXTdecltypec th_context *, + #else #define CTXT @@ -382,6 +410,9 @@ #define CTXTdecl #define CTXTdeclc +#define CTXTdecltype +#define CTXTdecltypec + #endif /* MULTI_THREAD */ #endif /* __CONTEXT_H__ */ Index: odbc_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/odbc_xsb.c,v retrieving revision 1.49 retrieving revision 1.50 diff -u -r1.49 -r1.50 --- odbc_xsb.c 7 Jul 2005 18:59:43 -0000 1.49 +++ odbc_xsb.c 26 Jul 2005 22:46:23 -0000 1.50 @@ -61,19 +61,19 @@ //#include "error_xsb.h" #include "context.h" #include "varstring_xsb.h" +#include "thread_xsb.h" #define MAXCURSORNUM 25 #define MAXVARSTRLEN 65000 #define MAXI(a,b) ((a)>(b)?(a):(b)) -static Psc nullFctPsc; +static Psc nullFctPsc = NULL; /* static int numberOfCursors = 0; */ static long SQL_NTSval = SQL_NTS; static long SQL_NULL_DATAval = SQL_NULL_DATA; static HENV henv = NULL; /*HDBC hdbc;*/ -UCHAR uid[128]; struct Cursor { struct Cursor *NCursor; /* Next Cursor in cursor chain*/ @@ -92,10 +92,6 @@ UCHAR **Data; /* pointer to array of pointers to data*/ }; -/* global cursor table*/ -struct Cursor *FCursor; /* root of curser chain*/ -struct Cursor *LCursor; /* tail of curser chain*/ - /* Number of Cursors per Connection */ struct NumberofCursors{ HDBC hdbc; @@ -103,7 +99,15 @@ struct NumberofCursors *NCurNum; }; +//The below variables are declaired global for single-threaded XSB, and declaired +//in thread context for multi-threaded XSB. +#ifndef MULTI_THREAD struct NumberofCursors *FCurNum; /* First in the list of Number of Cursors */ +/* global cursor table*/ +struct Cursor *FCursor; /* root of curser chain*/ +struct Cursor *LCursor; /* tail of curser chain*/ + +#endif /* for debugging: just dumps memory... */ void print_hdbc(char *msg, HDBC hdbc) { @@ -451,6 +455,7 @@ /*-----------------------------------------------------------------------------*/ void ODBCConnect(CTXTdecl) { + UCHAR uid[128]; UCHAR *server; UCHAR *pwd; UCHAR *connectIn; @@ -460,6 +465,8 @@ /* if we don't yet have an environment, allocate one.*/ if (!henv) { + //locked to prevent two threads from fighting over who creates the env. + SYS_MUTEX_LOCK( MUTEX_ODBC) ; /* allocate environment handler*/ rc = SQLAllocEnv(&henv); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { @@ -473,7 +480,9 @@ LCursor = FCursor = NULL; FCurNum = NULL; - nullFctPsc = pair_psc(insert("NULL",1,global_mod,&new)); + if (nullFctPsc == NULL) + nullFctPsc = pair_psc(insert("NULL",1,global_mod,&new)); + SYS_MUTEX_UNLOCK( MUTEX_ODBC) ; } /* allocate connection handler*/ @@ -489,7 +498,7 @@ server = (UCHAR *)ptoc_string(CTXTc 3); strcpy(uid, (UCHAR *)ptoc_string(CTXTc 4)); pwd = (UCHAR *)ptoc_string(CTXTc 5); - + /* connect to database*/ rc = SQLConnect(hdbc, server, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { @@ -814,7 +823,7 @@ } } *((int *)cur->BindList[j]) = oint_val(BindVal); - } else if (isfloat(BindVal)) { + } else if (isofloat(BindVal)) { if (cur->BindTypes[j] != 1) { /*printf("ODBC: Changing Type: flt to %d\n",cur->BindTypes[j]);*/ if (cur->BindTypes[j] < 2) free((void *)cur->BindList[j]); @@ -829,7 +838,7 @@ return; } } - *((float *)cur->BindList[j]) = (float)float_val(BindVal); + *((float *)cur->BindList[j]) = (float)ofloat_val(BindVal); } else if (isstring(BindVal)) { if (cur->BindTypes[j] != 2) { /*printf("ODBC: Changing Type: str to %d\n",cur->BindTypes[j]);*/ @@ -871,12 +880,12 @@ if (!cur->BindList[j]) xsb_abort("[ODBC] Not enough memory for an int in SetBindVal!"); *((int *)cur->BindList[j]) = oint_val(BindVal); - } else if (isfloat(BindVal)) { + } else if (isofloat(BindVal)) { cur->BindTypes[j] = 1; cur->BindList[j] = (UCHAR *)malloc(sizeof(float)); if (!cur->BindList[j]) xsb_abort("[ODBC] Not enough memory for a float in SetBindVal!"); - *((float *)cur->BindList[j]) = (float)float_val(BindVal); + *((float *)cur->BindList[j]) = (float)ofloat_val(BindVal); } else if (isstring(BindVal)) { cur->BindTypes[j] = 2; cur->BindList[j] = string_val(BindVal); @@ -1186,7 +1195,7 @@ return 0; } } -extern xsbBool unify(Cell, Cell); +extern xsbBool unify(CTXTdecltypec Cell, Cell); /*-----------------------------------------------------------------------------*/ /* FUNCTION NAME:*/ @@ -1200,8 +1209,8 @@ /*-----------------------------------------------------------------------------*/ void ODBCDataSources(CTXTdecl) { - static SQLCHAR DSN[SQL_MAX_DSN_LENGTH+1]; - static SQLCHAR Description[SQL_MAX_DSN_LENGTH+1]; + SQLCHAR DSN[SQL_MAX_DSN_LENGTH+1]; + SQLCHAR Description[SQL_MAX_DSN_LENGTH+1]; RETCODE rc; int seq, new; SWORD dsn_size, descr_size; @@ -1209,6 +1218,8 @@ Cell op3 = ptoc_tag(CTXTc 4); if (!henv) { + //locked to prevent two threads from fighting over who creates the env + SYS_MUTEX_LOCK( MUTEX_ODBC) ; /* allocate environment handler*/ rc = SQLAllocEnv(&henv); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { @@ -1218,11 +1229,12 @@ } LCursor = FCursor = NULL; FCurNum = NULL; - nullFctPsc = pair_psc(insert("NULL",1,global_mod,&new)); + if (nullFctPsc == NULL) + nullFctPsc = pair_psc(insert("NULL",1,global_mod,&new)); + SYS_MUTEX_UNLOCK( MUTEX_ODBC) ; } seq = ptoc_int(CTXTc 2); - if (seq == 1) { rc = SQLDataSources(henv,SQL_FETCH_FIRST,DSN, SQL_MAX_DSN_LENGTH,&dsn_size, @@ -1254,7 +1266,11 @@ } XSB_Deref(op2); if (isref(op2)) - unify(op2, makestring(string_find(DSN,1))); + { + char * tempDSNstring= string_find(DSN,1); + Cell cellStr = makestring(tempDSNstring); + unify(CTXTc op2, cellStr); + } else { xsb_error("[ODBCDataSources] Param 2 should be a free variable."); ctop_int(CTXTc 5,1); @@ -1262,7 +1278,9 @@ } XSB_Deref(op3); if (isref(op3)) - unify(op3, makestring(string_find(Description,1))); + { + unify(CTXTc op3, makestring(string_find(Description,1))); + } else { xsb_error("[ODBCDataSources] Param 3 should be a free variable."); ctop_int(CTXTc 5,1); @@ -1473,13 +1491,11 @@ int ColCurNum = ptoc_int(CTXTc 3); Cell op = ptoc_tag(CTXTc 4); UDWORD len; - if (ColCurNum < 0 || ColCurNum >= cur->NumCols) { /* no more columns in the result row*/ ctop_int(CTXTc 5,1); return TRUE; } - ctop_int(CTXTc 5,0); /* get the data*/ @@ -1490,8 +1506,8 @@ new_heap_free(hreg); if (isconstr(op) && get_arity(get_str_psc(op)) == 1) /* for "string" and "term"... */ - return unify(cell(clref_val(op)+1),nullterm); - else return unify(op,nullterm); + return unify(CTXTc cell(clref_val(op)+1),nullterm); + else return unify(CTXTc op,nullterm); } /* convert the string to either integer, float or string*/ @@ -1506,10 +1522,10 @@ /* compare strings here, so don't intern strings unnecessarily*/ XSB_Deref(op); if (isref(op)) - return unify(op, makestring(string_find(cur->Data[ColCurNum],1))); + return unify(CTXTc op, makestring(string_find(cur->Data[ColCurNum],1))); if (isconstr(op) && get_arity(get_str_psc(op)) == 1) { if (!strcmp(get_name(get_str_psc(op)),"string")) { - return unify(cell(clref_val(ptoc_tag(CTXTc 4))+1), /* op might have moved! */ + return unify(CTXTc cell(clref_val(ptoc_tag(CTXTc 4))+1), /* op might have moved! */ build_codes_list(CTXTc cur->Data[ColCurNum])); } else { STRFILE strfile; @@ -1534,10 +1550,10 @@ /* compare strings here, so don't intern strings unnecessarily*/ XSB_Deref(op); if (isref(op)) - return unify(op, makestring(string_find(cur->Data[ColCurNum],1))); + return unify(CTXTc op, makestring(string_find(cur->Data[ColCurNum],1))); if (isconstr(op) && get_arity(get_str_psc(op)) == 1) { if (!strcmp(get_name(get_str_psc(op)),"string")) { - return unify(cell(clref_val(ptoc_tag(CTXTc 4))+1), /* op might have moved! */ + return unify(CTXTc cell(clref_val(ptoc_tag(CTXTc 4))+1), /* op might have moved! */ build_codes_list(CTXTc cur->Data[ColCurNum])); } else { STRFILE strfile; @@ -1552,11 +1568,10 @@ if (strcmp(string_val(op),cur->Data[ColCurNum])) return FALSE; return TRUE; case SQL_C_SLONG: - return unify(op,makeint(*(long *)(cur->Data[ColCurNum]))); + return unify(CTXTc op,makeint(*(long *)(cur->Data[ColCurNum]))); case SQL_C_FLOAT: - return unify(op,makefloat(*(float *)(cur->Data[ColCurNum]))); + return unify(CTXTc op,makefloat(*(float *)(cur->Data[ColCurNum]))); } - return FALSE; } /*-----------------------------------------------------------------------------*/ Index: odbc_xsb.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/odbc_xsb.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- odbc_xsb.h 11 Feb 2005 01:54:27 -0000 1.7 +++ odbc_xsb.h 26 Jul 2005 22:46:24 -0000 1.8 @@ -22,9 +22,16 @@ ** */ +#ifndef __ODBC_XSB_H__ +#define __ODBC_XSB_H__ #ifdef XSB_ODBC +#include "odbc_def_xsb.h" + +//the function declarations are kept seperate from the constant and structure declarations +//of ODBC_XSB since each function depends on CTXTdecl from context.h, but context.h depends on the +//structure declarations of ODBC_XSB. extern void ODBCConnect(CTXTdecl); extern void ODBCDisconnect(CTXTdecl); extern void SetBindVarNum(CTXTdecl); @@ -45,25 +52,7 @@ extern void ODBCGetInfo(CTXTdecl); extern void ODBCRowCount(CTXTdecl); -#define ODBC_CONNECT 1 -#define ODBC_PARSE 2 -#define ODBC_SET_BIND_VAR_NUM 3 -#define ODBC_FETCH_NEXT_ROW 4 -#define ODBC_GET_COLUMN 5 -#define ODBC_SET_BIND_VAL 6 -#define ODBC_FIND_FREE_CURSOR 7 -#define ODBC_DISCONNECT 8 -#define ODBC_SET_CURSOR_CLOSE 9 -#define ODBC_COMMIT 10 -#define ODBC_ROLLBACK 11 -#define ODBC_COLUMNS 12 -#define ODBC_TABLES 13 -#define ODBC_USER_TABLES 14 -#define ODBC_DESCRIBE_SELECT 15 -#define ODBC_CONNECT_OPTION 16 -#define ODBC_DATA_SOURCES 17 -#define ODBC_GET_INFO 18 -#define ODBC_ROW_COUNT 19 +#endif /*XSB_ODBC defined*/ -#endif +#endif /*this header included*/ Index: thread_defs_xsb.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/thread_defs_xsb.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- thread_defs_xsb.h 11 Jul 2005 21:43:57 -0000 1.6 +++ thread_defs_xsb.h 26 Jul 2005 22:46:24 -0000 1.7 @@ -53,6 +53,7 @@ #define MUTEX_STACKS 18 #define MUTEX_SOCKETS 19 #define MUTEX_MEM 20 +#define MUTEX_ODBC 21 /* Some mutexes available to users */ #define MUTEX_CONSOLE 30 Index: token_xsb.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/token_xsb.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- token_xsb.c 8 Jul 2005 20:25:22 -0000 1.15 +++ token_xsb.c 26 Jul 2005 22:46:25 -0000 1.16 @@ -350,7 +350,7 @@ c = GetC(card,instr); BACK: if (c < 0) { if (c == EOF) /* to mostly handle cygwin stdio.h bug ... */ -ERROR: if (q < 0) { +READ_ERROR: if (q < 0) { SyntaxError("end of file in character constant"); return -2; /* encounters EOF */ } else { @@ -375,7 +375,7 @@ switch (c) { case EOF: clearerr(card); - goto ERROR; + goto READ_ERROR; case 'n': case 'N': /* newline */ return 10; case 't': case 'T': /* tab */ @@ -398,7 +398,7 @@ return 7; case '^': /* control */ c = GetC(card,instr); - if (c < 0) goto ERROR; + if (c < 0) goto READ_ERROR; return c == '?' ? 127 : c&31; case 'c': case 'C': /* continuation */ while (IsLayout(c = GetC(card,instr))) ; @@ -407,7 +407,7 @@ { int i, n; for (n = 0, i = 2; --i >= 0; n = (n<<4) + DigVal(c)) if (DigVal(c = GetC(card,instr)) >= 16) { - if (c < 0) goto ERROR; + if (c < 0) goto READ_ERROR; (void)unGetC(c, card, instr); break; } @@ -416,7 +416,7 @@ case 'o': case 'O': /* octal */ c = GetC(card,instr); if (DigVal(c) >= 8) { - if (c < 0) goto ERROR; + if (c < 0) goto READ_ERROR; (void) unGetC(c, card, instr); return 0; } @@ -425,7 +425,7 @@ { int i, n; for (n = c-'0', i = 2; --i >= 0; n = (n<<3) + DigVal(c)) if (DigVal(c = GetC(card,instr)) >= 8) { - if (c < 0) goto ERROR; + if (c < 0) goto READ_ERROR; (void) unGetC(c, card, instr); break; } Index: tst_retrv.c =================================================================== RCS file: /cvsroot/xsb/XSB/emu/tst_retrv.c,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- tst_retrv.c 14 Jan 2005 18:31:39 -0000 1.19 +++ tst_retrv.c 26 Jul 2005 22:46:25 -0000 1.20 @@ -40,7 +40,7 @@ #include "binding.h" #include "cut_xsb.h" /* trail frame field access macros */ #include "sw_envs.h" -#include "subp.h" /* xsbBool unify(Cell, Cell) */ +#include "subp.h" /* xsbBool unify(CTXTc Cell, Cell) */ #include "table_stats.h" #include "trie_internals.h" #include "macro_xsb.h" Index: unify_xsb_i.h =================================================================== RCS file: /cvsroot/xsb/XSB/emu/unify_xsb_i.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- unify_xsb_i.h 14 Jan 2005 18:31:54 -0000 1.9 +++ unify_xsb_i.h 26 Jul 2005 22:46:25 -0000 1.10 @@ -86,7 +86,7 @@ \ op1 = (Cell)(clref_val(op1));\ op2 = (Cell)(clref_val(op2));\ - if ( !unify(cell((CPtr)op1), cell((CPtr)op2)))\ + if ( !unify(CTXTc cell((CPtr)op1), cell((CPtr)op2)))\ { IFTHEN_FAILED; }\ op1 = (Cell)((CPtr)op1+1);\ op2 = (Cell)((CPtr)op2+1);\ @@ -109,7 +109,7 @@ while (--arity)\ {\ op1 = (Cell)((CPtr)op1+1); op2 = (Cell)((CPtr)op2+1);\ - if (!unify(cell((CPtr)op1), cell((CPtr)op2)))\ + if (!unify(CTXTc cell((CPtr)op1), cell((CPtr)op2)))\ {\ IFTHEN_FAILED;\ }\ |