threadtest1.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. ** 2002 January 15
  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.
  10. **
  11. *************************************************************************
  12. ** This file implements a simple standalone program used to test whether
  13. ** or not the SQLite library is threadsafe.
  14. **
  15. ** Testing the thread safety of SQLite is difficult because there are very
  16. ** few places in the code that are even potentially unsafe, and those
  17. ** places execute for very short periods of time. So even if the library
  18. ** is compiled with its mutexes disabled, it is likely to work correctly
  19. ** in a multi-threaded program most of the time.
  20. **
  21. ** This file is NOT part of the standard SQLite library. It is used for
  22. ** testing only.
  23. */
  24. #include "sqlite.h"
  25. #include <pthread.h>
  26. #include <sched.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. /*
  32. ** Enable for tracing
  33. */
  34. static int verbose = 0;
  35. /*
  36. ** Come here to die.
  37. */
  38. static void Exit(int rc){
  39. exit(rc);
  40. }
  41. extern char *sqlite3_mprintf(const char *zFormat, ...);
  42. extern char *sqlite3_vmprintf(const char *zFormat, va_list);
  43. /*
  44. ** When a lock occurs, yield.
  45. */
  46. static int db_is_locked(void *NotUsed, int iCount){
  47. /* sched_yield(); */
  48. if( verbose ) printf("BUSY %s #%d\n", (char*)NotUsed, iCount);
  49. usleep(100);
  50. return iCount<25;
  51. }
  52. /*
  53. ** Used to accumulate query results by db_query()
  54. */
  55. struct QueryResult {
  56. const char *zFile; /* Filename - used for error reporting */
  57. int nElem; /* Number of used entries in azElem[] */
  58. int nAlloc; /* Number of slots allocated for azElem[] */
  59. char **azElem; /* The result of the query */
  60. };
  61. /*
  62. ** The callback function for db_query
  63. */
  64. static int db_query_callback(
  65. void *pUser, /* Pointer to the QueryResult structure */
  66. int nArg, /* Number of columns in this result row */
  67. char **azArg, /* Text of data in all columns */
  68. char **NotUsed /* Names of the columns */
  69. ){
  70. struct QueryResult *pResult = (struct QueryResult*)pUser;
  71. int i;
  72. if( pResult->nElem + nArg >= pResult->nAlloc ){
  73. if( pResult->nAlloc==0 ){
  74. pResult->nAlloc = nArg+1;
  75. }else{
  76. pResult->nAlloc = pResult->nAlloc*2 + nArg + 1;
  77. }
  78. pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
  79. if( pResult->azElem==0 ){
  80. fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
  81. return 1;
  82. }
  83. }
  84. if( azArg==0 ) return 0;
  85. for(i=0; i<nArg; i++){
  86. pResult->azElem[pResult->nElem++] =
  87. sqlite3_mprintf("%s",azArg[i] ? azArg[i] : "");
  88. }
  89. return 0;
  90. }
  91. /*
  92. ** Execute a query against the database. NULL values are returned
  93. ** as an empty string. The list is terminated by a single NULL pointer.
  94. */
  95. char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
  96. char *zSql;
  97. int rc;
  98. char *zErrMsg = 0;
  99. va_list ap;
  100. struct QueryResult sResult;
  101. va_start(ap, zFormat);
  102. zSql = sqlite3_vmprintf(zFormat, ap);
  103. va_end(ap);
  104. memset(&sResult, 0, sizeof(sResult));
  105. sResult.zFile = zFile;
  106. if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
  107. rc = sqlite3_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
  108. if( rc==SQLITE_SCHEMA ){
  109. if( zErrMsg ) free(zErrMsg);
  110. rc = sqlite3_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
  111. }
  112. if( verbose ) printf("DONE %s %s\n", zFile, zSql);
  113. if( zErrMsg ){
  114. fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
  115. free(zErrMsg);
  116. free(zSql);
  117. Exit(1);
  118. }
  119. sqlite3_free(zSql);
  120. if( sResult.azElem==0 ){
  121. db_query_callback(&sResult, 0, 0, 0);
  122. }
  123. sResult.azElem[sResult.nElem] = 0;
  124. return sResult.azElem;
  125. }
  126. /*
  127. ** Execute an SQL statement.
  128. */
  129. void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
  130. char *zSql;
  131. int rc;
  132. char *zErrMsg = 0;
  133. va_list ap;
  134. va_start(ap, zFormat);
  135. zSql = sqlite3_vmprintf(zFormat, ap);
  136. va_end(ap);
  137. if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
  138. do{
  139. rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
  140. }while( rc==SQLITE_BUSY );
  141. if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
  142. if( zErrMsg ){
  143. fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
  144. free(zErrMsg);
  145. sqlite3_free(zSql);
  146. Exit(1);
  147. }
  148. sqlite3_free(zSql);
  149. }
  150. /*
  151. ** Free the results of a db_query() call.
  152. */
  153. void db_query_free(char **az){
  154. int i;
  155. for(i=0; az[i]; i++){
  156. sqlite3_free(az[i]);
  157. }
  158. free(az);
  159. }
  160. /*
  161. ** Check results
  162. */
  163. void db_check(const char *zFile, const char *zMsg, char **az, ...){
  164. va_list ap;
  165. int i;
  166. char *z;
  167. va_start(ap, az);
  168. for(i=0; (z = va_arg(ap, char*))!=0; i++){
  169. if( az[i]==0 || strcmp(az[i],z)!=0 ){
  170. fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
  171. zFile, zMsg, i+1, az[i]);
  172. db_query_free(az);
  173. Exit(1);
  174. }
  175. }
  176. va_end(ap);
  177. db_query_free(az);
  178. }
  179. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  180. pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
  181. int thread_cnt = 0;
  182. static void *worker_bee(void *pArg){
  183. const char *zFilename = (char*)pArg;
  184. char *azErr;
  185. int i, cnt;
  186. int t = atoi(zFilename);
  187. char **az;
  188. sqlite *db;
  189. pthread_mutex_lock(&lock);
  190. thread_cnt++;
  191. pthread_mutex_unlock(&lock);
  192. printf("%s: START\n", zFilename);
  193. fflush(stdout);
  194. for(cnt=0; cnt<10; cnt++){
  195. sqlite3_open(&zFilename[2], &db);
  196. if( db==0 ){
  197. fprintf(stdout,"%s: can't open\n", zFilename);
  198. Exit(1);
  199. }
  200. sqlite3_busy_handler(db, db_is_locked, zFilename);
  201. db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
  202. for(i=1; i<=100; i++){
  203. db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
  204. t, i, i*2, i*i);
  205. }
  206. az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
  207. db_check(zFilename, "tX size", az, "100", 0);
  208. az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
  209. db_check(zFilename, "tX avg", az, "101", 0);
  210. db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
  211. az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
  212. db_check(zFilename, "tX avg2", az, "51", 0);
  213. for(i=1; i<=50; i++){
  214. char z1[30], z2[30];
  215. az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
  216. sprintf(z1, "%d", i*2);
  217. sprintf(z2, "%d", i*i);
  218. db_check(zFilename, "readback", az, z1, z2, 0);
  219. }
  220. db_execute(db, zFilename, "DROP TABLE t%d;", t);
  221. sqlite3_close(db);
  222. }
  223. printf("%s: END\n", zFilename);
  224. /* unlink(zFilename); */
  225. fflush(stdout);
  226. pthread_mutex_lock(&lock);
  227. thread_cnt--;
  228. if( thread_cnt<=0 ){
  229. pthread_cond_signal(&sig);
  230. }
  231. pthread_mutex_unlock(&lock);
  232. return 0;
  233. }
  234. int main(int argc, char **argv){
  235. char *zFile;
  236. int i, n;
  237. pthread_t id;
  238. if( argc>2 && strcmp(argv[1], "-v")==0 ){
  239. verbose = 1;
  240. argc--;
  241. argv++;
  242. }
  243. if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
  244. for(i=0; i<n; i++){
  245. char zBuf[200];
  246. sprintf(zBuf, "testdb-%d", (i+1)/2);
  247. unlink(zBuf);
  248. }
  249. for(i=0; i<n; i++){
  250. zFile = sqlite3_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
  251. if( (i%2)==0 ){
  252. /* Remove both the database file and any old journal for the file
  253. ** being used by this thread and the next one. */
  254. char *zDb = &zFile[2];
  255. char *zJournal = sqlite3_mprintf("%s-journal", zDb);
  256. unlink(zDb);
  257. unlink(zJournal);
  258. free(zJournal);
  259. }
  260. pthread_create(&id, 0, worker_bee, (void*)zFile);
  261. pthread_detach(id);
  262. }
  263. pthread_mutex_lock(&lock);
  264. while( thread_cnt>0 ){
  265. pthread_cond_wait(&sig, &lock);
  266. }
  267. pthread_mutex_unlock(&lock);
  268. for(i=0; i<n; i++){
  269. char zBuf[200];
  270. sprintf(zBuf, "testdb-%d", (i+1)/2);
  271. unlink(zBuf);
  272. }
  273. return 0;
  274. }