#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 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.3 2004/04/23 17:04:45 drh Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#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.
*/
struct Thread {
/* The first group of fields are writable by the master and read-only
** to the thread. */
/* The next group of fields are writable by the thread but read-only to the
** master. */
};
/*
** There can be as many as 26 threads running at once. Each is named
** by a capital letter: A, B, C, ..., Y, Z.
*/
/*
** The main loop for a thread. Threads use busy waiting.
*/
if( p->db ){
sqlite_close(p->db);
}
p->vm = 0;
p->completed = 1;
while( p->xOp ){
sqlite_freemem(p->zErr);
p->zErr = 0;
}
(*p->xOp)(p);
p->completed++;
}
if( p->vm ){
sqlite_finalize(p->vm, 0);
p->vm = 0;
}
if( p->db ){
sqlite_close(p->db);
p->db = 0;
}
sqlite_freemem(p->zErr);
p->zErr = 0;
}
p->completed++;
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.
*/
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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
pthread_t x;
int rc;
if( argc!=3 ){
" ID FILENAME", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
if( rc ){
return TCL_ERROR;
}
pthread_detach(x);
return TCL_OK;
}
/*
** Wait for a thread to reach its idle state.
*/
}
/*
** Usage: thread_wait ID
**
** Wait on thread ID to reach its idle state.
*/
static int tcl_thread_wait(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" ID", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_OK;
}
/*
** Stop a thread.
*/
thread_wait(p);
p->xOp = 0;
p->opnum++;
thread_wait(p);
sqliteFree(p->zArg);
p->zArg = 0;
sqliteFree(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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" ID", 0);
return TCL_ERROR;
}
for(i=0; i<N_THREAD; i++){
}
}else{
if( i<0 ) return TCL_ERROR;
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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" ID", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
int n;
if( argc!=3 ){
" ID N", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_ERROR;
}
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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
int n;
if( argc!=3 ){
" ID N", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_ERROR;
}
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,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
const char *zName;
if( argc!=2 ){
" ID", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
default: zName = "SQLITE_Unknown"; break;
}
return TCL_OK;
}
/*
** Usage: thread_error ID
**
** Wait on the most recent operation to complete, then return the
** error string.
*/
static int tcl_thread_error(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" ID", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_OK;
}
/*
** This procedure runs in the thread to compile an SQL statement.
*/
if( p->db==0 ){
p->rc = SQLITE_ERROR;
return;
}
if( p->vm ){
sqlite_finalize(p->vm, 0);
p->vm = 0;
}
}
/*
** Usage: thread_compile ID SQL
**
** Compile a new virtual machine.
*/
static int tcl_thread_compile(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=3 ){
" ID SQL", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_OK;
}
/*
** This procedure runs in the thread to step the virtual machine.
*/
if( p->vm==0 ){
p->rc = SQLITE_ERROR;
return;
}
}
/*
** Usage: thread_step ID
**
** Advance the virtual machine by one step
*/
static int tcl_thread_step(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" IDL", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_OK;
}
/*
** This procedure runs in the thread to finalize a virtual machine.
*/
if( p->vm==0 ){
p->rc = SQLITE_ERROR;
return;
}
p->vm = 0;
}
/*
** Usage: thread_finalize ID
**
** Finalize the virtual machine.
*/
static int tcl_thread_finalize(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i;
if( argc!=2 ){
" IDL", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
return TCL_OK;
}
/*
** Usage: thread_swap ID ID
**
** Interchange the sqlite* pointer between two threads.
*/
static int tcl_thread_swap(
void *NotUsed,
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
int i, j;
if( argc!=3 ){
" ID1 ID2", 0);
return TCL_ERROR;
}
if( i<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[i]);
if( j<0 ) return TCL_ERROR;
return TCL_ERROR;
}
thread_wait(&threadset[j]);
return TCL_OK;
}
/*
** Register commands with the TCL interpreter.
*/
static struct {
char *zName;
} aCmd[] = {
};
int i;
}
return TCL_OK;
}
#else
#endif /* OS_UNIX */