Better check for 64-bit.
[AROS-Contrib.git] / sqlite3 / test / threadtest2.c
blobf16fd1a12ee4a788782ad461468949d5e536e172
1 /*
2 ** 2004 January 13
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file implements a simple standalone program used to test whether
13 ** or not the SQLite library is threadsafe.
15 ** This file is NOT part of the standard SQLite library. It is used for
16 ** testing only.
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <pthread.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "sqlite.h"
26 ** Name of the database
28 #define DB_FILE "test.db"
30 /*
31 ** When this variable becomes non-zero, all threads stop
32 ** what they are doing.
34 volatile int all_stop = 0;
36 /*
37 ** Callback from the integrity check. If the result is anything other
38 ** than "ok" it means the integrity check has failed. Set the "all_stop"
39 ** global variable to stop all other activity. Print the error message
40 ** or print OK if the string "ok" is seen.
42 int check_callback(void *notUsed, int argc, char **argv, char **notUsed2){
43 if( strcmp(argv[0],"ok") ){
44 all_stop = 1;
45 fprintf(stderr,"pid=%d. %s\n", getpid(), argv[0]);
46 }else{
47 /* fprintf(stderr,"pid=%d. OK\n", getpid()); */
49 return 0;
53 ** Do an integrity check on the database. If the first integrity check
54 ** fails, try it a second time.
56 int integrity_check(sqlite *db){
57 int rc;
58 if( all_stop ) return 0;
59 /* fprintf(stderr,"pid=%d: CHECK\n", getpid()); */
60 rc = sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0);
61 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
62 fprintf(stderr,"pid=%d, Integrity check returns %d\n", getpid(), rc);
64 if( all_stop ){
65 sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0);
67 return 0;
71 ** This is the worker thread
73 void *worker(void *notUsed){
74 sqlite *db;
75 int rc;
76 int cnt = 0;
77 while( !all_stop && cnt++<10000 ){
78 if( cnt%1000==0 ) printf("pid=%d: %d\n", getpid(), cnt);
79 while( (sqlite3_open(DB_FILE, &db))!=SQLITE_OK ) sched_yield();
80 sqlite3_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0);
81 integrity_check(db);
82 if( all_stop ){ sqlite3_close(db); break; }
83 /* fprintf(stderr, "pid=%d: BEGIN\n", getpid()); */
84 rc = sqlite3_exec(db, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0);
85 /* fprintf(stderr, "pid=%d: END rc=%d\n", getpid(), rc); */
86 sqlite3_close(db);
88 return 0;
92 ** Initialize the database and start the threads
94 int main(int argc, char **argv){
95 sqlite *db;
96 int i, rc;
97 pthread_t aThread[5];
99 if( strcmp(DB_FILE,":memory:") ){
100 char *zJournal = sqlite3_mprintf("%s-journal", DB_FILE);
101 unlink(DB_FILE);
102 unlink(zJournal);
103 free(zJournal);
105 sqlite3_open(DB_FILE, &db);
106 if( db==0 ){
107 fprintf(stderr,"unable to initialize database\n");
108 exit(1);
110 rc = sqlite3_exec(db, "CREATE TABLE t1(x);", 0,0,0);
111 if( rc ){
112 fprintf(stderr,"cannot create table t1: %d\n", rc);
113 exit(1);
115 sqlite3_close(db);
116 for(i=0; i<sizeof(aThread)/sizeof(aThread[0]); i++){
117 pthread_create(&aThread[i], 0, worker, 0);
119 for(i=0; i<sizeof(aThread)/sizeof(aThread[i]); i++){
120 pthread_join(aThread[i], 0);
122 if( !all_stop ){
123 printf("Everything seems ok.\n");
124 return 0;
125 }else{
126 printf("We hit an error.\n");
127 return 1;