2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2004 January 13
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This file implements a simple standalone program used to test whether
2N/A** or not the SQLite library is threadsafe.
2N/A**
2N/A** This file is NOT part of the standard SQLite library. It is used for
2N/A** testing only.
2N/A*/
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <pthread.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include "sqlite.h"
2N/A
2N/A/*
2N/A** Name of the database
2N/A*/
2N/A#define DB_FILE "test.db"
2N/A
2N/A/*
2N/A** When this variable becomes non-zero, all threads stop
2N/A** what they are doing.
2N/A*/
2N/Avolatile int all_stop = 0;
2N/A
2N/A/*
2N/A** Callback from the integrity check. If the result is anything other
2N/A** than "ok" it means the integrity check has failed. Set the "all_stop"
2N/A** global variable to stop all other activity. Print the error message
2N/A** or print OK if the string "ok" is seen.
2N/A*/
2N/Aint check_callback(void *notUsed, int argc, char **argv, char **notUsed2){
2N/A if( strcmp(argv[0],"ok") ){
2N/A all_stop = 1;
2N/A fprintf(stderr,"pid=%d. %s\n", getpid(), argv[0]);
2N/A }else{
2N/A /* fprintf(stderr,"pid=%d. OK\n", getpid()); */
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** Do an integrity check on the database. If the first integrity check
2N/A** fails, try it a second time.
2N/A*/
2N/Aint integrity_check(sqlite *db){
2N/A int rc;
2N/A if( all_stop ) return 0;
2N/A /* fprintf(stderr,"pid=%d: CHECK\n", getpid()); */
2N/A rc = sqlite_exec(db, "pragma integrity_check", check_callback, 0, 0);
2N/A if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
2N/A fprintf(stderr,"pid=%d, Integrity check returns %d\n", getpid(), rc);
2N/A }
2N/A if( all_stop ){
2N/A sqlite_exec(db, "pragma integrity_check", check_callback, 0, 0);
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** This is the worker thread
2N/A*/
2N/Avoid *worker(void *notUsed){
2N/A sqlite *db;
2N/A int rc;
2N/A int cnt = 0;
2N/A while( !all_stop && cnt++<10000 ){
2N/A if( cnt%1000==0 ) printf("pid=%d: %d\n", getpid(), cnt);
2N/A while( (db = sqlite_open(DB_FILE, 0, 0))==0 ) sched_yield();
2N/A sqlite_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0);
2N/A integrity_check(db);
2N/A if( all_stop ){ sqlite_close(db); break; }
2N/A /* fprintf(stderr, "pid=%d: BEGIN\n", getpid()); */
2N/A rc = sqlite_exec(db, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0);
2N/A /* fprintf(stderr, "pid=%d: END rc=%d\n", getpid(), rc); */
2N/A sqlite_close(db);
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** Initialize the database and start the threads
2N/A*/
2N/Aint main(int argc, char **argv){
2N/A sqlite *db;
2N/A int i, rc;
2N/A pthread_t aThread[5];
2N/A
2N/A if( strcmp(DB_FILE,":memory:") ) unlink(DB_FILE);
2N/A db = sqlite_open(DB_FILE, 0, 0);
2N/A if( db==0 ){
2N/A fprintf(stderr,"unable to initialize database\n");
2N/A exit(1);
2N/A }
2N/A rc = sqlite_exec(db, "CREATE TABLE t1(x);", 0,0,0);
2N/A if( rc ){
2N/A fprintf(stderr,"cannot create table t1: %d\n", rc);
2N/A exit(1);
2N/A }
2N/A sqlite_close(db);
2N/A for(i=0; i<sizeof(aThread)/sizeof(aThread[0]); i++){
2N/A pthread_create(&aThread[i], 0, worker, 0);
2N/A }
2N/A for(i=0; i<sizeof(aThread)/sizeof(aThread[i]); i++){
2N/A pthread_join(aThread[i], 0);
2N/A }
2N/A if( !all_stop ){
2N/A printf("Everything seems ok.\n");
2N/A return 0;
2N/A }else{
2N/A printf("We hit an error.\n");
2N/A return 1;
2N/A }
2N/A}