📄 test7.c
字号:
/*** 2006 January 09**** The author disclaims copyright to this source code. In place of** a legal notice, here is a blessing:**** May you do good and not evil.** May you find forgiveness for yourself and forgive others.** May you share freely, never taking more than you give.***************************************************************************** Code for testing the client/server version of the SQLite library.** Derived from test4.c.**** $Id: test7.c,v 1.4 2006/03/22 22:10:08 drh Exp $*/#include "sqliteInt.h"#include "tcl.h"#include "os.h"/*** This test only works on UNIX with a THREADSAFE build that includes** the SQLITE_SERVER option.*/#if OS_UNIX && defined(THREADSAFE) && THREADSAFE==1 && \ defined(SQLITE_SERVER) && !defined(SQLITE_OMIT_SHARED_CACHE)#include <stdlib.h>#include <string.h>#include <pthread.h>#include <sched.h>#include <ctype.h>/*** Interfaces defined in server.c*/int sqlite3_client_open(const char*, sqlite3**);int sqlite3_client_prepare(sqlite3*,const char*,int, sqlite3_stmt**,const char**);int sqlite3_client_step(sqlite3_stmt*);int sqlite3_client_reset(sqlite3_stmt*);int sqlite3_client_finalize(sqlite3_stmt*);int sqlite3_client_close(sqlite3*);int sqlite3_server_start(void);int sqlite3_server_stop(void);/*** Each thread is controlled by an instance of the following** structure.*/typedef struct Thread Thread;struct Thread { /* The first group of fields are writable by the supervisor thread ** and read-only to the client threads */ char *zFilename; /* Name of database file */ void (*xOp)(Thread*); /* next operation to do */ char *zArg; /* argument usable by xOp */ volatile int opnum; /* Operation number */ volatile int busy; /* True if this thread is in use */ /* The next group of fields are writable by the client threads ** but read-only to the superviser thread. */ volatile int completed; /* Number of operations completed */ sqlite3 *db; /* Open database */ sqlite3_stmt *pStmt; /* Pending operation */ char *zErr; /* operation error */ char *zStaticErr; /* Static error message */ int rc; /* operation return code */ int argc; /* number of columns in result */ const char *argv[100]; /* result columns */ const char *colv[100]; /* result column names */};/*** There can be as many as 26 threads running at once. Each is named** by a capital letter: A, B, C, ..., Y, Z.*/#define N_THREAD 26static Thread threadset[N_THREAD];/*** The main loop for a thread. Threads use busy waiting. */static void *client_main(void *pArg){ Thread *p = (Thread*)pArg; if( p->db ){ sqlite3_client_close(p->db); } sqlite3_client_open(p->zFilename, &p->db); if( SQLITE_OK!=sqlite3_errcode(p->db) ){ p->zErr = strdup(sqlite3_errmsg(p->db)); sqlite3_client_close(p->db); p->db = 0; } p->pStmt = 0; p->completed = 1; while( p->opnum<=p->completed ) sched_yield(); while( p->xOp ){ if( p->zErr && p->zErr!=p->zStaticErr ){ sqlite3_free(p->zErr); p->zErr = 0; } (*p->xOp)(p); p->completed++; while( p->opnum<=p->completed ) sched_yield(); } if( p->pStmt ){ sqlite3_client_finalize(p->pStmt); p->pStmt = 0; } if( p->db ){ sqlite3_client_close(p->db); p->db = 0; } if( p->zErr && p->zErr!=p->zStaticErr ){ sqlite3_free(p->zErr); p->zErr = 0; } p->completed++; sqlite3_thread_cleanup(); return 0;}/*** Get a thread ID which is an upper case letter. Return the index.** If the argument is not a valid thread ID put an error message in** the interpreter and return -1.*/static int parse_client_id(Tcl_Interp *interp, const char *zArg){ if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper((unsigned char)zArg[0]) ){ Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0); return -1; } return zArg[0] - 'A';}/*** Usage: client_create NAME FILENAME**** NAME should be an upper case letter. Start the thread running with** an open connection to the given database.*/static int tcl_client_create( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; pthread_t x; int rc; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID FILENAME", 0); return TCL_ERROR; } i = parse_client_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( threadset[i].busy ){ Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0); return TCL_ERROR; } threadset[i].busy = 1; sqliteFree(threadset[i].zFilename); threadset[i].zFilename = sqliteStrDup(argv[2]); threadset[i].opnum = 1; threadset[i].completed = 0; rc = pthread_create(&x, 0, client_main, &threadset[i]); if( rc ){ Tcl_AppendResult(interp, "failed to create the thread", 0); sqliteFree(threadset[i].zFilename); threadset[i].busy = 0; return TCL_ERROR; } pthread_detach(x); sqlite3_server_start(); return TCL_OK;}/*** Wait for a thread to reach its idle state.*/static void client_wait(Thread *p){ while( p->opnum>p->completed ) sched_yield();}/*** Usage: client_wait ID**** Wait on thread ID to reach its idle state.*/static int tcl_client_wait( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_client_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } client_wait(&threadset[i]); return TCL_OK;}/*** Stop a thread.*/static void stop_thread(Thread *p){ client_wait(p); p->xOp = 0; p->opnum++; client_wait(p); sqliteFree(p->zArg); p->zArg = 0; sqliteFree(p->zFilename); p->zFilename = 0; p->busy = 0;}/*** Usage: client_halt ID**** Cause a client thread to shut itself down. Wait for the shutdown to be** completed. If ID is "*" then stop all client threads.*/static int tcl_client_halt( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } if( argv[1][0]=='*' && argv[1][1]==0 ){ for(i=0; i<N_THREAD; i++){ if( threadset[i].busy ){ stop_thread(&threadset[i]); } } }else{ i = parse_client_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } stop_thread(&threadset[i]); } /* If no client threads are still running, also stop the server */ for(i=0; i<N_THREAD && threadset[i].busy==0; i++){} if( i>=N_THREAD ){ sqlite3_server_stop(); } return TCL_OK;}/*** Usage: client_argc ID**** Wait on the most recent client_step to complete, then return the** number of columns in the result set.*/static int tcl_client_argc( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_client_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } client_wait(&threadset[i]); sprintf(zBuf, "%d", threadset[i].argc); Tcl_AppendResult(interp, zBuf, 0); return TCL_OK;}/*** Usage: client_argv ID N**** Wait on the most recent client_step to complete, then return the** value of the N-th columns in the result set.*/static int tcl_client_argv( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; int n; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID N", 0); return TCL_ERROR; } i = parse_client_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; client_wait(&threadset[i]); if( n<0 || n>=threadset[i].argc ){ Tcl_AppendResult(interp, "column number out of range", 0); return TCL_ERROR; } Tcl_AppendResult(interp, threadset[i].argv[n], 0); return TCL_OK;}/*** Usage: client_colname ID N**** Wait on the most recent client_step to complete, then return the** name of the N-th columns in the result set.*/static int tcl_client_colname( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */){ int i; int n; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID N", 0); return TCL_ERROR; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -