⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test4.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** 2003 December 18**** 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 the SQLite library in a multithreaded environment.**** $Id: test4.c,v 1.23 2008/07/28 19:34:54 drh Exp $*/#include "sqliteInt.h"#include "tcl.h"#if defined(SQLITE_OS_UNIX) && OS_UNIX==1 && SQLITE_THREADSAFE#include <stdlib.h>#include <string.h>#include <pthread.h>#include <sched.h>#include <ctype.h>/*** 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 master and read-only  ** to the thread. */  char *zFilename;       /* Name of database file */  void (*xOp)(Thread*);  /* next operation to do */  char *zArg;            /* argument usable by xOp */  int opnum;             /* Operation number */  int busy;              /* True if this thread is in use */  /* The next group of fields are writable by the thread but read-only to the  ** master. */  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 *thread_main(void *pArg){  Thread *p = (Thread*)pArg;  if( p->db ){    sqlite3_close(p->db);  }  sqlite3_open(p->zFilename, &p->db);  if( SQLITE_OK!=sqlite3_errcode(p->db) ){    p->zErr = strdup(sqlite3_errmsg(p->db));    sqlite3_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_finalize(p->pStmt);    p->pStmt = 0;  }  if( p->db ){    sqlite3_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_thread_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:    thread_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_thread_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_thread_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;  sqlite3_free(threadset[i].zFilename);  threadset[i].zFilename = sqlite3DbStrDup(0, argv[2]);  threadset[i].opnum = 1;  threadset[i].completed = 0;  rc = pthread_create(&x, 0, thread_main, &threadset[i]);  if( rc ){    Tcl_AppendResult(interp, "failed to create the thread", 0);    sqlite3_free(threadset[i].zFilename);    threadset[i].busy = 0;    return TCL_ERROR;  }  pthread_detach(x);  return TCL_OK;}/*** Wait for a thread to reach its idle state.*/static void thread_wait(Thread *p){  while( p->opnum>p->completed ) sched_yield();}/*** Usage:  thread_wait ID**** Wait on thread ID to reach its idle state.*/static int tcl_thread_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_thread_id(interp, argv[1]);  if( i<0 ) return TCL_ERROR;  if( !threadset[i].busy ){    Tcl_AppendResult(interp, "no such thread", 0);    return TCL_ERROR;  }  thread_wait(&threadset[i]);  return TCL_OK;}/*** Stop a thread.*/static void stop_thread(Thread *p){  thread_wait(p);  p->xOp = 0;  p->opnum++;  thread_wait(p);  sqlite3_free(p->zArg);  p->zArg = 0;  sqlite3_free(p->zFilename);  p->zFilename = 0;  p->busy = 0;}/*** Usage:  thread_halt ID**** Cause a thread to shut itself down.  Wait for the shutdown to be** completed.  If ID is "*" then stop all threads.*/static int tcl_thread_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_thread_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]);  }  return TCL_OK;}/*** Usage: thread_argc  ID**** Wait on the most recent thread_step to complete, then return the** number of columns in the result set.*/static int tcl_thread_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_thread_id(interp, argv[1]);  if( i<0 ) return TCL_ERROR;  if( !threadset[i].busy ){    Tcl_AppendResult(interp, "no such thread", 0);    return TCL_ERROR;  }  thread_wait(&threadset[i]);  sprintf(zBuf, "%d", threadset[i].argc);  Tcl_AppendResult(interp, zBuf, 0);  return TCL_OK;}/*** Usage: thread_argv  ID   N**** Wait on the most recent thread_step to complete, then return the** value of the N-th columns in the result set.*/static int tcl_thread_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_thread_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;  thread_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: thread_colname  ID   N**** Wait on the most recent thread_step to complete, then return the** name of the N-th columns in the result set.*/static int tcl_thread_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;  }  i = parse_thread_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;  thread_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].colv[n], 0);  return TCL_OK;}/*** Usage: thread_result  ID**** Wait on the most recent operation to complete, then return the** result code from that operation.*/static int tcl_thread_result(  void *NotUsed,  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */  int argc,              /* Number of arguments */  const char **argv      /* Text of each argument */){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -