From: Terrance S. <ts...@us...> - 2007-02-22 00:22:00
|
Update of /cvsroot/xsb/XSB/examples/c_calling_XSB In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv15027 Added Files: cvarstring_thread2.c make_thread2.P Log Message: Examples of many C threads calling many XSB threads. --- NEW FILE: cvarstring_thread2.c --- /* File: cvarstring.c ** Author(s): Swift ** Contact: xsb...@cs... ** ** 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. ** ** $Id: cvarstring_thread2.c,v 1.1 2007/02/22 00:21:57 tswift Exp $ ** */ /* Simple example file showing how to call XSB from C without varstrings * To make this file, see the instructions in ./README */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <pthread.h> /* cinterf.h is necessary for the XSB API, as well as the path manipulation routines*/ #include "cinterf.h" extern char *xsb_executable_full_path(char *); extern char *strip_names_from_path(char*, int); /* context.h is necessary for the type of a thread context. */ #include "context.h" #include "thread_xsb.h" /******************************************************************* Succeeding commands *********************************************************************/ void *command_ps(void * arg) { int rc; th_context *p_th; p_th = (th_context *)arg; if ((rc = xsb_command_string(p_th,"test_p.")) == XSB_ERROR) fprintf(stderr,"Command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); return NULL; } void *command_rs(void * arg) { int rc; th_context *r_th; r_th = (th_context *)arg; if ((rc = xsb_command_string(r_th,"test_r.")) == XSB_ERROR) fprintf(stderr,"++Command Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); return NULL; } /******************************************************************* Failing commands *********************************************************************/ void *command_ps_fail(void * arg) { int rc; th_context *p_th; p_th = (th_context *)arg; if ((rc = xsb_command_string(p_th,"test_p_fail.")) == XSB_ERROR) fprintf(stderr,"Command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); else if ((rc = xsb_command_string(p_th,"test_p_fail.")) == XSB_FAILURE) fprintf(stderr,"p_fail failed properly\n"); return NULL; } void *command_rs_fail(void * arg) { int rc; th_context *r_th; r_th = (th_context *)arg; if ((rc = xsb_command_string(r_th,"test_r_fail.")) == XSB_ERROR) fprintf(stderr,"++Command Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); else if ((rc = xsb_command_string(r_th,"test_r_fail.")) == XSB_FAILURE) fprintf(stderr,"r_fail failed properly\n"); return NULL; } /******************************************************************* Error-throwing commands *********************************************************************/ void *command_ps_err(void * arg) { int rc; th_context *p_th; p_th = (th_context *)arg; if ((rc = xsb_command_string(p_th,"test_p_err.")) == XSB_ERROR) fprintf(stderr,"Planned command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); else if ((rc = xsb_command_string(p_th,"test_p_err.")) == XSB_FAILURE) fprintf(stderr,"++p_err failed improperly\n"); return NULL; } void *command_rs_err(void * arg) { int rc; th_context *r_th; r_th = (th_context *)arg; if ((rc = xsb_command_string(r_th,"test_r_err.")) == XSB_ERROR) fprintf(stderr,"Planned command error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); else if ((rc = xsb_command_string(r_th,"test_r_fail.")) == XSB_FAILURE) fprintf(stderr,"++r_err failed improperly\n"); return NULL; } /******************************************************************* Successful queries *********************************************************************/ void *query_ps(void * arg) { int rc; th_context *p_th; XSB_StrDefine(p_return_string); p_th = (th_context *)arg; rc = xsb_query_string_string(p_th,"p(X,Y,Z).",&p_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Return p %s\n",(p_return_string.string)); rc = xsb_next_string(p_th, &p_return_string,"|"); } if (rc == XSB_ERROR) fprintf(stderr,"++Query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); return NULL; } void *query_rs(void * arg) { int rc; th_context *r_th; XSB_StrDefine(r_return_string); r_th = (th_context *)arg; rc = xsb_query_string_string(r_th,"r(X,Y,Z).",&r_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Return r %s\n",(r_return_string.string)); rc = xsb_next_string(r_th, &r_return_string,"|"); } if (rc == XSB_ERROR) fprintf(stderr,"++Query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); return NULL; } /******************************************************************* Closed queries *********************************************************************/ void *close_query_ps(void * arg) { int rc; th_context *p_th; XSB_StrDefine(p_return_string); p_th = (th_context *)arg; rc = xsb_query_string_string(p_th,"p(X,Y,Z).",&p_return_string,"|"); if (rc == XSB_ERROR) fprintf(stderr,"++Closed query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); printf("Closed return p %s\n",(p_return_string.string)); rc = xsb_close_query(p_th); if (rc == XSB_FAILURE) fprintf(stderr,"++Failure on closed query (p)!\n"); return NULL; } void *close_query_rs(void * arg) { int rc; th_context *r_th; XSB_StrDefine(r_return_string); r_th = (th_context *)arg; rc = xsb_query_string_string(r_th,"r(X,Y,Z).",&r_return_string,"|"); if (rc == XSB_ERROR) fprintf(stderr,"++Closed query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); printf("Closed return r %s\n",(r_return_string.string)); rc = xsb_close_query(r_th); if (rc == XSB_FAILURE) fprintf(stderr,"++Failure on closed query (r)!\n"); return NULL; } /******************************************************************* Error-throwing queries *********************************************************************/ void *query_ps_err(void * arg) { int rc; th_context *p_th; XSB_StrDefine(p_return_string); p_th = (th_context *)arg; rc = xsb_query_string_string(p_th,"p_err(X,Y,Z).",&p_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Pre-err return p %s\n",(p_return_string.string)); rc = xsb_next_string(p_th, &p_return_string,"|"); } if (rc == XSB_ERROR) fprintf(stderr,"Planned query Error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); return NULL; } void *query_rs_err(void * arg) { int rc; th_context *r_th; XSB_StrDefine(r_return_string); r_th = (th_context *)arg; rc = xsb_query_string_string(r_th,"r_err(X,Y,Z).",&r_return_string,"|"); while (rc == XSB_SUCCESS) { printf("Pre-err return r %s\n",(r_return_string.string)); rc = xsb_next_string(r_th, &r_return_string,"|"); } if (rc == XSB_ERROR) fprintf(stderr,"Planned query Error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); return NULL; } int main(int argc, char *argv[]) { static th_context *main_th; static th_context *r_th; static th_context *q_th; pthread_t qthread_id,rthread_id; int qstatus, rstatus, rc; void *qreturn; void *rreturn; char init_string[MAXPATHLEN]; /* xsb_init_string relies on the calling program to pass the absolute or relative path name of the XSB installation directory. We assume that the current program is sitting in the directory ../examples/c_calling_xsb/ To get installation directory, we strip 3 file names from the path. */ strcpy(init_string,strip_names_from_path(xsb_executable_full_path(argv[0]),3)); if (xsb_init_string(init_string)) { fprintf(stderr,"%s initializing XSB: %s\n",xsb_get_error_type(xsb_get_main_thread()), xsb_get_error_message(xsb_get_main_thread())); exit(XSB_ERROR); } main_th = xsb_get_main_thread(); /* Create command to consult a file: edb.P, and send it. */ if (xsb_command_string(main_th, "consult('edb.P').") == XSB_ERROR) fprintf(stderr,"++Error consulting edb.P: %s/%s\n",xsb_get_error_type(main_th), xsb_get_error_message(main_th)); xsb_ccall_thread_create(main_th,&r_th); xsb_ccall_thread_create(main_th,&q_th); /* while (!(r_th->_xsb_ready)) usleep(1000); while (!(q_th->_xsb_ready)) usleep(1000); */ pthread_create(&rthread_id,NULL,command_rs,r_th); pthread_create(&qthread_id,NULL,command_ps,q_th); pthread_create(&rthread_id,NULL,command_rs,r_th); pthread_create(&qthread_id,NULL,command_ps,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); pthread_create(&rthread_id,NULL,query_rs,r_th); pthread_create(&qthread_id,NULL,query_ps,q_th); pthread_create(&rthread_id,NULL,query_rs,r_th); pthread_create(&qthread_id,NULL,query_ps,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); printf("/******************************************/\n"); pthread_create(&rthread_id,NULL,command_rs_fail,r_th); pthread_create(&qthread_id,NULL,command_ps_fail,q_th); pthread_create(&rthread_id,NULL,command_rs_fail,r_th); pthread_create(&qthread_id,NULL,command_ps_fail,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); pthread_create(&rthread_id,NULL,close_query_rs,r_th); pthread_create(&qthread_id,NULL,close_query_ps,q_th); pthread_create(&rthread_id,NULL,close_query_rs,r_th); pthread_create(&qthread_id,NULL,close_query_ps,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); printf("/******************************************/\n"); pthread_create(&rthread_id,NULL,command_rs_err,r_th); pthread_create(&qthread_id,NULL,command_ps_err,q_th); pthread_create(&rthread_id,NULL,command_rs_err,r_th); pthread_create(&qthread_id,NULL,command_ps_err,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); pthread_create(&rthread_id,NULL,query_rs_err,r_th); pthread_create(&qthread_id,NULL,query_ps_err,q_th); pthread_create(&rthread_id,NULL,query_rs_err,r_th); pthread_create(&qthread_id,NULL,query_ps_err,q_th); rstatus = pthread_join(rthread_id,&rreturn); if (rstatus != 0) fprintf(stderr,"R join returns status %d\n",rstatus); qstatus = pthread_join(qthread_id,&qreturn); if (qstatus != 0) fprintf(stderr,"Q join returns status %d\n",qstatus); if ((rc = xsb_command_string(r_th,"test_r.")) != XSB_SUCCESS) fprintf(stderr,"error on test_r: rc: %d\n",rc); if (xsb_command_string(r_th,"import thread_exit/0 from thread.") == XSB_ERROR) fprintf(stderr,"++Error exiting r: %s/%s\n",xsb_get_error_type(r_th), xsb_get_error_message(r_th)); xsb_kill_thread(r_th); //xsb_command_string(r_th,"thread_exit."); /* if (xsb_command_string(r_th,"thread_exit.") == XSB_ERROR) fprintf(stderr,"++Error exiting: %s/%s\n",xsb_get_error_type(r_th), xsb_get_error_message(r_th)); */ xsb_close(xsb_get_main_thread()); /* Close connection */ return(0); } /******************************************************************* Low-level commands *********************************************************************/ void *ll_command_ps(void * arg) { th_context *p_th; int rc = 0; p_th = (th_context *)arg; c2p_functor(p_th,"ll_test_p",0,reg_term(p_th,1)); if ((rc = xsb_command(p_th)) == XSB_ERROR) fprintf(stderr,"LowLevel command error p: %s/%s\n",xsb_get_error_type(p_th),xsb_get_error_message(p_th)); else if (rc == XSB_FAILURE) printf("llp failed\n"); else if (rc == XSB_SUCCESS) printf("llp succeeded\n"); return NULL; } void *ll_command_rs(void * arg) { th_context *r_th; r_th = (th_context *)arg; c2p_functor(r_th,"ll_test_r",0,reg_term(r_th,1)); if (xsb_command(r_th) == XSB_ERROR) fprintf(stderr,"LowLevel command error r: %s/%s\n",xsb_get_error_type(r_th),xsb_get_error_message(r_th)); return NULL; } --- NEW FILE: make_thread2.P --- %% This compiles the examples below on Unix :- xsb_configuration(compiler_flags,CFLAGS), xsb_configuration(loader_flags,LDFLAGS), xsb_configuration(config_dir,CONFDIR), xsb_configuration(emudir,Emudir), xsb_configuration(compiler,Compiler), str_cat(CONFDIR, '/saved.o/', ObjDir), shell([Compiler, ' -c ', ' -I', Emudir, ' -I', CONFDIR, ' ', CFLAGS, ' cvarstring_thread2.c ']), shell([Compiler, ' -o ', './cvstest_thread2 ', ObjDir, 'xsb.o ', ' cvarstring_thread2.o ', LDFLAGS]), writeln(done), writeln('You can find the executable in ./cvstest_thread2'). |