blob: 685562f771c322cd86cbcc083b9de0d07f5ac595 [file] [log] [blame]
danielk19771a9ed0b2008-06-18 09:45:56 +00001# 2008 June 17
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
danielk197771bc31c2008-06-26 08:29:34 +000012# $Id: mutex1.test,v 1.5 2008/06/26 08:29:35 danielk1977 Exp $
danielk19771a9ed0b2008-06-18 09:45:56 +000013
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
drh55b0cf02008-06-19 17:54:33 +000016sqlite3_reset_auto_extension
danielk19771a9ed0b2008-06-18 09:45:56 +000017
18proc mutex_counters {varname} {
19 upvar $varname var
20 set var(total) 0
21 foreach {name value} [read_mutex_counters] {
22 set var($name) $value
23 incr var(total) $value
24 }
25}
26
27#-------------------------------------------------------------------------
28# Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
29# is called at the wrong time. And that the first time sqlite3_initialize
danielk197771bc31c2008-06-26 08:29:34 +000030# is called it obtains the 'static_master' mutex 3 times and a recursive
31# mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops
32# that do not require any mutexes.
danielk19771a9ed0b2008-06-18 09:45:56 +000033#
34do_test mutex1-1.0 {
35 install_mutex_counters 1
36} {SQLITE_MISUSE}
37
38do_test mutex1-1.1 {
39 db close
40 install_mutex_counters 1
41} {SQLITE_MISUSE}
42
43do_test mutex1-1.2 {
44 sqlite3_shutdown
45 install_mutex_counters 1
46} {SQLITE_OK}
47
48do_test mutex1-1.3 {
49 install_mutex_counters 0
50} {SQLITE_OK}
51
52do_test mutex1-1.4 {
53 install_mutex_counters 1
54} {SQLITE_OK}
55
56do_test mutex1-1.5 {
57 mutex_counters counters
58 set counters(total)
59} {0}
60
61do_test mutex1-1.6 {
62 sqlite3_initialize
63} {SQLITE_OK}
64
65do_test mutex1-1.7 {
66 mutex_counters counters
67 list $counters(total) $counters(static_master)
danielk197771bc31c2008-06-26 08:29:34 +000068} {6 3}
danielk19771a9ed0b2008-06-18 09:45:56 +000069
70do_test mutex1-1.8 {
71 clear_mutex_counters
72 sqlite3_initialize
73} {SQLITE_OK}
74
75do_test mutex1-1.9 {
76 mutex_counters counters
77 list $counters(total) $counters(static_master)
78} {0 0}
79
danielk197759f8c082008-06-18 17:09:10 +000080#-------------------------------------------------------------------------
81# Tests mutex1-2.* test the three thread-safety related modes that
82# can be selected using sqlite3_config:
83#
84# * Serialized mode,
85# * Multi-threaded mode,
86# * Single-threaded mode.
87#
88
danielk197701bf2992008-06-18 17:59:03 +000089ifcapable threadsafe {
90 foreach {mode mutexes} {
91 singlethread {}
92 multithread {fast static_master static_mem static_prng}
93 serialized {fast recursive static_master static_mem static_prng}
94 } {
95 do_test mutex1.2.$mode.1 {
96 catch {db close}
97 sqlite3_shutdown
98 sqlite3_config $mode
99 } SQLITE_OK
danielk197771bc31c2008-06-26 08:29:34 +0000100
danielk197701bf2992008-06-18 17:59:03 +0000101 do_test mutex1.2.$mode.2 {
danielk197771bc31c2008-06-26 08:29:34 +0000102 sqlite3_initialize
danielk197701bf2992008-06-18 17:59:03 +0000103 clear_mutex_counters
104 sqlite3 db test.db
105 catchsql { CREATE TABLE abc(a, b, c) }
106 db eval {
107 INSERT INTO abc VALUES(1, 2, 3);
danielk197759f8c082008-06-18 17:09:10 +0000108 }
danielk197701bf2992008-06-18 17:59:03 +0000109 } {}
110
111 do_test mutex1.2.$mode.3 {
112 mutex_counters counters
113
114 set res [list]
115 foreach {key value} [array get counters] {
116 if {$key ne "total" && $value > 0} {
117 lappend res $key
118 }
119 }
120 lsort $res
121 } $mutexes
122 }
danielk197759f8c082008-06-18 17:09:10 +0000123}
danielk19771a9ed0b2008-06-18 09:45:56 +0000124
125do_test mutex1-X {
danielk197701bf2992008-06-18 17:59:03 +0000126 catch {db close}
danielk19771a9ed0b2008-06-18 09:45:56 +0000127 sqlite3_shutdown
128 clear_mutex_counters
129 install_mutex_counters 0
danielk197701bf2992008-06-18 17:59:03 +0000130 sqlite3_initialize
danielk19771a9ed0b2008-06-18 09:45:56 +0000131} {SQLITE_OK}
132
drh55b0cf02008-06-19 17:54:33 +0000133autoinstall_test_functions
danielk19771a9ed0b2008-06-18 09:45:56 +0000134finish_test